How do I get a locale with a region?

I am trying to get the current localization of a device with a scope like "en_us", "en_gb".

I am calling Locale.getDefault().getLanguage()

and returning only two characters of code en

.

+3


source to share


3 answers


Format "en_us" or "en_gb" has "language code" _ "country code"

The Locale object contains the country code and language code.

So you can use the snippet below to format your own code.

String cCode = Locale.getDefault().getCountry();
String lCode = Locale.getDefault().getLanguage();
String code = lCode+"_"+cCode;

      



or

you can use method toString()

on Locale object to get data

String code = Locale.getDefault().toString();

      

+11


source


By default it Locale

is created statically at runtime for your application process from the system property settings, so it will display what Locale

was selected on that device when the application was started . This is generally fine, but it means that if the user changes their preferences Locale

in preferences after starting your application process, the value getDefaultLocale()

probably won't be updated immediately.

If you need to somehow trap such events in your application, you can instead try to get those Locale

available from the resource Configuration

, i.e.

Locale current = getResources().getConfiguration().locale;

      



You may find that this value is updated faster after changing the settings, if necessary for your application.

I tested this :)

i got it from this as the link can be removed so the answer is copied :)

+1


source


Using

Locale.getDisplayName();

      

This is shorthand for

Locale.getDisplayName(Locale.getDefault());

      

The documentation is here: http://developer.android.com/reference/java/util/Locale.html

-2


source







All Articles