Committed by
GitHub
Create internationalization.md
This is a very basic documentation to getting started with internationalization with getx.
Showing
1 changed file
with
51 additions
and
0 deletions
docs/internationalization.md
0 → 100644
| 1 | +## Translations | ||
| 2 | +Translations are kept as a simple key-value dictionary map. | ||
| 3 | +To add custom translations, create a class and extend `Translations`. | ||
| 4 | +```dart | ||
| 5 | +import 'package:get/get.dart'; | ||
| 6 | + | ||
| 7 | +class Messages extends Translations { | ||
| 8 | + @override | ||
| 9 | + Map<String, Map<String, String>> get keys => { | ||
| 10 | + 'en_US': { | ||
| 11 | + 'hello': 'Hello World', | ||
| 12 | + }, | ||
| 13 | + 'de_DE': { | ||
| 14 | + 'hello': 'Hallo Welt', | ||
| 15 | + } | ||
| 16 | + }; | ||
| 17 | +} | ||
| 18 | +``` | ||
| 19 | + | ||
| 20 | +### Using translations | ||
| 21 | +Just append `.tr` to the specified key and it will be translated, using the current value of `Get.locale` and `Get.fallbackLocale`. | ||
| 22 | +```dart | ||
| 23 | +Text('title'.tr); | ||
| 24 | +``` | ||
| 25 | + | ||
| 26 | +## Locales | ||
| 27 | +Pass parameters to `GetMaterialApp` to define the locale and translations. | ||
| 28 | + | ||
| 29 | +```dart | ||
| 30 | +return GetMaterialApp( | ||
| 31 | + translations: Messages(), // your translations | ||
| 32 | + locale: Locale('en_US'), // translations will be displayed in that locale | ||
| 33 | + fallbackLocale: Locale('en_US'), // specify the fallback locale in case an invalid locale is selected. | ||
| 34 | + supportedLocales: <Locale>[Locale('en_US'), Locale('de_DE')] // specify the supported locales | ||
| 35 | +); | ||
| 36 | +``` | ||
| 37 | + | ||
| 38 | +### Change locale | ||
| 39 | +Call `Get.updateLocale(locale)` to update the locale. Translations then automatically use the new locale. | ||
| 40 | +```dart | ||
| 41 | +var locale = Locale('en_US'); | ||
| 42 | +Get.updateLocale(locale); | ||
| 43 | +``` | ||
| 44 | + | ||
| 45 | +### System locale | ||
| 46 | +To read the system locale, you could use `Platform.localeName`. | ||
| 47 | +```dart | ||
| 48 | +return GetMaterialApp( | ||
| 49 | + locale: Locale(Platform.localeName), | ||
| 50 | +); | ||
| 51 | +``` |
-
Please register or login to post a comment