slang
slang copied to clipboard
Dynamic Translations (e.g. via network)
Motivation To reduce asset size, developers can extract the translations to the server. The translations will be fetched at runtime.
Developer Experience Because every key needs to be known at compile time, the base translations must be available.
// strings.i18n.json
{
"greet": "Welcome {name}"
}
Now load a locale:
// your logic, represents ONE locale!
Map fetchFromServer() {
return {
'greet': 'Hello {name}'
}
}
// via LocaleSettings
void example1() {
String a = t.greet(name: 'Tom'); // "Welcome Tom"
Map data = fetchFromServer();
LocaleSettings.loadCustom(data); // the new load function
String b = t.greet(name: 'Tom'); // "Hello Tom"
}
// via dependency injection
void example2() {
String a = t.greet(name: 'Tom'); // "Welcome Tom"
Map data = fetchFromServer();
final t2 = AppLocaleUtils.buildCustom(data); // the new build function
String b = t2.greet(name: 'Tom'); // "Hello Tom"
}
This will blow up the generated file. Therefore, this should be opt-in.
targets:
$default:
builders:
fast_i18n:
options:
contexts:
dynamic_translations: true # enable this feature
When can we expect this to land @Tienisto ?
Currently, I have no plans to implement this feature because there are still lots of open questions.
This library generates the locale enum - should all locales be specified and how? The base translations have to be included beforehand which is annoying.
Why do you want this feature? Only to reduce asset size?
Yes, reducing asset size and the ability to change strings remotely.
Released in v3.0.0-dev.0
.