localizeArgs function
The localizeArgs function applies interpolations on text
with the given params, p1
, p2
, p3
, ..., p15
.
1. Interpolation with named placeholders
Your translations file may contain interpolations:
static var _t = Translations.byText('en-US') +
{
'en-US': 'Hello {student} and {teacher}',
'pt-BR': 'Olá {student} e {teacher}',
};
String get i18n => localize(this, _t);
Then use the args
function:
print('Hello {student} and {teacher}'.i18n
.args({'student': 'John', 'teacher': 'Mary'}));
The above code will print Hello John and Mary
if the locale is English,
or Olá John e Mary
if it's Portuguese. This interpolation method allows for the
translated string to change the order of the parameters.
2. Interpolation with numbered placeholders
static var _t = Translations.byText('en-US') +
{
'en-US': 'Hello {1} and {2}',
'pt-BR': 'Olá {1} e {2}',
};
String get i18n => localize(this, _t);
Then use the args
function:
print('Hello {1} and {2}'.i18n
.args({1: 'John', 2: 'Mary'}));
The above code will print Hello John and Mary
if the locale is English,
or Olá John e Mary
if it's Portuguese. This interpolation method allows for the
translated string to change the order of the parameters.
3. Interpolation with unnamed placeholders
static var _t = Translations.byText('en-US') +
{
'en-US': 'Hello {} and {}',
'pt-BR': 'Olá {} e {}',
};
String get i18n => localize(this, _t);
Then use the args
function:
print('Hello {} and {}'.i18n.args('John', 'Mary'));
print('Hello {} and {}'.i18n.args(['John', 'Mary'])); // Also works
The above code will replace the {}
in order,
and print Hello John and Mary
if the locale is English,
or Olá John e Mary
if it's Portuguese.
The problem with this interpolation method is that it doesn’t allow for the translated string to change the order of the parameters.
Implementation
String localizeArgs(Object? text, Object p1,
[Object? p2,
Object? p3,
Object? p4,
Object? p5,
Object? p6,
Object? p7,
Object? p8,
Object? p9,
Object? p10,
Object? p11,
Object? p12,
Object? p13,
Object? p14,
Object? p15]) =>
core.localizeArgs(
text, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);