Committed by
GitHub
Merge pull request #943 from CpdnCristiano/master
Another translation option with parameters
Showing
2 changed files
with
36 additions
and
0 deletions
| @@ -348,6 +348,27 @@ var products = []; | @@ -348,6 +348,27 @@ var products = []; | ||
| 348 | Text('singularKey'.trPlural('pluralKey', products.length, Args)); | 348 | Text('singularKey'.trPlural('pluralKey', products.length, Args)); |
| 349 | ``` | 349 | ``` |
| 350 | 350 | ||
| 351 | +#### Using translation with parameters | ||
| 352 | + | ||
| 353 | +```dart | ||
| 354 | +import 'package:get/get.dart'; | ||
| 355 | + | ||
| 356 | + | ||
| 357 | +Map<String, Map<String, String>> get keys => { | ||
| 358 | + 'en_US': { | ||
| 359 | + 'logged_in': 'logged in as @name with email @email', | ||
| 360 | + }, | ||
| 361 | + 'es_ES': { | ||
| 362 | + 'logged_in': 'iniciado sesión como @name con e-mail @email', | ||
| 363 | + } | ||
| 364 | +}; | ||
| 365 | + | ||
| 366 | +Text('logged_in'.trParams({ | ||
| 367 | + 'name': 'Jhon', | ||
| 368 | + 'email': 'jhon@example.com' | ||
| 369 | + })); | ||
| 370 | +``` | ||
| 371 | + | ||
| 351 | ### Locales | 372 | ### Locales |
| 352 | 373 | ||
| 353 | Pass parameters to `GetMaterialApp` to define the locale and translations. | 374 | Pass parameters to `GetMaterialApp` to define the locale and translations. |
| @@ -53,6 +53,21 @@ extension Trans on String { | @@ -53,6 +53,21 @@ extension Trans on String { | ||
| 53 | String trPlural([String pluralKey, int i, List<String> args = const []]) { | 53 | String trPlural([String pluralKey, int i, List<String> args = const []]) { |
| 54 | return i > 1 ? pluralKey.trArgs(args) : trArgs(args); | 54 | return i > 1 ? pluralKey.trArgs(args) : trArgs(args); |
| 55 | } | 55 | } |
| 56 | + | ||
| 57 | + String trParams([Map<String, String> params = const {}]) { | ||
| 58 | + var trans = tr; | ||
| 59 | + if (params.isNotEmpty) { | ||
| 60 | + params.forEach((key, value) { | ||
| 61 | + trans = trans.replaceAll('@$key', value); | ||
| 62 | + }); | ||
| 63 | + } | ||
| 64 | + return trans; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + String trPluralParams( | ||
| 68 | + [String pluralKey, int i, Map<String, String> params = const {}]) { | ||
| 69 | + return i > 1 ? pluralKey.trParams(params) : trParams(params); | ||
| 70 | + } | ||
| 56 | } | 71 | } |
| 57 | 72 | ||
| 58 | class _IntlHost { | 73 | class _IntlHost { |
-
Please register or login to post a comment