Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to automatically determinate user language

I'm developing a project where I need to automatically determinate which is the user language used to created the content.

We're actually using Locale.getDefault().getLanguage() but this can not be the right choice because, for istance, I live in Italy, I write content in Italian but my default Locale on my phone is en_US.

So I tought that maybe I could determinate it from the SIM Country (and fallback to user Locale when the device does not have one) but there's no one-to-one relation from a Country to a Language. For istance, US has en_US and es_US.

Do you have any suggestion? Should I ask the user for their current content's Locale?

like image 978
StErMi Avatar asked Mar 05 '15 09:03

StErMi


1 Answers

What's the reason you need the user language?

I too have my locale set to en_US, and I have that because I prefer to have menus etc in English. (The translation of menus and settings etc feels strained in my native language.)

Chances are that your users will be annoyed (I know I would) if you use anything other than Locale.getDefault().getLanguage() for whatever you're using the language for.

That being said, if you really want to use a local language, I suggest you figure out the country code by some heuristics based on

  • Default locale
  • SIM country
  • Current network country

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

  • List of language codes by country
  • Country codes / language codes on the OpenStreetMap wiki
like image 115
aioobe Avatar answered Oct 11 '22 17:10

aioobe