In my android app I need to know which is the language of the phone in this format :fr (for francais), en (for english),etc.. How can I do this? I know that I can display language with this :
Locale.getDefault().getDisplayLanguage()
but the result is francais,english,...
Can I obtain somehow the initials of languages?
Locale.getDefault().getLanguage()
Returns the language code for this Locale or the empty string if no language was set.
http://developer.android.com/reference/java/util/Locale.html#getLanguage()
How about using
Locale.getDefault().getLanguage();
I think you can try one of these two.
Locale.getDefault().getLanguage();
This will give language iso code i.e. "de", "ru". OR
Resources.getSystem().getConfiguration().locale;
This returns global shared Resources object that provides access to only system resources.
Related
For date parsing, I'm using SimpleDateFormat.
I know that it's possible to pass Locale object to translate month names.
I tried to use the Estonian language: Locale("ee", "Estonia"), but it did not work.
Am I using the wrong language code or Estonian language is not supported thus I've to use a different approach?
I was able to use Estonian by using:
Locale("et", "ee")
You can get the correct Locale for any country from Translations Editor in Android studio.
For Estonian country, Locale should be Locale("et", "ee")
The app I work on will be launched in the UK, US and AU. I have strings.xml in values-en-rUS , values-en-rAU and values folders. I have certain strings for ex "Zipcode" which will only be used in the US so I have added them to strings.xml in values-en-rUS. I have "Postcode" as the equivalent in AU and UK strings.xml files.
This works fine as long as the device locale is set correctly ie, English(United states) in United states. If the device is in English (Australia) and the user is using the app in United states the solution fails.
Is there a standard approach for displaying a specific string irrespective of the device locale? Any help is appreciated
I think you are using the wrong localization.
values-en-rUS you are defining resources in english (locale en) for mobiles situated in the US region.
If you use values-en_US instead it should correctly work for the locale en_US.
To better understand how the fallback between the different locales will happen consider this link:
Android doc
The correct Java SE standard usage is:
Locale: Properties file name part:
Locale("en", "US") values_en_US
Locale("en", "AUS") values_en_AUS
Locale("en", "UK") values_en_UK
Best have a Locale("en") / values_en and values too.
There are some Locale constants. A small prototype will see whether this works satisfactory.
I am trying to create multilangualge app and faced with a problem! I have a string in values\strings.xml translated in German language values-de\strings.xml. I am trying to compare user input with those strings. If my input is in English and device's language is also English, everything works fine, but if I switch device's language to German and input a string in English, contains() and equals() methods will return false. Is there a way to compare strings in different languages? Thanks in advance! Also, sorry for my English!
if (mystring.contains(context.getResources().getString(R.string.testString))) {
check = true;
}
if (mystring.equals(context.getResources().getString(R.string.testString))) {
check = true;
}
In android when you call getResources() it will always get the resources of the default locale, to get one from other locales you must specify explicitly which locale you want to use, and you can find how to do it here :
https://stackoverflow.com/a/33629163/6171845
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