16

I want to show a button only for English users, is there a way to detect the language settings?

I know how to get the current Locale, but I don't know if comparing it against Locale.English is sufficient, since there must be a lot of English variations etc.

Anyone experience doing this?

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • http://stackoverflow.com/questions/4212320/get-the-current-language-in-device/23168383#23168383 – trante Jun 02 '15 at 19:49

8 Answers8

28

From the Locale docs:

The language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1. The country codes are two-letter uppercase ISO country codes (such as "US") as defined by ISO 3166-1.

This means that

Locale.getDefault().getLanguage().equals("en")

should be true. I'd be careful with hiding/showing UI only by default Locale though. Many countries may have many users that prefer another language, but are perfectly fluent in English.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • But is there a possibility to only get `en` instead of English? – Max Allan Sep 19 '12 at 19:56
  • 1
    @MaxAllan It's not only a possibility, that's the intent of this code. That's why I'm comparing to `"en"`. This is preferrable especially in the general case in my opinion because then you don't have to think or worry about cases like "Español" vs. "Spanish", etc. There's a universally accepted language code for Spanish: `es`. – kabuko Sep 19 '12 at 20:45
  • Yes, I understand that part, but in the case that I would like to select rows from a database where the column `lang = 'sv'` for Swedish, how will I do then? If the UI is in Swedish. – Max Allan Sep 19 '12 at 21:12
  • @MaxAllan Locale.getDefault().getLanguage().equals("sv") – IgorGanapolsky Jun 17 '14 at 16:23
  • it can be Locale.getDefault().getLanguage().contains("en") to check all varient of english – AMD Feb 10 '16 at 10:45
15
Locale.getDefault().getDisplayLanguage() will give your default language of your device

Example

System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());

Result

My locale::English

the_drow
  • 18,571
  • 25
  • 126
  • 193
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
5

What about using Java's startsWith() function to check whether the current locale is an English variant or not.

Locale.getDefault().getLanguage().startsWith("en")
blizzard
  • 5,275
  • 2
  • 34
  • 48
2

The proper way is probably:

boolean def_english = Locale.getDefault().getISO3Language().equals(Locale.ENGLISH.getISO3Language());
Pascalius
  • 14,024
  • 4
  • 40
  • 38
2

An alternative solution would be to create a localized English version of the form. See http://developer.android.com/guide/topics/resources/localization.html for details.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
1

All I can say about language is that :

1- in order to get the current language of app itself you should use

String CurrentLang = getResources().getConfiguration().locale.getLanguage();

2- in order to get the current language of the device you should use

String CurrentLang = Locale.getDefault().getLanguage();

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
  • This is useful information; however, it doesn't answer the OP's question of, how to tell whether it is a *variant* of English, i.e. en-GB, etc. – Alex Aug 22 '17 at 16:08
0

To know if the default language is an english variant (en_GB or en_AU or en_IN or en_US) then try this

if (Locale.getDefault().getLanguage().equals(new Locale("en").getLanguage())) {
    Log.d(TAG, "Language is English");
}
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

Locale.getDefault().getDisplayLanguage();

if(Locale.getDefault().getDisplayLanguage().equals("English")){

    //do something
}
Jake Conway
  • 901
  • 16
  • 25
Demacin
  • 11