From 92405a2309f53d5f03cd9d05e2ce60d90672dd61 Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 8 Jul 2022 13:51:57 +0700 Subject: [PATCH] TF-297 Add LocalizationService to handle language translations --- .../localizations/localization_service.dart | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/main/localizations/localization_service.dart diff --git a/lib/main/localizations/localization_service.dart b/lib/main/localizations/localization_service.dart new file mode 100644 index 000000000..5034ec991 --- /dev/null +++ b/lib/main/localizations/localization_service.dart @@ -0,0 +1,43 @@ + +import 'dart:ui'; + +import 'package:get/get.dart'; + +class LocalizationService extends Translations { + + static const defaultLocale = Locale('en', 'US'); + static const fallbackLocale = Locale('en', 'US'); + + static final supportedLanguageCodes = [ + 'fr', + 'en', + 'vi', + 'ru' + ]; + + static const List supportedLocales = [ + Locale('fr', 'FR'), + Locale('en', 'US'), + Locale('vi', 'VN'), + Locale('ru', 'RU') + ]; + + static final locale = _getLocaleFromLanguage(); + + static void changeLocale(String langCode) { + final locale = _getLocaleFromLanguage(langCode: langCode); + Get.updateLocale(locale); + } + + static Locale _getLocaleFromLanguage({String? langCode}) { + final lang = langCode ?? Get.deviceLocale?.languageCode; + + final localeSelected = supportedLocales + .firstWhereOrNull((locale) => locale.languageCode == lang); + + return localeSelected ?? Get.locale ?? defaultLocale; + } + + @override + Map> get keys => {}; +} \ No newline at end of file