ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

Append to baseUri for network loading #169

Closed alnah005 closed 3 years ago

alnah005 commented 3 years ago

Is it possible to add to the NetworkFileTranslationLoader baseUri? e.g. from

NetworkFileTranslationLoader (
      baseUri: Uri.https("www.baylse-services.com"),
)

then somehow

FlutterI18n.translate(context, <text_to_translate>,<add "i18nget/" to the baseUri>))

Which would call www.baylse-services.com/i18nget/ to get the translation file en.json.

I'm just wondering if there's a way to avoid creating a flutterI18nDelegate for each route that contains en.json

alnah005 commented 3 years ago

Just looked at the code and it seems like this would be possible by overriding the translate method in flutter_i18n.dart and adding a CustomNetworkFileTranslationLoader that has the option to append to baseUri. Another option is to completely overwrite the translationLoader of _retrieveCurrentInstance and then calling load. Please reply here if there's a more concrete way.

ilteoood commented 3 years ago

You are using Uri.https, which also support an encoded path as second parameter: https://api.flutter.dev/flutter/dart-core/Uri/Uri.https.html

Did you tried that?

alnah005 commented 3 years ago

You are using Uri.https, which also support an encoded path as second parameter: https://api.flutter.dev/flutter/dart-core/Uri/Uri.https.html

Did you tried that?

That worked, but for a single route. My main problem is whether it's possible to use the same NetworkFileTranslationLoader for multiple routes.

ilteoood commented 3 years ago

What do you mean by routes?

A single instance of NetworkFileTranslationLoader is used for each language that your app support.

alnah005 commented 3 years ago

Let's say for example I have two "en.json" files, one in example.com/page1 and one in example.com/page2 or maybe one with example.com/page?num=1 and example.com/page?num=2. How would I use one NeworkFileTranslationLoader in those cases?

ilteoood commented 3 years ago

With only the NeworkFileTranslationLoader you can't do it. You should use the NamespaceFileTranslationLoader and combine its load with the NeworkFileTranslationLoader.

Currently isn't supported, but you can create your own loader and open a PR to integrate it in this repo :)