How to use easy_localization in a dependent flutter package?

Viewed 402

I am using easy_localization in my app and all is working fine. However, one of the packages my app depends on (internal package) has also translations and I would like to use easy_translation in this package. How can I configure my main app and package so they both have their own translations?

Here is the configuration of my main app:

  EasyLocalization(
    supportedLocales: AppConfig.SUPPORTED_LANG.split(',').map((e) => Locale.fromSubtags(languageCode: e)).toList(),
    path: 'lib/resources/assets/translations',
    useOnlyLangCode: true,
    assetLoader: CodegenLoader(),
    startLocale: Locale(Intl.getCurrentLocale()),
    fallbackLocale: Locale.fromSubtags(languageCode: AppConfig.DEFAULT_LANG),
    child: MyApp(),
  ),

All the translations of my main app are inside the CodegenLoader class. I would like to do the same for my package.

Regards

1 Answers

Right now, I think this is not possible. But there is a workaround that might help some people:

  1. Add easy_localization to the pubspec.yaml of your package
  2. Use the tr() function as you normally would inside the package code
  3. Add the translation keys you are using in the package to the the translation file of your main app
    • Example: If you're using the translation key welcome in your package, add "welcome": "Welcome to the App!" to the en.json of your main app

Of course, this is hacky and it does not work if the main app is not using easy_localization but maybe it until a better solution is found.

Related