ilteoood / flutter_i18n

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

Enable translation without context #191

Closed MilosKarakas closed 1 year ago

MilosKarakas commented 2 years ago

Hi, I just completed the PR that enables translation with FlutterI18n instance, and without context. Basically I made BuildContext nullable in 4 functions that required it: translate(..), plural(..), currentLocale(..) and refresh(..). I added an optional named parameter FlutterI18n? instance to all of these, and have added it to I18nText and I18nPlural widgets as well. I decided to go with this kind of implementation to avoid adding additional 4 functions to the FlutterI18n class. IMHO it would make it too bloated. I am not completely satisfied with this solution either, since you need to pass null for BuildContext each time you translate without it, but I didn't have any better ideas how to implement it.

I have added tests as well.

Now you can register a FlutterI18n instance, and call for translator, for instance, somewhere in your data layer, like this:

FlutterI18n instance = GetIt.instance.get();
String translatedText = FlutterI18n.translate(null, 'translation_key', instance: instance);  

Whenever you pass an instance that is not null it will avoid trying to use the context. When instance is not passed context is used. Developers must take care not to set both BuildContext and instance to null.

MilosKarakas commented 2 years ago

@ilteoood Any idea when you could review this PR? Thanks.

MilosKarakas commented 2 years ago

@ilteoood Hey, do you have any feedback on this?

ilteoood commented 2 years ago

Hi @MilosKarakas, Sorry but I'm in honeymoon rn with limited internet connection.

I'll check it when I'll go home, I promise.

MilosKarakas commented 2 years ago

Hi @ilteoood , thanks for the response. Have a nice time during honeymoon 😄

ilteoood commented 2 years ago

Hi @MilosKarakas, I've looked at this implementation and, sincerely I don't like it too.

It's too complicated and do too much stuff for a single method. I've not finished yet, but here is my proposal: https://github.com/ilteoood/flutter_i18n/pull/198

Can you look at it and give me a suggestion pls?

Matteo

MilosKarakas commented 2 years ago

Hey @ilteoood I'll take a look until the end of next week and give you my opinion.

Regards, Miloš

MilosKarakas commented 2 years ago

Sorry, I'm still overloaded with work. I'll let you know as soon as I am done looking into it.