How to detect if the language is English (All variants) on Android?
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 expe开发者_运维知识库rience doing this?
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.
Locale.getDefault().getDisplayLanguage() will give your default language of your device
Example
System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());
Result
My locale::English
What about using Java's startsWith() function to check whether the current locale is an English variant or not.
Locale.getDefault().getLanguage().startsWith("en")
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.
The proper way is probably:
boolean def_english = Locale.getDefault().getISO3Language().equals(Locale.ENGLISH.getISO3Language());
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();
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");
}
Locale.getDefault().getDisplayLanguage();
if(Locale.getDefault().getDisplayLanguage().equals("English")){
//do something
}
精彩评论