Skip to content
Efra Espada edited this page Jan 20, 2024 · 14 revisions

Stringcare can obfuscate your language files.

Project Configuration

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:

dart run stringcare:obfuscate 

Once the obfuscated lang files are generated, you can test the reveal:

dart run stringcare:reveal

Locales

Include any Locale you need.

If you define the country code, the language file will only act if it matches the device's locale country code.

es_ES.json

{
  "hello_there": "¿Qué tal?",
  "hello_format": "Hola $1!"
}

For the rest of spanish languages with different country codes, you can simply define the language code:

es.json

{
  "hello_there": "¿Qué pasa?",
  "hello_format": "¡Hola $1!"
}

I18n Configuration

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(
    navigatorKey: Stringcare().navigatorKey, // optional, enables the string() method
    supportedLocales: Stringcare().locales,
    localizationsDelegates: Stringcare().delegates,
    localeResolutionCallback: Stringcare().localeResolutionCallback,
    home: Scaffold(...)
  );
}

Usage

Retrieve any key with:

Stringcare().translate(context, R.strings.hello_there);

With extensions:

R.strings.hello_there.on(context);

If you set the navigatorKey in the MaterialApp you can use:

R.strings.hello_there.string();

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 set the navigatorKey in the MaterialApp you can use:

R.strings.hello_format.string(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");