Committed by
GitHub
Merge pull request #544 from kai-oswald/patch-2
Create internationalization.md
Showing
2 changed files
with
106 additions
and
0 deletions
@@ -29,6 +29,7 @@ | @@ -29,6 +29,7 @@ | ||
29 | - [More details about dependency management](#more-details-about-dependency-management) | 29 | - [More details about dependency management](#more-details-about-dependency-management) |
30 | - [How to contribute](#how-to-contribute) | 30 | - [How to contribute](#how-to-contribute) |
31 | - [Utils](#utils) | 31 | - [Utils](#utils) |
32 | + - [Internationalization](#internationalization) | ||
32 | - [Change Theme](#change-theme) | 33 | - [Change Theme](#change-theme) |
33 | - [Other Advanced APIs](#other-advanced-apis) | 34 | - [Other Advanced APIs](#other-advanced-apis) |
34 | - [Optional Global Settings and Manual configurations](#optional-global-settings-and-manual-configurations) | 35 | - [Optional Global Settings and Manual configurations](#optional-global-settings-and-manual-configurations) |
@@ -296,7 +297,61 @@ Text(controller.textFromApi); | @@ -296,7 +297,61 @@ Text(controller.textFromApi); | ||
296 | Any contribution is welcome! | 297 | Any contribution is welcome! |
297 | 298 | ||
298 | # Utils | 299 | # Utils |
300 | +## Internationalization | ||
301 | +### Translations | ||
302 | +Translations are kept as a simple key-value dictionary map. | ||
303 | +To add custom translations, create a class and extend `Translations`. | ||
304 | +```dart | ||
305 | +import 'package:get/get.dart'; | ||
306 | + | ||
307 | +class Messages extends Translations { | ||
308 | + @override | ||
309 | + Map<String, Map<String, String>> get keys => { | ||
310 | + 'en_US': { | ||
311 | + 'hello': 'Hello World', | ||
312 | + }, | ||
313 | + 'de_DE': { | ||
314 | + 'hello': 'Hallo Welt', | ||
315 | + } | ||
316 | + }; | ||
317 | +} | ||
318 | +``` | ||
319 | + | ||
320 | +#### Using translations | ||
321 | +Just append `.tr` to the specified key and it will be translated, using the current value of `Get.locale` and `Get.fallbackLocale`. | ||
322 | +```dart | ||
323 | +Text('title'.tr); | ||
324 | +``` | ||
325 | + | ||
326 | +### Locales | ||
327 | +Pass parameters to `GetMaterialApp` to define the locale and translations. | ||
328 | + | ||
329 | +```dart | ||
330 | +return GetMaterialApp( | ||
331 | + translations: Messages(), // your translations | ||
332 | + locale: Locale('en_US'), // translations will be displayed in that locale | ||
333 | + fallbackLocale: Locale('en_US'), // specify the fallback locale in case an invalid locale is selected. | ||
334 | + supportedLocales: <Locale>[Locale('en_US'), Locale('de_DE')] // specify the supported locales | ||
335 | +); | ||
336 | +``` | ||
337 | + | ||
338 | +#### Change locale | ||
339 | +Call `Get.updateLocale(locale)` to update the locale. Translations then automatically use the new locale. | ||
340 | +```dart | ||
341 | +var locale = Locale('en_US'); | ||
342 | +Get.updateLocale(locale); | ||
343 | +``` | ||
344 | + | ||
345 | +#### System locale | ||
346 | +To read the system locale, you could use `Platform.localeName`. | ||
347 | +```dart | ||
348 | +return GetMaterialApp( | ||
349 | + locale: Locale(Platform.localeName), | ||
350 | +); | ||
351 | +``` | ||
299 | 352 | ||
353 | +### More details about internationalization | ||
354 | +**See a more in-depth explanation of internationalization [here](./docs/en_US/internationalization.md)** | ||
300 | ## Change Theme | 355 | ## Change Theme |
301 | 356 | ||
302 | 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. | 357 | 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. |
docs/en_US/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