nest function
Replaces occurrences of nested key-values in string
for other
key-values. Essentially calls I18Next.translate
with the nested value.
E.g.:
{
key1: "Hello $t(key2)!"
key2: "World"
}
i18Next.t('key1') // "Hello World!"
Implementation
String nest(
Locale locale,
String string,
Translate translate,
Map<String, dynamic> variables,
I18NextOptions options,
) {
final pattern = nestingPattern(options);
return string.splitMapJoin(pattern, onMatch: (match) {
match = match as RegExpMatch;
final key = match.namedGroup('key');
if (key == null || key.isEmpty) {
throw NestingException('Key not found', match);
}
var newVariables = variables;
final varsString = match.namedGroup('variables');
if (varsString != null && varsString.isNotEmpty) {
final Map<String, dynamic> decoded = jsonDecode(varsString);
newVariables = Map<String, dynamic>.of(variables)..addAll(decoded);
}
final value = translate(key, locale, newVariables, options);
if (value == null) {
throw NestingException('Translation not found', match);
}
return value;
});
}