slang icon indicating copy to clipboard operation
slang copied to clipboard

Dynamic Translations (e.g. via network)

Open Tienisto opened this issue 3 years ago • 3 comments

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

Tienisto avatar Feb 05 '22 00:02 Tienisto

When can we expect this to land @Tienisto ?

erabti avatar Mar 21 '22 15:03 erabti

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?

Tienisto avatar Mar 21 '22 18:03 Tienisto

Yes, reducing asset size and the ability to change strings remotely.

erabti avatar Mar 22 '22 10:03 erabti

Released in v3.0.0-dev.0.

Tienisto avatar Sep 20 '22 00:09 Tienisto