47

I'd need to get the full country name from the country code. For example for Netherlands, I'd need the Netherlands from the country code NL.

I thought I could do that with Locale like:

Locale loc = new Locale("NL");
loc.getCountry();

but loc.getCountry(); is empty.

How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
noloman
  • 11,411
  • 20
  • 82
  • 129

5 Answers5

124

Java

Locale loc = new Locale("","NL");
loc.getDisplayCountry();

Kotlin

import java.util.Locale
Locale("", "NL").displayCountry
Felix
  • 2,548
  • 19
  • 48
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
  • 1
    The Locale constructor's above single param version takes first parameter as language and not country code. This did not work in my case. It should be something like this, if country code has to be used. Locale loc = new Locale("", "NL"); – Rajat Sharma Oct 10 '13 at 10:35
  • This worked for me, I just wasted a lot of time thinking whats going wrong with Local(OneParam)....my bad I didnt check the documentation...but thanks for sharing – anoop4real Nov 29 '17 at 06:16
25

This should work:

Locale l = new Locale("", "NL");
String country = l.getDisplayCountry();

The first parameter of Locale is the language, which is not useful in your case.

assylias
  • 321,522
  • 82
  • 660
  • 783
12

I would like to add more information to the answers above.

If you want to specify language for results, you can use Locale("your language") as the parameter of getDisplayCountry().

For example:

(new Locale("","NL")).getDisplayCountry(new Locale("ZH"));

"ZH" is the language code of Chinese. You will get "荷兰", which is the Chinese name of Netherlands.

And you can use Locale("languages", "ISO-3166 code") to specify your language variant.

for example:

(new Locale("","NL")).getDisplayCountry(new Locale("ZH", "TW"));

Locale("ZH", "TW") means the variant of Chinese in Taiwan (traditional Chinese). It has many differences from the Chinese Mainland variant.

You will get "荷蘭", which is the traditional Chinese name of Netherlands.

(Even if you don’t understand Chinese, I think it's obvious that the second Chinese character in the two names is different.)

If you do not specify a language, you will get the name of Netherlands in the device display language, which can be changed in the phone settings. The code is:

(new Locale("","NL")).getDisplayCountry();

You can get a list of all languages and variants supported by Android in this question :

If you're using kotlin:

Locale("", "NL").getDisplayCountry(Locale("ZH"))
Locale("", "NL").getDisplayCountry(Locale("ZH", "TW"))
Locale("", "NL").displayCountry
Sun Jiaojiao
  • 321
  • 2
  • 10
0

Try to use the other constructor

Locale loc = new Locale("NL", "The Netherlands");

Locale There does not appear to be a predefined Locale for The Netherlands

Ruben
  • 796
  • 3
  • 9
  • 21
  • 5
    but I can't know that `NL` is `the Netherlands`, actually, that's what I wanna get, `the Netherlands` from `NL` :) – noloman Jan 11 '13 at 10:20
0

for a full solution TelephonyManager (from this solution):

TelephonyManager teleMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String localeCountry = teleMgr.getNetworkCountryIso();
if (localeCountry != null) {
    Locale loc = new Locale("",localeCountry);
    Log.d(TAG, "User is from " + loc);
}
Community
  • 1
  • 1
Odaym
  • 1,896
  • 1
  • 20
  • 31