Jonny Borges
Committed by GitHub

Merge pull request #438 from lundin/master

fallbackLocale for translation (as GetMaterialApp config)
@@ -33,6 +33,8 @@ abstract class GetInterface { @@ -33,6 +33,8 @@ abstract class GetInterface {
33 GetMaterialController getxController = GetMaterialController(); 33 GetMaterialController getxController = GetMaterialController();
34 34
35 Locale locale; 35 Locale locale;
  36 +
  37 + Locale fallbackLocale;
36 38
37 GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>(); 39 GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
38 40
@@ -30,6 +30,7 @@ class GetMaterialApp extends StatelessWidget { @@ -30,6 +30,7 @@ class GetMaterialApp extends StatelessWidget {
30 this.darkTheme, 30 this.darkTheme,
31 this.themeMode = ThemeMode.system, 31 this.themeMode = ThemeMode.system,
32 this.locale, 32 this.locale,
  33 + this.fallbackLocale,
33 this.localizationsDelegates, 34 this.localizationsDelegates,
34 this.localeListResolutionCallback, 35 this.localeListResolutionCallback,
35 this.localeResolutionCallback, 36 this.localeResolutionCallback,
@@ -83,6 +84,7 @@ class GetMaterialApp extends StatelessWidget { @@ -83,6 +84,7 @@ class GetMaterialApp extends StatelessWidget {
83 final Map<String, Map<String, String>> translationsKeys; 84 final Map<String, Map<String, String>> translationsKeys;
84 final Translations translations; 85 final Translations translations;
85 final Locale locale; 86 final Locale locale;
  87 + final Locale fallbackLocale;
86 final Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates; 88 final Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates;
87 final LocaleListResolutionCallback localeListResolutionCallback; 89 final LocaleListResolutionCallback localeListResolutionCallback;
88 final LocaleResolutionCallback localeResolutionCallback; 90 final LocaleResolutionCallback localeResolutionCallback;
@@ -186,6 +188,10 @@ class GetMaterialApp extends StatelessWidget { @@ -186,6 +188,10 @@ class GetMaterialApp extends StatelessWidget {
186 Get.locale = locale; 188 Get.locale = locale;
187 } 189 }
188 190
  191 + if (fallbackLocale != null) {
  192 + Get.fallbackLocale = fallbackLocale;
  193 + }
  194 +
189 if (translations != null) { 195 if (translations != null) {
190 Get.translations = translations.keys; 196 Get.translations = translations.keys;
191 } else if (translationsKeys != null) { 197 } else if (translationsKeys != null) {
@@ -275,6 +281,16 @@ extension Trans on String { @@ -275,6 +281,16 @@ extension Trans on String {
275 Get.translations[Get.locale.languageCode].containsKey(this)) { 281 Get.translations[Get.locale.languageCode].containsKey(this)) {
276 return Get.translations[Get.locale.languageCode][this]; 282 return Get.translations[Get.locale.languageCode][this];
277 // If there is no corresponding language or corresponding key, return the key. 283 // If there is no corresponding language or corresponding key, return the key.
  284 + } else if (Get.fallbackLocale != null && Get.translations.containsKey(
  285 + "${Get.fallbackLocale.languageCode}_${Get.fallbackLocale.countryCode}") &&
  286 + Get.translations[
  287 + "${Get.fallbackLocale.languageCode}_${Get.fallbackLocale.countryCode}"]
  288 + .containsKey(this)) {
  289 + return Get.translations[
  290 + "${Get.fallbackLocale.languageCode}_${Get.fallbackLocale.countryCode}"]
  291 + [this];
  292 +
  293 + // Checks if there is a callback language in the absence of the specific country, and if it contains that key.
278 } else { 294 } else {
279 return this; 295 return this;
280 } 296 }