Android get device locale -
After installing my Android program I check the device locale:
string Device locale = locale. .getDefault () getLanguage ();
If deviceLocale is inside my supported languages (English, French, German), then I do not change the locale.
But for example, if I do not support the device's language: for example Spanish.
I set the existing locale in English, because most people can understand English to some extent.
Locale Locale = New Locale ("N"); Locale.setDefault (location); Configuration config = new configuration (); Config.locale = locale; PContext.getResources (). Updateconfiguration (config, null);
But after that, in other ways, when I check the device's locale like this:
string device locale = locale.gate default ( ). GetLanguage ();
I get the result as "English" but the location of the device is Spanish
What does the language () give to the current app's locale?
How can I get the device's locale?
EDIT: In the first part of the app, this device is an option to save locale but if the user changes the device after saving it, then I can not locate the new locale of the device. I can only know the saved location in app installs
There is a very simple and cleaner way to do this in comparison to refining or parsing some system properties .
As you once set the default locale in your app, you do not have access to the system default locale. The default locale set in your app is valid only during the life cycle of your app. Whatever you set in your locale, the set-local goes after the process ends. When the app is restarted, you will be able to read the system default locale again.
So whatever you have to do, the system retrieves the system default locale when it starts, as long as the app runs and should update it, the user decides to change the language in Android settings should do. Since we need this piece of information till the app runs, we can put it in a stable variable, which is accessible from somewhere in your app.
And here's how we do it:
Application in public class MyApication {public static string sDefSystemLanguage; @ Override Public Empty on Create () {Super. Connet (); SDefSystemLanguage = Locale.getDefault (). GetLanguage (); } @ Override converting configuration to public Zero (Configuration new config) {super.onConfigurationChanged (newConfig); SDefSystemLanguage = newConfig.locale.getLanguage (); }}
Use an application (do not forget to define it in your manifest) When the app starts (turns on) we get the default locale and when the user changes Languages in Android settings (On Configuration Change). All this is there whenever you need to know that if you use your set default call before using the system default language, sDefSystemLanguage will respond to you.
There is an issue I saw in your code when you set a new locale that you do:
configuration configuration = new configuration (); Config.locale = locale; PContext.getResources (). Updateconfiguration (config, null);
When you do this, you overwrite all the configuration information that you want to keep, such as Configuration.fontScale Accessibility settings shows large text. Setting up the language and losing all other configurations will not cause your app to show large text settings more, which means that this text should be less (if the user has enlarged large text setting). The correct way to do this is:
resource res = pContext.getResources (); Configuration config = res.getconfiguration (); If (Build.VERSION.SDK_INT & gt; = Build.VERSION_CODES.JELLY_BEAN_MR1) {configuration.setLocale (Locale.CANADA); } Else {configuration.locale = Locale.CANADA; } Res.updateConfiguration (config, null);
So instead of creating a new configuration object, we only update the current one.
Comments
Post a Comment