diff --git a/README.md b/README.md index 6337b30de..dab84faa1 100644 --- a/README.md +++ b/README.md @@ -423,6 +423,50 @@ return GetMaterialApp( ); ``` +#### Optimum structure for language maps + +In order to reduce the repition of language keys when using variants of a language you can place all the keys in a language only key-value dictionary map and then add only the keys that need to be changed to the language variant. + +```dart +import 'package:get/get.dart'; + +class Messages extends Translations { + @override + Map> get keys => { + 'en': { + 'hello': 'Hello World', + 'netAmount': 'Net amount', + 'tax': 'Tax' + }, + 'en_GB': { + 'tax': 'VAT' + }, + 'en_US': { + 'tax': 'Sales tax' + }, + 'de_DE': { + 'hello': 'Hallo Welt', + 'netAmount': 'Nettobetrag' + 'tax': 'Steuer' + } + }; +} +``` + +Using the code above the translations for the keys when using different English language variants is as follows: + +| Locale | Key | Translation | Dictionary map used | +| ------ | --------- | ----------- | ------------------- | +| en_GB | hello | Hello World | en | +| | netAmount | Net amount | en | +| | tax | VAT | en_GB | +| en_US | hello | Hello World | en | +| | netAmount | Net amount | en | +| | tax | Sales tax | en_US | +| en_AU | hello | Hello World | en | +| | netAmount | Net amount | en | +| | tax | Tax | en | + ## Change Theme Please do not use any higher level widget than `GetMaterialApp` in order to update it. This can trigger duplicate keys. A lot of people are used to the prehistoric approach of creating a "ThemeProvider" widget just to change the theme of your app, and this is definitely NOT necessary with **GetX™**. diff --git a/lib/get_utils/src/extensions/internacionalization.dart b/lib/get_utils/src/extensions/internacionalization.dart index e0155dd7c..62fa988d1 100644 --- a/lib/get_utils/src/extensions/internacionalization.dart +++ b/lib/get_utils/src/extensions/internacionalization.dart @@ -66,6 +66,9 @@ extension Trans on String { // Checks if there is a callback language in the absence of the specific // country, and if it contains that key. Map? get _getSimilarLanguageTranslation { + if(Get.translations.containsKey(Get.locale!.languageCode.split("_").first)){ + return Get.translations[Get.locale!.languageCode.split("_").first]; + } final translationsWithNoCountry = Get.translations .map((key, value) => MapEntry(key.split("_").first, value)); final containsKey = translationsWithNoCountry diff --git a/test/internationalization/internationalization_test.dart b/test/internationalization/internationalization_test.dart index 19b1d14f5..ba7e43546 100644 --- a/test/internationalization/internationalization_test.dart +++ b/test/internationalization/internationalization_test.dart @@ -12,7 +12,7 @@ void main() { await tester.pumpAndSettle(); - expect('covid'.tr, 'Corona Virus'); + expect('covid'.tr, 'Corona Virus (US totals)'); expect('total_confirmed'.tr, 'Total Confirmed'); expect('total_deaths'.tr, 'Total Deaths'); @@ -31,5 +31,21 @@ void main() { expect('covid'.tr, 'Corona Virus'); expect('total_confirmed'.tr, 'Total Confirmed'); expect('total_deaths'.tr, 'Total Deaths'); + + Get.updateLocale(const Locale('en', 'GB')); + + await tester.pumpAndSettle(); + + expect('covid'.tr, 'Corona Virus (UK totals)'); + expect('total_confirmed'.tr, 'Total Confirmed'); + expect('total_deaths'.tr, 'Total Deaths'); + + Get.updateLocale(const Locale('en', 'AU')); + + await tester.pumpAndSettle(); + + expect('covid'.tr, 'Corona Virus'); + expect('total_confirmed'.tr, 'Total Confirmed'); + expect('total_deaths'.tr, 'Total Deaths'); }); } diff --git a/test/navigation/utils/wrapper.dart b/test/navigation/utils/wrapper.dart index 20a7e8053..b6efaf939 100644 --- a/test/navigation/utils/wrapper.dart +++ b/test/navigation/utils/wrapper.dart @@ -61,11 +61,17 @@ class WrapperTranslations extends Translations { static Locale? get locale => const Locale('en', 'US'); @override Map> get keys => { - 'en_US': { + 'en': { 'covid': 'Corona Virus', 'total_confirmed': 'Total Confirmed', 'total_deaths': 'Total Deaths', }, + 'en_US': { + 'covid': 'Corona Virus (US totals)', + }, + 'en_GB': { + 'covid': 'Corona Virus (UK totals)', + }, 'pt_BR': { 'covid': 'Corona Vírus', 'total_confirmed': 'Total confirmado',