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?
You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").
Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have to choose qualifiers as Locale from the available list and select the language as Hindi from the drop-down list. Below is the picture of the steps to be performed.
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
System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With