Locale: App's Current Language - android

How to programatically find the language the app is using. For example the device Locale is set to es_MX but my app supports only en_US and hence the app displays text only in English.
How to programatically find that app is using English and not Spanish in this case?
All the below code returns ex_MX
Locale locale = getResources().getConfiguration().getLocales().get(0);
Locale l = Resources.getSystem().getConfiguration().getLocales().get(0);
Locale l1 = Locale.getDefault();

Please Refer this given link's answer as you must get locale at the time of starting the app. If you will set your locale once in whole life cycle, you will get only the language as default as you set in the locale.
https://stackoverflow.com/a/23556454/6549856

You can get the system language.
Resources.getSystem().getConfiguration().locale.getLanguage();
for app default language
String CurrentLanguage = Locale.getDefault().getLanguage();

Related

How to get locale with region?

I am trying to get the current device locale with the region like "en_us","en_gb".
I am calling Locale.getDefault().getLanguage() and it returns only the two letters code en.
Format like "en_us" or "en_gb" has "language code"_"country code"
A Locale object contains both country code and language code.
So you can use below snippet to format your own code..
String cCode = Locale.getDefault().getCountry();
String lCode = Locale.getDefault().getLanguage();
String code = lCode+"_"+cCode;
or
you can use toString() method on Locale object to get the data
String code = Locale.getDefault().toString();
The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched. Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated.
If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e.
Locale current = getResources().getConfiguration().locale;
You may find that this value is updated more quickly after a settings change if that is necessary for your application.
i have tested this :)
i have got from this as link may be deleted so Answer copied :)
It's worth pointing out that locale codes and language tags are related but different. Locale codes have an underscore separator (e.g. fr_CA), and language tags have a dash separator (e.g. fr-ca). I'm sure there are some deeper differences but that's beyond my pay grade.
This answer gives the result of various methods on the Locale class: https://stackoverflow.com/a/23168383
It looks like you want the toString() method (to get the locale code) or the toLanguageTag() (to get the language tag).
Use
Locale.getDisplayName();
This is shorthand for
Locale.getDisplayName(Locale.getDefault());
The documentation is in here:http://developer.android.com/reference/java/util/Locale.html

Match DatePickerDialog button order to device locale on Android

The button order on all DatePickerDialog in my app isn't affected by the device's locale. If, for example, I change my locale to a right-to-left aligned language, such as Hebrew or Arabic, the button order on all other apps on my device automatically changes but remains the same on my own app! Why is that? And how can I tell my app to "adapt" to the newly selected locale and change the button order accordingly?
This is driving me crazy. Any help or hint you can provide will be greatly appreciated. Thanks...
You can use getDefault() to get an appropriate locale for the user of the device you're running on. Then try to apply the locale to the config.
Locale locale = Locale.getDefault();
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
I don't know if the code snippet above works on your case. But you might give it a try.

Any country name in current default locale

What I want do do is to provide a ISO 3166-1 country code and retrive the name of that country in the current locale. For those of you who are familiar with iPhone, I have an an example of exactly what I want to do:
NSLocale* currentLocale = [NSLocale currentLocale];
NSString* countryName = [currentLocale displayNameForKey:NSLocaleCountryCode value:#"NO"];
In this case the variable countryName would contain "Norway" given the iPhone was running in an english locale.
What I have understood so far is that to get the current locale in the Android SDK by a simple static method of the Locale class.
Locale currentLocale = Locale.getDefault();
But Im stuck here...
Locale l = new Locale("en", "NO");
String norway = l.getDisplayCountry();
works for me. Just replace "NO" by the country you want and it should give you the name in your current default locale. The "en" is just there to fill in some language but it should not matter which you use (At least I hope that works for all combinations - have not tested them all)

How do I access default locale resource strings in Android whilst in a different locale?

If I switch my phone's locale to German for example, I still want the option to be able to access the english / default resources using something along the lines of:
getString(R.string.some_text, locale);
I've tried switching the default locale using
Locale.setDefault
but as expected, when using R.getString(x), my resource was still in German.
Answer:
Changing Locale within the app itself

How to retrieve actual Locale/Language

can anyone tell me how I programmatically retrieve the actual Locale and/or Language in Android ?
I think you mean the locale of the phone:
Locale myPhoneLocale = Locale.getDefault();

Categories

Resources