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
Related
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.
May i know what is the difference between Locale.getDefault().getLanguage() and mContext.getResources().getConfiguration().locale.getLanguage()
Sorry for my draft but i need a detailed distinction between them.
Locale.getDefault().getLanguage();
Returns the language name for this Locale or the empty string if no
language was set, though document states that getLanguage() returns language code but it doesn't.
getResources().getConfiguration().locale.getLanguage()
Returns the Language code.
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
So I Have a TextView in my calculator app that I eventually display the results in it ...
It works ... but recently during the testing I found out that if The Default Language of That device is set to a non-english language (for example arabic or farsi) numbers in TextView get to be shown in that specific language (not english) and in a completely different format !!
I used this code to generate the result
result = String.format("%.4f", mResultBeforeFormatting);
resultTextView.settext(result);
also to note is that if I set the TextView with a hard coded string the issue doesn't happen
resultTextView.settext("343");
The formats used by the String.format method will by default be the formats specified by the device's default locale.
If you want to force the use of a specific locale, use the String.format method that accepts a locale parameter.
For example:
result = String.format(Locale.ENGLISH, "%.4f", mResultBeforeFormatting);
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)