From ebfd07b6e999a1eab6143d509c0d4bb9825a4c5e Mon Sep 17 00:00:00 2001 From: MOKTADIR Date: Tue, 7 Mar 2023 02:32:04 +0600 Subject: [PATCH] localaization in eceptions --- lib/app/components/empty_widget.dart | 89 +++++++++---------- lib/app/service/api_exceptions.dart | 24 ++--- lib/app/service/base_controller.dart | 6 +- lib/app/service/helper/dialog_helper.dart | 3 +- .../translations/ar_AR/ar_ar_translation.dart | 30 ++++--- .../translations/en_US/en_us_translation.dart | 28 +++--- .../translations/localization_service.dart | 24 +++-- lib/config/translations/strings_enum.dart | 25 +++--- 8 files changed, 124 insertions(+), 105 deletions(-) diff --git a/lib/app/components/empty_widget.dart b/lib/app/components/empty_widget.dart index a14da15..b78807b 100644 --- a/lib/app/components/empty_widget.dart +++ b/lib/app/components/empty_widget.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:get/get.dart'; +import 'package:getx_standard/config/translations/strings_enum.dart'; import 'package:ionicons/ionicons.dart'; import 'package:lottie/lottie.dart'; @@ -10,55 +12,52 @@ class EmptyWidget extends StatelessWidget { Widget build(BuildContext context) { var theme = Theme.of(context); - return Padding( - padding: EdgeInsets.only(bottom: 100.h), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Lottie.asset( - 'animations/error.json', - height: 120.h, - repeat: true, - reverse: true, - fit: BoxFit.cover, - ), - Center( - child: Text( - "Empty or No data !", - style: TextStyle( - fontSize: 18.sp, - fontWeight: FontWeight.w500, - color: theme.hintColor.withOpacity(.5), - ), - ), - ), - SizedBox(height: 40.h), - SizedBox( - height: 44.h, - child: ElevatedButton( - onPressed: onPressed, - style: ElevatedButton.styleFrom( - backgroundColor: Colors.grey.shade50, - elevation: .5, - shadowColor: theme.hintColor, - padding: EdgeInsets.zero, - ), - child: Icon( - Ionicons.refresh, - size: 24, - color: theme.primaryColor.withOpacity(.7), - )), - ), - const SizedBox(height: 5), - Text( - "refresh!", + return Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Lottie.asset( + 'animations/error.json', + height: 120.h, + repeat: true, + reverse: true, + fit: BoxFit.cover, + ), + Center( + child: Text( + Strings.empty.tr, style: TextStyle( + fontSize: 18.sp, + fontWeight: FontWeight.w500, color: theme.hintColor.withOpacity(.5), ), ), - ], - ), + ), + SizedBox(height: 50.h), + SizedBox( + height: 44.h, + child: ElevatedButton( + onPressed: onPressed, + style: ElevatedButton.styleFrom( + backgroundColor: Colors.grey.shade50, + elevation: .5, + shadowColor: theme.hintColor, + padding: EdgeInsets.zero, + ), + child: Icon( + Ionicons.refresh, + size: 24, + color: theme.primaryColor.withOpacity(.7), + )), + ), + const SizedBox(height: 5), + Text( + Strings.refresh.tr, + style: TextStyle( + color: theme.hintColor.withOpacity(.5), + ), + ), + ], ); } } diff --git a/lib/app/service/api_exceptions.dart b/lib/app/service/api_exceptions.dart index 1d02f93..bbf3aeb 100644 --- a/lib/app/service/api_exceptions.dart +++ b/lib/app/service/api_exceptions.dart @@ -1,30 +1,32 @@ import 'package:dio/dio.dart'; +import 'package:get/get.dart'; +import 'package:getx_standard/config/translations/strings_enum.dart'; class DioExceptions implements Exception { String message = ""; DioExceptions.fromDioError(DioError dioError) { switch (dioError.type) { case DioErrorType.cancel: - message = "Request cancelled!"; + message = Strings.requestCanceled.tr; break; case DioErrorType.connectTimeout: - message = "Connection timeout!"; + message = Strings.connectionTimeout.tr; break; case DioErrorType.other: - message = "Connection problem!"; + message = Strings.connectionProblem.tr; break; case DioErrorType.receiveTimeout: - message = "Receive timeout!"; + message = Strings.receiveTimeout.tr; break; case DioErrorType.response: message = _handleError( dioError.response!.statusCode!.toInt(), dioError.response!.data); break; case DioErrorType.sendTimeout: - message = "Send timeout!"; + message = Strings.sendTimeout.tr; break; default: - message = "Something went wrong"; + message = Strings.somethingWrong.tr; break; } } @@ -32,13 +34,15 @@ class DioExceptions implements Exception { String _handleError(int statusCode, dynamic error) { switch (statusCode) { case 400: - return error['error'] ?? error["message"] ?? 'Bad request'; + return error['error'] ?? error["message"] ?? Strings.badRequest.tr; case 404: - return error['error'] ?? error["message"] ?? "Api Url Incorrect"; + return error['error'] ?? error["message"] ?? Strings.urlIncorrect.tr; case 500: - return error['error'] ?? error["message"] ?? "Internal Server Error"; + return error['error'] ?? + error["message"] ?? + Strings.internalServerError.tr; default: - return error['error'] ?? error["message"] ?? 'Something went wrong'; + return error['error'] ?? error["message"] ?? Strings.somethingWrong.tr; } } diff --git a/lib/app/service/base_controller.dart b/lib/app/service/base_controller.dart index e6ce4c2..4e9307c 100644 --- a/lib/app/service/base_controller.dart +++ b/lib/app/service/base_controller.dart @@ -1,7 +1,7 @@ -import 'dart:async'; - import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; +import 'package:getx_standard/config/translations/strings_enum.dart'; + import 'api_exceptions.dart'; import 'helper/dialog_helper.dart'; @@ -13,7 +13,7 @@ class BaseController { var errorText = DioExceptions.fromDioError(error).toString(); - showErrorDialog("Oops!", errorText); + showErrorDialog(Strings.oops.tr, errorText); if (kDebugMode) { print(errorText); diff --git a/lib/app/service/helper/dialog_helper.dart b/lib/app/service/helper/dialog_helper.dart index ed15892..1725d03 100644 --- a/lib/app/service/helper/dialog_helper.dart +++ b/lib/app/service/helper/dialog_helper.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; +import 'package:getx_standard/config/translations/strings_enum.dart'; import 'package:lottie/lottie.dart'; class DialogHelper { @@ -37,7 +38,7 @@ class DialogHelper { onPressed: () { if (Get.isDialogOpen!) Get.back(); }, - child: const Text('Okay'), + child: Text(Strings.okay.tr), ), ], ), diff --git a/lib/config/translations/ar_AR/ar_ar_translation.dart b/lib/config/translations/ar_AR/ar_ar_translation.dart index 1118b09..c5e06d5 100644 --- a/lib/config/translations/ar_AR/ar_ar_translation.dart +++ b/lib/config/translations/ar_AR/ar_ar_translation.dart @@ -1,16 +1,22 @@ import '../strings_enum.dart'; -final Map arAR = -{ - Strings.hello : 'مرحباً!', - Strings.loading : 'جاري التحميل', +final Map arAR = { + ///API EXCEPTIONS + Strings.requestCanceled: "تم إلغاء الطلب!", + Strings.connectionTimeout: "انتهى وقت محاولة الاتصال!", + Strings.receiveTimeout: "مهلة الاستلام!", + Strings.sendTimeout: "مهلة الإرسال!", + Strings.connectionProblem: "مشكلة في الاتصال!", + Strings.somethingWrong: "هناك خطأ ما!", + Strings.badRequest: "اقتراح غير جيد", + Strings.urlIncorrect: "عنوان Api Url غير صحيح", + Strings.internalServerError: "خطأ في الخادم الداخلي", - Strings.changeTheme : 'تغيير الثيم', - Strings.changeLanguage : 'تغيير اللغة', - - Strings.noInternetConnection : 'لا يوجد إتصال بالإنترنت', - Strings.serverNotResponding : 'لا يوجد إستجابة من السيرفر!', - Strings.someThingWentWorng : 'حدث خطأ غير متوقع!', - Strings.apiNotFound : 'الرابط الذي تحاول الوصول اليه غير موجود!', - Strings.serverError : 'مشكلة من السيرفر', + /// + Strings.changeTheme: 'تغيير الثيم', + Strings.changeLanguage: 'تغيير اللغة', + Strings.oops: "أُووبس!", + Strings.okay: "تمام", + Strings.refresh: "ينعش!", + Strings.empty: "فارغة أو لا توجد بيانات!", }; diff --git a/lib/config/translations/en_US/en_us_translation.dart b/lib/config/translations/en_US/en_us_translation.dart index 0d1a071..655086e 100644 --- a/lib/config/translations/en_US/en_us_translation.dart +++ b/lib/config/translations/en_US/en_us_translation.dart @@ -1,16 +1,22 @@ import '../strings_enum.dart'; const Map enUs = { - Strings.hello : 'Hello!', - Strings.loading : 'Loading', - - Strings.changeTheme : 'Change theme', - Strings.changeLanguage : 'Change language', - - Strings.noInternetConnection : 'No internet connection!', - Strings.serverNotResponding : 'Server is not responding!', - Strings.someThingWentWorng : 'Something went wrong', - Strings.apiNotFound : 'Route not found!', - Strings.serverError : 'Server error', + ///API EXCEPTIONS + Strings.requestCanceled: "Request cancelled!", + Strings.connectionTimeout: "Connection timeout!", + Strings.receiveTimeout: "Receive timeout!", + Strings.sendTimeout: "Send timeout!", + Strings.connectionProblem: "Connection problem!", + Strings.somethingWrong: "Something went wrong!", + Strings.badRequest: "Bad request", + Strings.urlIncorrect: "Api Url Incorrect", + Strings.internalServerError: "Internal Server Error", + /// + Strings.changeTheme: 'Change theme', + Strings.changeLanguage: 'Change language', + Strings.oops: "Oops!", + Strings.okay: "Okay", + Strings.refresh: "refresh!", + Strings.empty: "Empty or No data!", }; diff --git a/lib/config/translations/localization_service.dart b/lib/config/translations/localization_service.dart index cff5c57..4feb707 100644 --- a/lib/config/translations/localization_service.dart +++ b/lib/config/translations/localization_service.dart @@ -10,32 +10,31 @@ class LocalizationService extends Translations { static Locale defaultLanguage = supportedLanguages['en']!; // supported languages - static Map supportedLanguages = { - 'en' : const Locale('en', 'US'), - 'ar' : const Locale('ar', 'AR'), + static Map supportedLanguages = { + 'en': const Locale('en', 'US'), + 'ar': const Locale('ar', 'AR'), }; // supported languages fonts family (must be in assets & pubspec yaml) or you can use google fonts - static Map supportedLanguagesFontsFamilies = { - 'en' : const TextStyle(fontFamily: 'Poppins'), + static Map supportedLanguagesFontsFamilies = { + 'en': const TextStyle(fontFamily: 'Poppins'), 'ar': const TextStyle(fontFamily: 'Cairo'), }; @override Map> get keys => { - 'en_US': enUs, - 'ar_AR': arAR, - }; + 'en_US': enUs, + 'ar_AR': arAR, + }; /// check if the language is supported static isLanguageSupported(String languageCode) => - supportedLanguages.keys.contains(languageCode); - + supportedLanguages.keys.contains(languageCode); /// update app language by code language for example (en,ar..etc) static updateLanguage(String languageCode) async { // check if the language is supported - if(!isLanguageSupported(languageCode)) return; + if (!isLanguageSupported(languageCode)) return; // update current language in shared pref MySharedPref.setCurrentLanguage(languageCode); await Get.updateLocale(supportedLanguages[languageCode]!); @@ -46,6 +45,5 @@ class LocalizationService extends Translations { MySharedPref.getCurrentLocal().languageCode.toLowerCase().contains('en'); /// get current locale - static Locale getCurrentLocal () => MySharedPref.getCurrentLocal(); + static Locale getCurrentLocal() => MySharedPref.getCurrentLocal(); } - diff --git a/lib/config/translations/strings_enum.dart b/lib/config/translations/strings_enum.dart index df3059a..4c76981 100644 --- a/lib/config/translations/strings_enum.dart +++ b/lib/config/translations/strings_enum.dart @@ -1,15 +1,20 @@ class Strings { - static const String hello = 'hello'; - static const String loading = 'loading'; - + /// API EXCEPTIONS + static const String requestCanceled = "Request cancelled!"; + static const String connectionTimeout = "Connection timeout!"; + static const String receiveTimeout = "Receive timeout!"; + static const String sendTimeout = "Send timeout!"; + static const String connectionProblem = "Connection problem!"; + static const String somethingWrong = "Something went wrong!"; + static const String badRequest = "Bad request"; + static const String urlIncorrect = "Api Url Incorrect"; + static const String internalServerError = "Internal Server Error"; + /// static const String changeTheme = 'change_theme'; static const String changeLanguage = 'change_language'; - - - static const String noInternetConnection = 'no internet connection'; - static const String serverNotResponding = 'server not responding'; - static const String someThingWentWorng = 'something went wrong'; - static const String apiNotFound = 'api not found'; - static const String serverError = 'Server errro'; + static const String oops = "Oops!"; + static const String okay = "Okay"; + static const String refresh = "refresh!"; + static const String empty = "Empty or No data!"; }