Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, String>> 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™**.
Expand Down
3 changes: 3 additions & 0 deletions lib/get_utils/src/extensions/internacionalization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>? 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
Expand Down
18 changes: 17 additions & 1 deletion test/internationalization/internationalization_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
}
8 changes: 7 additions & 1 deletion test/navigation/utils/wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ class WrapperTranslations extends Translations {
static Locale? get locale => const Locale('en', 'US');
@override
Map<String, Map<String, String>> 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',
Expand Down