Skip to content

Commit

Permalink
localaization in eceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
MOKTADIR authored and MOKTADIR committed Mar 6, 2023
1 parent 78c2739 commit ebfd07b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 105 deletions.
89 changes: 44 additions & 45 deletions lib/app/components/empty_widget.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
),
),
],
);
}
}
24 changes: 14 additions & 10 deletions lib/app/service/api_exceptions.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
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;
}
}

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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/app/service/base_controller.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,7 +13,7 @@ class BaseController {

var errorText = DioExceptions.fromDioError(error).toString();

showErrorDialog("Oops!", errorText);
showErrorDialog(Strings.oops.tr, errorText);

if (kDebugMode) {
print(errorText);
Expand Down
3 changes: 2 additions & 1 deletion lib/app/service/helper/dialog_helper.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -37,7 +38,7 @@ class DialogHelper {
onPressed: () {
if (Get.isDialogOpen!) Get.back();
},
child: const Text('Okay'),
child: Text(Strings.okay.tr),
),
],
),
Expand Down
30 changes: 18 additions & 12 deletions lib/config/translations/ar_AR/ar_ar_translation.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import '../strings_enum.dart';

final Map<String, String> arAR =
{
Strings.hello : 'مرحباً!',
Strings.loading : 'جاري التحميل',
final Map<String, String> 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: "فارغة أو لا توجد بيانات!",
};
28 changes: 17 additions & 11 deletions lib/config/translations/en_US/en_us_translation.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import '../strings_enum.dart';

const Map<String, String> 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!",
};
24 changes: 11 additions & 13 deletions lib/config/translations/localization_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@ class LocalizationService extends Translations {
static Locale defaultLanguage = supportedLanguages['en']!;

// supported languages
static Map<String,Locale> supportedLanguages = {
'en' : const Locale('en', 'US'),
'ar' : const Locale('ar', 'AR'),
static Map<String, Locale> 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<String,TextStyle> supportedLanguagesFontsFamilies = {
'en' : const TextStyle(fontFamily: 'Poppins'),
static Map<String, TextStyle> supportedLanguagesFontsFamilies = {
'en': const TextStyle(fontFamily: 'Poppins'),
'ar': const TextStyle(fontFamily: 'Cairo'),
};

@override
Map<String, Map<String, String>> 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]!);
Expand All @@ -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();
}

25 changes: 15 additions & 10 deletions lib/config/translations/strings_enum.dart
Original file line number Diff line number Diff line change
@@ -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!";
}

0 comments on commit ebfd07b

Please sign in to comment.