Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting language names to ISO 639 language codes

I need to convert language names like 'Hungarian', 'English' to ISO 639 codes. ISO 639-6 would be the best but ISO 639-2 is good enough. What's the best way to achieve this?

I should convert the English to locale and get the language with getLanguage()? If thats the only way how can I convert a string like 'English' to a java locale?

My goal is to store book language info using the ISO 639 codes.

like image 527
Lakatos Gyula Avatar asked Apr 14 '15 16:04

Lakatos Gyula


People also ask

What is ISO language code?

ISO 639 is a standardized nomenclature used to classify languages. Each language is assigned a two-letter (639-1) and three-letter (639-2 and 639-3) lowercase abbreviation, amended in later versions of the nomenclature.

How many languages are used in ISO?

There are 58 languages in ISO 639-2 which are considered, for the purposes of the standard, to be "macrolanguages" in ISO 639-3.

What is zh-CN language code?

zh-cn. Chinese (Hong Kong)

What are the abbreviations for languages?

There are two common abbreviations of language: lang. and lg.


1 Answers

    for (Locale locale : Locale.getAvailableLocales()) {
        System.out.println("" + locale
                + "; display: " + locale.getDisplayLanguage()
                + "; name: " + locale.getDisplayName()
                + "; lang: " + locale.getLanguage()
                + "; iso3: " + locale.getISO3Language());
    }

This will find some 150 locales, where ISO3 is the three letter variant, as opposed to the older two letter getLanguage.

The display language is the bare language name, whereas the display name is embellished with the country "German (Austria)."

So

public String toISO3(String name) {
    for (Locale locale : Locale.getAvailableLocales()) {
        if (name.equals(locale.getDisplayLanguage()) {
            return locale.getISO3Language();
        }
    }
    throw new IllegalArgumentException("No language found: " + name);
}

For the display methods there is an optional Locale parameter, to explicitly set to Locale.ENGLISH.

like image 63
Joop Eggen Avatar answered Sep 29 '22 08:09

Joop Eggen