From bdc88b1e48446ab0362d78ab61721daad8eb6df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Klete=C4=8Dka?= Date: Sun, 27 Aug 2023 20:55:59 +0200 Subject: [PATCH] Added mappings between various representation of languages --- language.go | 21 ---- mapping.go | 355 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 355 insertions(+), 21 deletions(-) create mode 100644 mapping.go diff --git a/language.go b/language.go index deb21cc..8932d47 100644 --- a/language.go +++ b/language.go @@ -145,27 +145,6 @@ func AllLanguagesWithLatinScript() []Language { return allLanguagesWithScript(latin) } -// GetLanguageFromIsoCode639_1 returns the language for the given -// ISO 639-1 code. -func GetLanguageFromIsoCode639_1(isoCode IsoCode639_1) Language { - for _, language := range AllLanguages() { - if language.IsoCode639_1() == isoCode { - return language - } - } - return -1 -} - -// GetLanguageFromIsoCode639_3 returns the language for the given -// ISO 639-3 code. -func GetLanguageFromIsoCode639_3(isoCode IsoCode639_3) Language { - for _, language := range AllLanguages() { - if language.IsoCode639_3() == isoCode { - return language - } - } - return -1 -} func allLanguagesWithScript(script alphabet) (languages []Language) { for _, language := range AllLanguages() { diff --git a/mapping.go b/mapping.go new file mode 100644 index 0000000..33e8293 --- /dev/null +++ b/mapping.go @@ -0,0 +1,355 @@ +package lingua + +var stringToIsoCode639_1 = map[string]IsoCode639_1 { + "AF": AF, + "AR": AR, + "AZ": AZ, + "BE": BE, + "BG": BG, + "BN": BN, + "BS": BS, + "CA": CA, + "CS": CS, + "CY": CY, + "DA": DA, + "DE": DE, + "EL": EL, + "EN": EN, + "EO": EO, + "ES": ES, + "ET": ET, + "EU": EU, + "FA": FA, + "FI": FI, + "FR": FR, + "GA": GA, + "GU": GU, + "HE": HE, + "HI": HI, + "HR": HR, + "HU": HU, + "HY": HY, + "ID": ID, + "IS": IS, + "IT": IT, + "JA": JA, + "KA": KA, + "KK": KK, + "KO": KO, + "LA": LA, + "LG": LG, + "LT": LT, + "LV": LV, + "MI": MI, + "MK": MK, + "MN": MN, + "MR": MR, + "MS": MS, + "NB": NB, + "NL": NL, + "NN": NN, + "PA": PA, + "PL": PL, + "PT": PT, + "RO": RO, + "RU": RU, + "SK": SK, + "SL": SL, + "SN": SN, + "SO": SO, + "SQ": SQ, + "SR": SR, + "ST": ST, + "SV": SV, + "SW": SW, + "TA": TA, + "TE": TE, + "TH": TH, + "TL": TL, + "TN": TN, + "TR": TR, + "TS": TS, + "UK": UK, + "UR": UR, + "VI": VI, + "XH": XH, + "YO": YO, + "ZH": ZH, + "ZU": ZU, +} + + +var isoCode639_1ToLanguage = map[IsoCode639_1]Language { + AF: Afrikaans, + AR: Arabic, + AZ: Azerbaijani, + BE: Belarusian, + BG: Bulgarian, + BN: Bengali, + BS: Bosnian, + CA: Catalan, + CS: Czech, + CY: Welsh, + DA: Danish, + DE: German, + EL: Greek, + EN: English, + EO: Esperanto, + ES: Spanish, + ET: Estonian, + EU: Basque, + FA: Persian, + FI: Finnish, + FR: French, + GA: Irish, + GU: Gujarati, + HE: Hebrew, + HI: Hindi, + HR: Croatian, + HU: Hungarian, + HY: Armenian, + ID: Indonesian, + IS: Icelandic, + IT: Italian, + JA: Japanese, + KA: Georgian, + KK: Kazakh, + KO: Korean, + LA: Latin, + LG: Ganda, + LT: Lithuanian, + LV: Latvian, + MI: Maori, + MK: Macedonian, + MN: Mongolian, + MR: Marathi, + MS: Malay, + NB: Bokmal, + NL: Dutch, + NN: Nynorsk, + PA: Punjabi, + PL: Polish, + PT: Portuguese, + RO: Romanian, + RU: Russian, + SK: Slovak, + SL: Slovene, + SN: Shona, + SO: Somali, + SQ: Albanian, + SR: Serbian, + ST: Sotho, + SV: Swedish, + SW: Swahili, + TA: Tamil, + TE: Telugu, + TH: Thai, + TL: Tagalog, + TN: Tswana, + TR: Turkish, + TS: Tsonga, + UK: Ukrainian, + UR: Urdu, + VI: Vietnamese, + XH: Xhosa, + YO: Yoruba, + ZH: Chinese, + ZU: Zulu, +} + +var stringToIsoCode639_3 = map[string]IsoCode639_3 { + "AFR": AFR, + "ARA": ARA, + "AZE": AZE, + "BEL": BEL, + "BEN": BEN, + "BOS": BOS, + "BUL": BUL, + "CAT": CAT, + "CES": CES, + "CYM": CYM, + "DAN": DAN, + "DEU": DEU, + "ELL": ELL, + "ENG": ENG, + "EPO": EPO, + "EST": EST, + "EUS": EUS, + "FAS": FAS, + "FIN": FIN, + "FRA": FRA, + "GLE": GLE, + "GUJ": GUJ, + "HEB": HEB, + "HIN": HIN, + "HRV": HRV, + "HUN": HUN, + "HYE": HYE, + "IND": IND, + "ISL": ISL, + "ITA": ITA, + "JPN": JPN, + "KAT": KAT, + "KAZ": KAZ, + "KOR": KOR, + "LAT": LAT, + "LAV": LAV, + "LIT": LIT, + "LUG": LUG, + "MAR": MAR, + "MKD": MKD, + "MON": MON, + "MRI": MRI, + "MSA": MSA, + "NLD": NLD, + "NNO": NNO, + "NOB": NOB, + "PAN": PAN, + "POL": POL, + "POR": POR, + "RON": RON, + "RUS": RUS, + "SLK": SLK, + "SLV": SLV, + "SNA": SNA, + "SOM": SOM, + "SOT": SOT, + "SPA": SPA, + "SQI": SQI, + "SRP": SRP, + "SWA": SWA, + "SWE": SWE, + "TAM": TAM, + "TEL": TEL, + "TGL": TGL, + "THA": THA, + "TSN": TSN, + "TSP": TSO, + "TUR": TUR, + "UKR": UKR, + "URD": URD, + "VIE": VIE, + "XHO": XHO, + "YOR": YOR, + "ZHO": ZHO, + "ZUL": ZUL, +} + +var isoCode639_3ToLanguage = map[IsoCode639_3]Language { + AFR: Afrikaans, + ARA: Arabic, + AZE: Azerbaijani, + BEL: Belarusian, + BEN: Bengali, + BOS: Bosnian, + BUL: Bulgarian, + CAT: Catalan, + CES: Czech, + CYM: Welsh, + DAN: Danish, + DEU: German, + ELL: Greek, + ENG: English, + EPO: Esperanto, + EST: Estonian, + EUS: Basque, + FAS: Persian, + FIN: Finnish, + FRA: French, + GLE: Irish, + GUJ: Gujarati, + HEB: Hebrew, + HIN: Hindi, + HRV: Croatian, + HUN: Hungarian, + HYE: Armenian, + IND: Indonesian, + ISL: Icelandic, + ITA: Italian, + JPN: Japanese, + KAT: Georgian, + KAZ: Kazakh, + KOR: Korean, + LAT: Latin, + LAV: Latvian, + LIT: Lithuanian, + LUG: Ganda, + MAR: Marathi, + MKD: Macedonian, + MON: Mongolian, + MRI: Maori, + MSA: Malay, + NLD: Dutch, + NNO: Nynorsk, + NOB: Bokmal, + PAN: Punjabi, + POL: Polish, + POR: Portuguese, + RON: Romanian, + RUS: Russian, + SLK: Slovak, + SLV: Slovene, + SNA: Shona, + SOM: Somali, + SOT: Sotho, + SPA: Spanish, + SQI: Albanian, + SRP: Serbian, + SWA: Swahili, + SWE: Swedish, + TAM: Tamil, + TEL: Telugu, + TGL: Tagalog, + THA: Thai, + TSN: Tswana, + TSO: Tsonga, + TUR: Turkish, + UKR: Ukrainian, + URD: Urdu, + VIE: Vietnamese, + XHO: Xhosa, + YOR: Yoruba, + ZHO: Chinese, + ZUL: Zulu, +} + +// GetLanguageFromIsoCode639_1 returns the language for the given +// ISO 639-1 code - string variant. +func GetLanguageFromIsoCode639_1String(code string) Language { + if val, ok := stringToIsoCode639_1[code]; ok { + if val, ok := isoCode639_1ToLanguage[val]; ok { + return val + } + } + return -1 +} + + +// GetLanguageFromIsoCode639_3 returns the language for the given +// ISO 639-3 code - string variant. +func GetLanguageFromIsoCode639_3String(code string) Language { + if val, ok := stringToIsoCode639_3[code]; ok { + if val, ok := isoCode639_3ToLanguage[val]; ok { + return val + } + } + return -1 +} + +// GetLanguageFromIsoCode639_1 returns the language for the given +// ISO 639-1 code. +func GetLanguageFromIsoCode639_1(isoCode IsoCode639_1) Language { + if val, ok := isoCode639_1ToLanguage[isoCode]; ok { + return val + } + return -1 +} + +// GetLanguageFromIsoCode639_3 returns the language for the given +// ISO 639-3 code. +func GetLanguageFromIsoCode639_3(isoCode IsoCode639_3) Language { + if val, ok := isoCode639_3ToLanguage[isoCode]; ok { + return val + } + return -1 +}