Search code examples
pythonpyenchant

Python Enchant language descriptions


I'm working with the pyenchant module. I couldn't find in their documentaion and anywhere else, the descriptions for the languages listed in enchant.list_languages(). This function returns a list of tags, I need the human friendly descripction of each language.

import enchant
enchant.list_languages()
['af', 'am', 'ar', 'bg', 'bn', 'br', 'ca', 'cs', 'cy', 'da', 'de', 'de_AT', 'de_CH', 'de_DE', 'el', 'en', 'en_CA', 'en_GB', 'en_US', 'eo', 'es', 'et', 'eu', 'fa', 'fo', 'fr', 'fr_CH', 'fr_FR', 'ga', 'gl', 'gu', 'he', 'hi', 'hr', 'hsb', 'hu', 'hy', 'id', 'is', 'it', 'kk', 'kn', 'ku', 'lt', 'lv', 'ml', 'mr', 'nb', 'nl', 'nn', 'no', 'nr', 'ns', 'or', 'pa', 'pl', 'pt_BR', 'pt_PT', 'ro', 'ru', 'sk', 'sk_SK', 'sl', 'st', 'sv', 'ta', 'te', 'tl', 'tl_PH', 'tn', 'ts', 'uk', 'uz', 'xh', 'zu']

How can I translate all this tags to the human friendly name of these languages ('English', 'Spanish', 'Italian', etc)? Is there a function in pyenchant or another library to accomplish this?

Thanks a lot!!


Solution

  • You can use pycountry:

    To get the language name, you can use:

    pycountry.languages.get(alpha_2=language)
    

    Where language is one of the pyenchant languages.

    EDIT

    Here is how you can print the pyenchant language names (and countries):

    import pycountry
    
    enchant_codes = [
        "af", "am", "ar", "bg", "bn", "br", "ca", "cs", "cy", "da",
        "de", "de_AT", "de_CH", "de_DE", "el", "en", "en_CA", "en_GB", "en_US", "eo",
        "es", "et", "eu", "fa", "fo", "fr", "fr_CH", "fr_FR", "ga", "gl", "gu",
        "he", "hi", "hr", "hsb", "hu", "hy", "id", "is", "it", "kk",
        "kn", "ku", "lt", "lv", "ml", "mr", "nb", "nl", "nn", "no", "nr",
        "ns", "or", "pa", "pl", "pt_BR", "pt_PT", "ro", "ru", "sk", "sk_SK", "sl",
        "st", "sv", "ta", "te", "tl", "tl_PH", "tn", "ts", "uk", "uz", "xh", "zu",
    ]
    
    for code in enchant_codes:
        lang_code, _, country_code = code.partition("_")
        if len(lang_code) == 2:
            language = pycountry.languages.get(alpha_2=lang_code)
        elif len(lang_code) == 3:
            language = pycountry.languages.get(alpha_3=lang_code)
        else:
            language = None
        language_name = language.name if language else "(unknown language)"
        country = pycountry.countries.get(alpha_2=country_code) if country_code else None
        country_name = country.name if country else ""
        print(code, "=>", language_name, "/", country_name)