Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose API to get current locale #16

Closed
crizant opened this issue Feb 27, 2019 · 11 comments
Closed

Expose API to get current locale #16

crizant opened this issue Feb 27, 2019 · 11 comments
Assignees
Labels
enhancement New feature or request

Comments

@crizant
Copy link

crizant commented Feb 27, 2019

Hi, thanks for creating this awesome plugin.
Is there any ways to get the current locale?

@ilteoood
Copy link
Owner

Hi, to find system locale I use this function: https://docs.flutter.io/flutter/intl_standalone/findSystemLocale.html

The function that I wrote return a Locale instead of a String. If you prefer, I could make that method public.

@crizant
Copy link
Author

crizant commented Feb 28, 2019

Yes please, I see you stored a locale object in FlutterI18n instance, it would be useful if you make it public.

@ilteoood
Copy link
Owner

That object doesn't represent the system locale, but the locale used by the library for translation purposes.
I will make the _findCurrentLocale public and static.

@crizant
Copy link
Author

crizant commented Feb 28, 2019

Yes i would like to get the locale being used by the library. Thanks.

@ilteoood
Copy link
Owner

Why you need it?

@crizant
Copy link
Author

crizant commented Feb 28, 2019

Because my application would change language in runtime, I have an observable store to store the language that the app is using, when it changes, it triggers the component to re-render.
But first I have to know the locale currently using, in order to initialize the observable store.

@ilteoood
Copy link
Owner

Wait, you don't need to do this.
When the system language change, the library will load the new translation asset and will reload every translation.
Have you a detailed example? Other people do the same thing without this information.

@crizant
Copy link
Author

crizant commented Mar 1, 2019

No it's not about the system language.

Like this example from you,
you have to have a state to hold the language, when it updates it triggers rebuild of the layout.
When you take it away, the layout won't rebuild when language changes.

My case is very similar, but in line 32 you initialized currentLang to en, this is incorrect, when the device locale is not english, e.g. Italian.
I need to initialize the state to the locale that your plugin is using.

@Quijx
Copy link

Quijx commented Mar 2, 2019

@ilteoood
I think @crizant has a point. I also have an application where the language is not dependent on the system language, but on a setting in a settings page.

I did a bit of research and I think that the proper way to control the language inside the app is to set the locale attribute inside the MaterialApp Widget. The value can be dynamic either with something like the scoped_model package or simply a StatefulWidget.

If the locale changes, the load method of the LocalizationsDelegate is called again with the given locale.

If the locale attribute is unset, the locale given to load is dependent on the system language priority list.
Sadly I could not find a lot of documentation on this, but I tested a bit and believe the algorithm for selecting the locale goes like this:

Take the locale with the highest priority in the system language settings, that is also in the supportedLocales attribute in the MaterialApp Widget.
If none is found, use the first locale in the supportedLocales attribute.

This also means that you shouldn't need a fallback file because the fallback locale is managed by flutter.

But since you are not using the locale that is given inside of load in your FlutterI18nDelegate class, switching the language this way does not work. (Also FlutterI18n.refresh is not working for me, but that might have a different reason.)

And when the locale is selected in this way, getting the current locale is really useful because I think the only alternative would be to create a custom Delegate that stores the locale somewhere when load is called.

You should take everything I just wrote with a grain of salt, as I am relatively new to flutter.

@ilteoood
Copy link
Owner

ilteoood commented Mar 3, 2019

Hi @Quijx,
your analysis is correct and well formed.
The reason why I don't use the LocalizationsDelegate is to leave the possibility to override the fallback settings of the device (eg: English for everyone, even if the fallback language is Spanish). Also, with this approch, the entire widget tree isn't reloaded when the system language change.
I read your motivation and decided to approve this request. Will be available ASAP.

Thank you,
Matteo

@ilteoood ilteoood self-assigned this Mar 3, 2019
@ilteoood ilteoood added the enhancement New feature or request label Mar 3, 2019
ilteoood added a commit that referenced this issue Mar 5, 2019
@ilteoood
Copy link
Owner

ilteoood commented Mar 5, 2019

Fixed with 9e56c99

Please pay attention to the breaking changes.

Thank you so much for your contribute,
Mattei

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants