I18n
Purpose¶
A utility to manage i18n.
Note
The default locale is EN
Methods¶
Binding name: p6.i18n
getText¶
Reads the locale pipeline entry and searches for the corresponding text in the map.
Syntax
String p6.i18n.getText(Map texts)
Note
If no entry is found, or the pipeline variable is empty, the default locale EN is used.
Note
Since 6.9.9 (cf. i18n) The string retrieved will be interpolated to inject any translation
Example
def label = [
EN: 'English',
FR: 'French'
]
println p6.i18n.getText(label)
getText (with locale)¶
Returns the corresponding text in the map matching the given locale.
Syntax
String p6.i18n.getText(Map texts, String locale)
Note
Since 6.9.9 (cf. i18n) The string retrieved will be interpolated to inject any translation
Note
If no entry is found, the default locale EN is used.
Example
def label = [
EN: 'English',
FR: 'French'
]
println p6.i18n.getText(label, 'FR')
// Output: French
println p6.i18n.getText(label, 'BE')
// Output: English
translate¶
Since 6.9.9 (cf. i18n)
Reads the locale pipeline entry and returns the corresponding translation.
Syntax
String p6.i18n.translate(String text)
String p6.i18n.translate(String text [, Map<String, Object> params])
Note
If no entry is found, or the pipeline variable is empty, the default locale EN is used.
The parameters are optional and can be used to interpolate the translation. It’s a shortcut to
p6.i18n.format(p6.i18n.translate(text), params)
Example
println p6.i18n.translate('text_unique_key')
println p6.i18n.translate('text_unique_key', [ 'param1': 'value1' ])
translate (with locale)¶
Since 6.9.9 (cf. i18n)
Returns the translated text matching the given locale.
Syntax
String p6.i18n.translate(String text, String locale)
String p6.i18n.translate(String text, String locale [, Map<String, Object> params])
Note
If no entry is found, the default locale EN is used.
The parameters are optional and can be used to interpolate the translation. It’s a shortcut to
p6.i18n.format(p6.i18n.translate(text, locale), locale, params)
Example
println p6.i18n.translate('text_unique_key', 'FR')
println p6.i18n.translate('text_unique_key', 'FR', [ 'param1': 'value1' ])
clearCache¶
Since 6.9.9 (cf. i18n)
Clear the translation cache. Next use of the translation will reload the file resources
Syntax
String p6.i18n.clearCache()
Example
println p6.i18n.clearCache()
format¶
Since 6.10.7
Clear the translation cache. Next use of the translation will reload the file resources
Syntax
String p6.i18n.format(String text, [String locale, ] Map<String, Object> params)
Note
You can specify the locale as the second parameter. If not, the default locale EN is used.
Example
println p6.i18n.format('{language} is {what}', [ 'language': 'Groovy', 'what': 'awesome' ])
println p6.i18n.format('{language} is {what}', 'FR', [ 'language': 'Groovy', 'what': 'awesome' ])
Patterns¶
Warning
The parameters must be named in the pattern. Indexes are not supported (i.e. {0} items).
The format method uses an advanced pattern to interpolate the translation. The pattern is based on the MessageFormat class.
Here are some examples:
{name}: The parameter namedname{amount, number, currency}: The parameter namedamountformatted as a currency{percent, number, percent}: The parameter namedpercentformatted as a percentage{when, date, short}: The parameter namedwhenformatted as a short date{when, time, short}: The parameter namedwhenformatted as a short time{when, date, short} {when, time, short}: The parameter namedwhenformatted as a short date and time{number, number, integer}: The parameter namednumberformatted as an integer{number, number, #,##0.00}: The parameter namednumberformatted as a number with 2 decimals and a comma as a thousand separator{gender, choice, 0#female|1#male}: The parameter namedgenderis used to select the correct translation{gender, select, male {He} female {She} other {They}}: The parameter namedgenderis used to select the correct translation{count, plural, =0{no files} =1{one file} other{# files}}: The parameter namedcountis used to select the correct translation
You can also use a combination of these patterns:
{gender, select,
male {He has {count, plural, one {one item} other {# items}}.}
female {She has {count, plural, one {one item} other {# items}}.}
other {They have {count, plural, one {one item} other {# items}}.}}
Referer to the ICU Documentation for more information.