The best way to automatically detect the user's language

I am developing a project where I need to automatically determine which user language is used to create content.

We actually use Locale.getDefault().getLanguage() , but this may not be the right choice, because, for istance, I live in Italy, I write content in Italian, but my local language by default on my phone is en_US .

So, I'm sure that perhaps I could identify it from the SIM country (and step back to the user language if the device does not have one), but there is no one-to-one relationship from the country to the language. For istance, the USA has en_US and es_US.

Do you have any suggestions? Should I request the current Locale content from the user?

+6
source share
1 answer

What is the reason you need the user language?

I also have the locale set by en_US, and I have it because I prefer to have a menu, etc. in English. (Translation of menus and settings, etc. Feels tense in my native language.)

Most likely, your users will be annoyed (I know I will) if you use anything other than Locale.getDefault().getLanguage() for what you use for this language.

If you really want to use the local language, I suggest you figure out the country code using heuristics based on

  • Default language
  • SIM country
  • Current network country

and use a hard-coded lookup table from the country code to (one of) the official language code. There are open databases with this information; see, for example, these resources:

+6
source

Source: https://habr.com/ru/post/983361/


All Articles