Any country name in current default locale - android

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)

Related

Handling Android Locale

I was struggling with date formatting in Kotlin.
Does someone know why using :
val locale = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale).format(Date())
give me :
In FR_fr = 08/09/2022 (expected)
In EN_gb = 08/09/2022 (unexpected)
BUT
val currentLanguage = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
val locale = Locale(currentLanguage)
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale).format(Date())
gives me :
In FR_fr = 08/09/2022 (expected)
In EN_gb = 9/8/2022 (expected)
Is there a simpler way?
In the second one, while you are doing this,
ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
it will give language as en.It will not give country variant english.It will return generic english locale.
So you should create locale like below.
val currentLanguage = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)
if (currentLanguage!=null) {
val currentLocale = Locale(currentLanguage.language, currentLanguage.country, currentLanguage.variant)
}
THis will create locale with en-GB in your case.
In the first one you are already getting locale object with en-GB.
en-GB and en has different formats of date.
08/09/2022 : en-GB
9/8/2022 : en
en-IN (Indian English) also will give 08/09/2022 as result.
The reason for the different results is because
in you first example, you work with actual en_GB/fr_FR locale,
but in the second example, your locale is only en/ fr.
That's because language/getLanguage() in this instance returns only en/ fr, without country specification.
So, I'd say, the first result (08/09/2022) is the correct one for en_GB. If you want a different date format you probably have to create your own one.

Locale: App's Current Language

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();

Android get Locale Currency

I have ran into a little tricky thing here.
The thing is that I want to get the Locale Currency of the User, so I do this:
final Locale currentLocale = getResources().getConfiguration().locale;
final Currency currency = Currency.getInstance(currentLocale);
mCurrencyCode = currency.getCurrencyCode();
Which gives me: AUD as for Australia
The problem? Well, I am in New Zealand and the Currency Code is NZD
How could I get the right Currency Code?
Cheers

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

Android new Locale("en_US") and Locale.US are different?

I just ran into this problem when testing the locale set in the preferences against constant values:
(new Locale("en_US")).equals(Locale.US) == false
When looking at the details it turns out that new Locale("en_us") returns an object with a language code "en_us" and a country code that is a zero length string whereas Locale.US returns an object with language code "en" and country code "US". Locale("en","US") returns the same result as Locale.US so its easy to avoid this problem, but is this the expected behavior of the Locale constructors?
Locale constructors are working as expected. You can compare the locale objects like this:
(new Locale("en_US")).toString().equalsIgnoreCase((Locale.US).toString())
It will give you the expected value

Categories

Resources