easy_localization have fallback values for keys on other languages

Viewed 423

When working with this package for localization, and base translation done in EN, I want my 2nd language to show keys if they are present in 2nd language json file, but if they are not, to consume en.json equivalent value. Issue is that by default this package shows key that doesn't exist in JSON file, instead of fallback translation file value. Is there a way to override this?

Usage of the plugin in main.dart file

runApp(
  EasyLocalization(
    child: MyApp(),
    useOnlyLangCode: true,
    fallbackLocale: Locale('en'),
    supportedLocales: [
      Locale('en'),
      Locale('es'),
    ],
    path: 'lang',
  ),
);

And when I want to translate a key that exists in en.json, but doesn't exist in es.json file it looks like this:

tr('appTitle');

Expected result would be "Hello world", but I get "appTitle" on the screen.

1 Answers

Old question, but since I just ran into the same problem:

There is now(?) a parameter "useFallbackTranslations" that you have to set to true. Indeed a bit counterintuitive, because one would assume that setting fallbackLocale should be enough.

EasyLocalization(
fallbackLocale: const Locale('en'),
supportedLocales: const [
  Locale('en'),
  Locale('es'),
  Locale('de'),
],
useOnlyLangCode: true,
useFallbackTranslations: true, // <------
path: 'assets/i18n',
child: const MyApp(),
))
Related