Monday, September 23, 2013

Locale language and the ISO-639-1 fix

If you ever want to have a "standard" naming convention for communicating Language code with a 3rd party service, you will want to have a common interface for that. For this purpose there's ISO reference called ISO-639-1 which Android is using.

So every thing is cool and the ISO can be used safely? Not 100%.

As Android docs mention:
Note that Java uses several deprecated two-letter codes. The Hebrew ("he") language code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji". This rewriting happens even if you construct your own Locale object, not just for instances returned by the various lookup methods.
Meaning, that those languages require a specific handling to transform them from the ISO-639 to ISO-639-1,
Here's a code snippet to help you out with it:
/**
* This method helps getting the right langauage ISO code, which suppose to be
* according to ISO-639-1, BUT, on some devices it still returns the deprecated ISO-639.
* <BR>
* Languages codes that are translated in this method:
* <ul>
* <li>Hebrew: IW -> HE
* <li>Indonesian: IN -> ID
* <li>Yiddish: JI -> YI
* </ul>
*
* @param locale - The locale to get the code from
* @return The right code according to ISO-639-1
*/
public static String getLanguageCode(Locale locale) {
String lang = locale.getLanguage();
if (lang.equalsIgnoreCase("IW")) {
return "HE";
} else if (lang.equalsIgnoreCase("IN")) {
return "ID";
} else if (lang.equalsIgnoreCase("JI")) {
return "YI";
} else {
return lang;
}
}
view raw getLanguageCode hosted with ❤ by GitHub
Use this whenever you want to interpret a language code - and you're good! 

No comments:

Post a Comment