Jonny Borges
Committed by GitHub

Merge pull request #544 from kai-oswald/patch-2

Create internationalization.md
... ... @@ -29,6 +29,7 @@
- [More details about dependency management](#more-details-about-dependency-management)
- [How to contribute](#how-to-contribute)
- [Utils](#utils)
- [Internationalization](#internationalization)
- [Change Theme](#change-theme)
- [Other Advanced APIs](#other-advanced-apis)
- [Optional Global Settings and Manual configurations](#optional-global-settings-and-manual-configurations)
... ... @@ -296,7 +297,61 @@ Text(controller.textFromApi);
Any contribution is welcome!
# Utils
## Internationalization
### Translations
Translations are kept as a simple key-value dictionary map.
To add custom translations, create a class and extend `Translations`.
```dart
import 'package:get/get.dart';
class Messages extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en_US': {
'hello': 'Hello World',
},
'de_DE': {
'hello': 'Hallo Welt',
}
};
}
```
#### Using translations
Just append `.tr` to the specified key and it will be translated, using the current value of `Get.locale` and `Get.fallbackLocale`.
```dart
Text('title'.tr);
```
### Locales
Pass parameters to `GetMaterialApp` to define the locale and translations.
```dart
return GetMaterialApp(
translations: Messages(), // your translations
locale: Locale('en_US'), // translations will be displayed in that locale
fallbackLocale: Locale('en_US'), // specify the fallback locale in case an invalid locale is selected.
supportedLocales: <Locale>[Locale('en_US'), Locale('de_DE')] // specify the supported locales
);
```
#### Change locale
Call `Get.updateLocale(locale)` to update the locale. Translations then automatically use the new locale.
```dart
var locale = Locale('en_US');
Get.updateLocale(locale);
```
#### System locale
To read the system locale, you could use `Platform.localeName`.
```dart
return GetMaterialApp(
locale: Locale(Platform.localeName),
);
```
### More details about internationalization
**See a more in-depth explanation of internationalization [here](./docs/en_US/internationalization.md)**
## 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 Get.
... ...
## Translations
Translations are kept as a simple key-value dictionary map.
To add custom translations, create a class and extend `Translations`.
```dart
import 'package:get/get.dart';
class Messages extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en_US': {
'hello': 'Hello World',
},
'de_DE': {
'hello': 'Hallo Welt',
}
};
}
```
### Using translations
Just append `.tr` to the specified key and it will be translated, using the current value of `Get.locale` and `Get.fallbackLocale`.
```dart
Text('title'.tr);
```
## Locales
Pass parameters to `GetMaterialApp` to define the locale and translations.
```dart
return GetMaterialApp(
translations: Messages(), // your translations
locale: Locale('en_US'), // translations will be displayed in that locale
fallbackLocale: Locale('en_US'), // specify the fallback locale in case an invalid locale is selected.
supportedLocales: <Locale>[Locale('en_US'), Locale('de_DE')] // specify the supported locales
);
```
### Change locale
Call `Get.updateLocale(locale)` to update the locale. Translations then automatically use the new locale.
```dart
var locale = Locale('en_US');
Get.updateLocale(locale);
```
### System locale
To read the system locale, you could use `Platform.localeName`.
```dart
return GetMaterialApp(
locale: Locale(Platform.localeName),
);
```
... ...