1

I am developing an Android application for the global market, which will (hopefully!) be installed by users speaking different languages.

Since I want to adjust my interface language to the device's language, I need to acquire that language name in a convenient, standard format - preferably, ISO 639-2 3-letter format in which Hebrew is heb, English is eng and Spanish is spa.

How can I get the Android interface language in ISO 639-2 3-letter format?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562

2 Answers2

3

You can use this code snippet:

protected static String lang = Locale.getDefault().getISO3Language();//.substring(0, 2);

I use the 2 chars variant, but I cut that part out, to leave you the 3 chars variant, as required.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Fantastic! 1. Just to complete the picture - what is the interface language on your device, and what value returns form the call? 2. The 2-letters abbreviations are **not** a substring of the 3-letters abbreviations. – Adam Matan Aug 25 '14 at 11:23
  • I often set the language in a different locale, to test my app, but most of the times I leave the default English, which returns "en" (because I only get the first 2 chars from "eng" - I added the `;//` to match your specifications). Without the cut, I'd get "eng", which is also faster, since it has an operation less to perform (of course in terms of nanoseconds). `The 2-letters abbreviations are not a substring of the 3-letters abbreviations.` Are you sure? Since it gets the ISO3 Variant, it should be ISO-Locale (shortened to 3 chars) compatible... – Phantômaxx Aug 25 '14 at 11:31
  • I have to test if German gives me `ger` or `deu` (but it should return `de`, not `ge`, in my case - since I have no localization issue in my apps). – Phantômaxx Aug 25 '14 at 11:34
  • Tested: I correctly get "de" for German and "es" for Spanish. And "it" for Italian, "fr" for French, "en" for English. – Phantômaxx Aug 25 '14 at 11:42
  • 1
    The substring part of this answer is incorrect. For example, the two letter swedish language code is "sv" while the three letter one is "swe". – Simon Bengtsson Jul 20 '18 at 12:26
  • It seems that they did not translate the 3 chars code for Sweden and left it in English, while the 2 chars code is correct. – Phantômaxx Jul 20 '18 at 15:22
  • Yes, it seems to be the same for for many languages: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes – Simon Bengtsson Jul 24 '18 at 09:43
  • substring(0, 2) of ISO3Language is not a result of ISO standard language code. – Shanmugapriyan Mar 14 '22 at 03:59
3

List of ISO codes for languages

As you can see, substring the ISO 639-3 (3 letters) code does not make always the ISO 639-1 (2 letters) code. Also I do not recommend this. Choose a hashtable to get the corresponding code.

Phoenix
  • 76
  • 3