ilteoood/flutter_i18n

Issue loading on Android

rsaldev opened this issue · 7 comments

When I use android the text shows the keys and not the values, it works on IOs and Web.

Is really hard to find something with this level of detail.

The library shows the keys when it can't load the translation resource.

Can you reproduce the issue with the example provided in this repo?

I tried it with the example, when runeed on IOs and Web, the packages picks up the Json file, but on android it says [flutter_i18n DEBUG]: Unable to load JSON file for es

Today, i catched thats error too. An Android my localizations not seen, only keys i saw. But i found how i can fix it.
We need to add assets/ for basePath at start...
Bcs on web, with prefix, its not working...

For Web my working code

FlutterI18nDelegate(
          translationLoader: NamespaceFileTranslationLoader(
            basePath: 'i18n',
            useCountryCode: false,
            decodeStrategies: [JsonDecodeStrategy()],
            namespaces: locale_namepsaces,
          ),
          missingTranslationHandler: (key, locale) {
            print(
                "--- Missing Key: $key, languageCode: ${locale?.languageCode}");
          },
        ),

But for Android

FlutterI18nDelegate(
          translationLoader: NamespaceFileTranslationLoader(
            basePath: 'assets/i18n',
            useCountryCode: false,
            decodeStrategies: [JsonDecodeStrategy()],
            namespaces: locale_namepsaces,
          ),
          missingTranslationHandler: (key, locale) {
            print(
                "--- Missing Key: $key, languageCode: ${locale?.languageCode}");
          },
        ),

P.S. At now, my web App working with basePath: 'assets/i18n'

I catch error on HotReload on web, when i added a new spacename to my list:
Error while trying to load an asset: Failed to load asset at "assets/assets/i18n/en/app.json" (404)
and for fix it, i need to switch basePath form assets/i18n to i18n

Hi @Atom735 , when you change the path in FlutterI18nDelegate you must adapt the pubsec.yml with the new asset folder.

Did you do that?

Nope... my localizations stay on PROJECT/assets/i18n/.
When i added a new keys to PROJECT/assets/i18n/ru/app.json, my WebApp in hotReload dont get it, with basePath = 'assets/i18n', and i changed it by hands to basePath = 'i18n', and hotReload getted new keys, and worked well... I overwrited to my Root Statefull widget method:

  @override
  void reassemble() {
    localeBasepath = localeBasepath.replaceAll('assets/', '');
    print(localeBasepath);
    super.reassemble();
  }

At now, it work well on web.. In first run, he had normal basepath, when he had hotReload, he change it, for all. But at now i catched error on Android, when i used hotReload. And i modifided it's to:

  @override
  void reassemble() {
    if (kIsWeb) {
      localeBasepath = localeBasepath.replaceAll('assets/', '');
      print(localeBasepath);
    }
    super.reassemble();
  }

And it work well...
I don't know why... But hotReload can't get new keys/soacenames on normal basePath, and it need to delete 'assert/' prefix, for FlutterI18n searched new tags and namespaces when hotReloaded.

The entry on pubsec.yaml and the basePath provided in the delegate must always be aligned, otherwise you can encounter the problem that you are describing.