13

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.

7
  • what you are try to achieve and whats your problem ? Commented Aug 25, 2014 at 11:01
  • @andruboy "only the two letters code" Commented Aug 25, 2014 at 11:02
  • yes that you have updated your question that i can see but what you want ? describe you problem Commented Aug 25, 2014 at 11:03
  • that when i change the device language to en(us) or en(gb) i will get the four digits locale and not only the two digits en Commented Aug 25, 2014 at 11:06
  • Locale.getDefault().getCountry() Commented Aug 25, 2014 at 11:06

4 Answers 4

30

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(); 
Sign up to request clarification or add additional context in comments.

2 Comments

'String cCode = Locale.getDefault().getCountry();' returns blank in emulator. Any idea how to fix this?
Because you assigned no country. that happens when you set locale with language.
2

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 :)

Comments

1

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).

Comments

-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.