File size: 4,447 Bytes
adca588 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
class Language():
def __init__(self, code, name):
self.code = code
self.name = name
def __str__(self):
return "Language(code={}, name={})".format(self.code, self.name)
LANGUAGES = [
Language('en', 'English'),
Language('zh', 'Chinese'),
Language('de', 'German'),
Language('es', 'Spanish'),
Language('ru', 'Russian'),
Language('ko', 'Korean'),
Language('fr', 'French'),
Language('ja', 'Japanese'),
Language('pt', 'Portuguese'),
Language('tr', 'Turkish'),
Language('pl', 'Polish'),
Language('ca', 'Catalan'),
Language('nl', 'Dutch'),
Language('ar', 'Arabic'),
Language('sv', 'Swedish'),
Language('it', 'Italian'),
Language('id', 'Indonesian'),
Language('hi', 'Hindi'),
Language('fi', 'Finnish'),
Language('vi', 'Vietnamese'),
Language('he', 'Hebrew'),
Language('uk', 'Ukrainian'),
Language('el', 'Greek'),
Language('ms', 'Malay'),
Language('cs', 'Czech'),
Language('ro', 'Romanian'),
Language('da', 'Danish'),
Language('hu', 'Hungarian'),
Language('ta', 'Tamil'),
Language('no', 'Norwegian'),
Language('th', 'Thai'),
Language('ur', 'Urdu'),
Language('hr', 'Croatian'),
Language('bg', 'Bulgarian'),
Language('lt', 'Lithuanian'),
Language('la', 'Latin'),
Language('mi', 'Maori'),
Language('ml', 'Malayalam'),
Language('cy', 'Welsh'),
Language('sk', 'Slovak'),
Language('te', 'Telugu'),
Language('fa', 'Persian'),
Language('lv', 'Latvian'),
Language('bn', 'Bengali'),
Language('sr', 'Serbian'),
Language('az', 'Azerbaijani'),
Language('sl', 'Slovenian'),
Language('kn', 'Kannada'),
Language('et', 'Estonian'),
Language('mk', 'Macedonian'),
Language('br', 'Breton'),
Language('eu', 'Basque'),
Language('is', 'Icelandic'),
Language('hy', 'Armenian'),
Language('ne', 'Nepali'),
Language('mn', 'Mongolian'),
Language('bs', 'Bosnian'),
Language('kk', 'Kazakh'),
Language('sq', 'Albanian'),
Language('sw', 'Swahili'),
Language('gl', 'Galician'),
Language('mr', 'Marathi'),
Language('pa', 'Punjabi'),
Language('si', 'Sinhala'),
Language('km', 'Khmer'),
Language('sn', 'Shona'),
Language('yo', 'Yoruba'),
Language('so', 'Somali'),
Language('af', 'Afrikaans'),
Language('oc', 'Occitan'),
Language('ka', 'Georgian'),
Language('be', 'Belarusian'),
Language('tg', 'Tajik'),
Language('sd', 'Sindhi'),
Language('gu', 'Gujarati'),
Language('am', 'Amharic'),
Language('yi', 'Yiddish'),
Language('lo', 'Lao'),
Language('uz', 'Uzbek'),
Language('fo', 'Faroese'),
Language('ht', 'Haitian creole'),
Language('ps', 'Pashto'),
Language('tk', 'Turkmen'),
Language('nn', 'Nynorsk'),
Language('mt', 'Maltese'),
Language('sa', 'Sanskrit'),
Language('lb', 'Luxembourgish'),
Language('my', 'Myanmar'),
Language('bo', 'Tibetan'),
Language('tl', 'Tagalog'),
Language('mg', 'Malagasy'),
Language('as', 'Assamese'),
Language('tt', 'Tatar'),
Language('haw', 'Hawaiian'),
Language('ln', 'Lingala'),
Language('ha', 'Hausa'),
Language('ba', 'Bashkir'),
Language('jw', 'Javanese'),
Language('su', 'Sundanese')
]
_TO_LANGUAGE_CODE = {
**{language.code: language for language in LANGUAGES},
"burmese": "my",
"valencian": "ca",
"flemish": "nl",
"haitian": "ht",
"letzeburgesch": "lb",
"pushto": "ps",
"panjabi": "pa",
"moldavian": "ro",
"moldovan": "ro",
"sinhalese": "si",
"castilian": "es",
}
_FROM_LANGUAGE_NAME = {
**{language.name.lower(): language for language in LANGUAGES}
}
def get_language_from_code(language_code, default=None) -> Language:
"""Return the language name from the language code."""
return _TO_LANGUAGE_CODE.get(language_code, default)
def get_language_from_name(language, default=None) -> Language:
"""Return the language code from the language name."""
return _FROM_LANGUAGE_NAME.get(language.lower() if language else None, default)
def get_language_names():
"""Return a list of language names."""
return [language.name for language in LANGUAGES]
if __name__ == "__main__":
# Test lookup
print(get_language_from_code('en'))
print(get_language_from_name('English'))
print(get_language_names()) |