-
Notifications
You must be signed in to change notification settings - Fork 4
i18n
Efra Espada edited this page May 1, 2021
·
14 revisions
Stringcare can obfuscate your language files. Include any language you need:
en.json
{
"hello_there": "Hello there!",
"hello_format": "Hello $1!"
}
es_ES.json
{
"hello_there": "¿Qué tal?",
"hello_format": "Hola $1!"
}
root
|
|_ langs
| |_ # obfuscated lang files
|
|_ langs_base
| |_ en.json
| |_ es_ES.json
|
|_ lang_test
|_ # test revealed lang files
stringcare:
lang:
obfuscated: "lang" # obfuscated langs files directory
original: "lang_base" # original langs files directory
test: "lang_test" # test reveal langs files directory (only needed for testing)
resources:
class_name: "R" # R class for accessing resources
class_dir: "lib" # R class directory
flutter:
assets:
- lang/en.json
- lang/es_ES.json
Run this command to generate the obfuscated lang files:
flutter pub run stringcare:obfuscate
Once the obfuscated lang files are generated, you can test the reveal:
flutter pub run stringcare:reveal
Include the supported languages:
void main() {
Stringcare.locales = [
Locale('en', ''),
Locale('es', ''),
Locale('es', 'AR'),
Locale('es', 'ES'),
Locale('es', 'US')
];
runApp(MyApp());
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
supportedLocales: Stringcare.locales,
localizationsDelegates: Stringcare.delegates,
localeResolutionCallback: Stringcare.localeResolutionCallback,
home: Scaffold(...)
);
}
Retrieve any key with:
Stringcare.translate(context, R.strings.hello_there);
With extensions:
R.strings.hello_there.on(context);
Retrieve any key with format:
Stringcare.translate(context, R.strings.hello_format, values: ["Tom"])
With extensions:
R.strings.hello_format.on(context, values: ["Tom"])
If you are working without BuildContext
or you want to get a specific language, you can use:
await Stringcare.translateWithLang("en", R.strings.hello_there);
await Stringcare.translateWithLang("es_ES", R.strings.hello_there);
With extensions:
await R.strings.hello_there.getLang("en");
await R.strings.hello_there.getLang("es_ES");