File indexing completed on 2024-12-22 04:40:14

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "languagecode.h"
0009 
0010 #include <QMap>
0011 #include <QLocale>
0012 
0013 #include <KLocalizedString>
0014 
0015 QString
0016 LanguageCode::toIso2(const QString &iso3Code)
0017 {
0018     static QMap<QString, QString> map;
0019     if(map.isEmpty()) {
0020         map["aar"] = "aa";
0021         map["abk"] = "ab";
0022         map["afr"] = "af";
0023         map["aka"] = "ak";
0024         map["alb"] = "sq";
0025         map["amh"] = "am";
0026         map["ara"] = "ar";
0027         map["arg"] = "an";
0028         map["arm"] = "hy";
0029         map["asm"] = "as";
0030         map["ava"] = "av";
0031         map["ave"] = "ae";
0032         map["aym"] = "ay";
0033         map["aze"] = "az";
0034         map["bak"] = "ba";
0035         map["bam"] = "bm";
0036         map["baq"] = "eu";
0037         map["bel"] = "be";
0038         map["ben"] = "bn";
0039         map["bih"] = "bh";
0040         map["bis"] = "bi";
0041         map["bos"] = "bs";
0042         map["bre"] = "br";
0043         map["bul"] = "bg";
0044         map["bur"] = "my";
0045         map["cat"] = "ca";
0046         map["cha"] = "ch";
0047         map["che"] = "ce";
0048         map["chi"] = "zh";
0049         map["chu"] = "cu";
0050         map["chv"] = "cv";
0051         map["cor"] = "kw";
0052         map["cos"] = "co";
0053         map["cre"] = "cr";
0054         map["cze"] = "cs";
0055         map["dan"] = "da";
0056         map["div"] = "dv";
0057         map["dut"] = "nl";
0058         map["dzo"] = "dz";
0059         map["eng"] = "en";
0060         map["epo"] = "eo";
0061         map["est"] = "et";
0062         map["ewe"] = "ee";
0063         map["fao"] = "fo";
0064         map["fij"] = "fj";
0065         map["fin"] = "fi";
0066         map["fre"] = "fr";
0067         map["fry"] = "fy";
0068         map["ful"] = "ff";
0069         map["geo"] = "ka";
0070         map["ger"] = "de";
0071         map["gla"] = "gd";
0072         map["gle"] = "ga";
0073         map["glg"] = "gl";
0074         map["glv"] = "gv";
0075         map["gre"] = "el";
0076         map["grn"] = "gn";
0077         map["guj"] = "gu";
0078         map["hat"] = "ht";
0079         map["hau"] = "ha";
0080         map["heb"] = "he";
0081         map["her"] = "hz";
0082         map["hin"] = "hi";
0083         map["hmo"] = "ho";
0084         map["hrv"] = "hr";
0085         map["hun"] = "hu";
0086         map["ibo"] = "ig";
0087         map["ice"] = "is";
0088         map["ido"] = "io";
0089         map["iii"] = "ii";
0090         map["iku"] = "iu";
0091         map["ile"] = "ie";
0092         map["ina"] = "ia";
0093         map["ind"] = "id";
0094         map["ipk"] = "ik";
0095         map["ita"] = "it";
0096         map["jav"] = "jv";
0097         map["jpn"] = "ja";
0098         map["kal"] = "kl";
0099         map["kan"] = "kn";
0100         map["kas"] = "ks";
0101         map["kau"] = "kr";
0102         map["kaz"] = "kk";
0103         map["khm"] = "km";
0104         map["kik"] = "ki";
0105         map["kin"] = "rw";
0106         map["kir"] = "ky";
0107         map["kom"] = "kv";
0108         map["kon"] = "kg";
0109         map["kor"] = "ko";
0110         map["kua"] = "kj";
0111         map["kur"] = "ku";
0112         map["lao"] = "lo";
0113         map["lat"] = "la";
0114         map["lav"] = "lv";
0115         map["lim"] = "li";
0116         map["lin"] = "ln";
0117         map["lit"] = "lt";
0118         map["ltz"] = "lb";
0119         map["lub"] = "lu";
0120         map["lug"] = "lg";
0121         map["mac"] = "mk";
0122         map["mah"] = "mh";
0123         map["mal"] = "ml";
0124         map["mao"] = "mi";
0125         map["mar"] = "mr";
0126         map["may"] = "ms";
0127         map["mlg"] = "mg";
0128         map["mlt"] = "mt";
0129         map["mon"] = "mn";
0130         map["nau"] = "na";
0131         map["nav"] = "nv";
0132         map["nbl"] = "nr";
0133         map["nde"] = "nd";
0134         map["ndo"] = "ng";
0135         map["nep"] = "ne";
0136         map["nno"] = "nn";
0137         map["nob"] = "nb";
0138         map["nor"] = "no";
0139         map["nya"] = "ny";
0140         map["oci"] = "oc";
0141         map["oji"] = "oj";
0142         map["ori"] = "or";
0143         map["orm"] = "om";
0144         map["oss"] = "os";
0145         map["pan"] = "pa";
0146         map["per"] = "fa";
0147         map["pli"] = "pi";
0148         map["pol"] = "pl";
0149         map["por"] = "pt";
0150         map["pus"] = "ps";
0151         map["que"] = "qu";
0152         map["roh"] = "rm";
0153         map["rum"] = "ro";
0154         map["run"] = "rn";
0155         map["rus"] = "ru";
0156         map["sag"] = "sg";
0157         map["san"] = "sa";
0158         map["sin"] = "si";
0159         map["slo"] = "sk";
0160         map["slv"] = "sl";
0161         map["sme"] = "se";
0162         map["smo"] = "sm";
0163         map["sna"] = "sn";
0164         map["snd"] = "sd";
0165         map["som"] = "so";
0166         map["sot"] = "st";
0167         map["spa"] = "es";
0168         map["srd"] = "sc";
0169         map["srp"] = "sr";
0170         map["ssw"] = "ss";
0171         map["sun"] = "su";
0172         map["swa"] = "sw";
0173         map["swe"] = "sv";
0174         map["tah"] = "ty";
0175         map["tam"] = "ta";
0176         map["tat"] = "tt";
0177         map["tel"] = "te";
0178         map["tgk"] = "tg";
0179         map["tgl"] = "tl";
0180         map["tha"] = "th";
0181         map["tib"] = "bo";
0182         map["tir"] = "ti";
0183         map["ton"] = "to";
0184         map["tsn"] = "tn";
0185         map["tso"] = "ts";
0186         map["tuk"] = "tk";
0187         map["tur"] = "tr";
0188         map["twi"] = "tw";
0189         map["uig"] = "ug";
0190         map["ukr"] = "uk";
0191         map["urd"] = "ur";
0192         map["uzb"] = "uz";
0193         map["ven"] = "ve";
0194         map["vie"] = "vi";
0195         map["vol"] = "vo";
0196         map["wel"] = "cy";
0197         map["wln"] = "wa";
0198         map["wol"] = "wo";
0199         map["xho"] = "xh";
0200         map["yid"] = "yi";
0201         map["yor"] = "yo";
0202         map["zha"] = "za";
0203         map["zul"] = "zu";
0204     }
0205 
0206     return map.contains(iso3Code) ? map[iso3Code] : QString();
0207 }
0208 
0209 QString
0210 LanguageCode::toIso3(const QString &iso2Code)
0211 {
0212     static QMap<QString, QString> map;
0213     if(map.isEmpty()) {
0214         map["aa"] = "aar";
0215         map["ab"] = "abk";
0216         map["ae"] = "ave";
0217         map["af"] = "afr";
0218         map["ak"] = "aka";
0219         map["am"] = "amh";
0220         map["an"] = "arg";
0221         map["ar"] = "ara";
0222         map["as"] = "asm";
0223         map["av"] = "ava";
0224         map["ay"] = "aym";
0225         map["az"] = "aze";
0226         map["ba"] = "bak";
0227         map["be"] = "bel";
0228         map["bg"] = "bul";
0229         map["bh"] = "bih";
0230         map["bi"] = "bis";
0231         map["bm"] = "bam";
0232         map["bn"] = "ben";
0233         map["bo"] = "tib";
0234         map["br"] = "bre";
0235         map["bs"] = "bos";
0236         map["ca"] = "cat";
0237         map["ce"] = "che";
0238         map["ch"] = "cha";
0239         map["co"] = "cos";
0240         map["cr"] = "cre";
0241         map["cs"] = "cze";
0242         map["cu"] = "chu";
0243         map["cv"] = "chv";
0244         map["cy"] = "wel";
0245         map["da"] = "dan";
0246         map["de"] = "ger";
0247         map["dv"] = "div";
0248         map["dz"] = "dzo";
0249         map["ee"] = "ewe";
0250         map["el"] = "gre";
0251         map["en"] = "eng";
0252         map["eo"] = "epo";
0253         map["es"] = "spa";
0254         map["et"] = "est";
0255         map["eu"] = "baq";
0256         map["fa"] = "per";
0257         map["ff"] = "ful";
0258         map["fi"] = "fin";
0259         map["fj"] = "fij";
0260         map["fo"] = "fao";
0261         map["fr"] = "fre";
0262         map["fy"] = "fry";
0263         map["ga"] = "gle";
0264         map["gd"] = "gla";
0265         map["gl"] = "glg";
0266         map["gn"] = "grn";
0267         map["gu"] = "guj";
0268         map["gv"] = "glv";
0269         map["ha"] = "hau";
0270         map["he"] = "heb";
0271         map["hi"] = "hin";
0272         map["ho"] = "hmo";
0273         map["hr"] = "hrv";
0274         map["ht"] = "hat";
0275         map["hu"] = "hun";
0276         map["hy"] = "arm";
0277         map["hz"] = "her";
0278         map["ia"] = "ina";
0279         map["id"] = "ind";
0280         map["ie"] = "ile";
0281         map["ig"] = "ibo";
0282         map["ii"] = "iii";
0283         map["ik"] = "ipk";
0284         map["io"] = "ido";
0285         map["is"] = "ice";
0286         map["it"] = "ita";
0287         map["iu"] = "iku";
0288         map["ja"] = "jpn";
0289         map["jv"] = "jav";
0290         map["ka"] = "geo";
0291         map["kg"] = "kon";
0292         map["ki"] = "kik";
0293         map["kj"] = "kua";
0294         map["kk"] = "kaz";
0295         map["kl"] = "kal";
0296         map["km"] = "khm";
0297         map["kn"] = "kan";
0298         map["ko"] = "kor";
0299         map["kr"] = "kau";
0300         map["ks"] = "kas";
0301         map["ku"] = "kur";
0302         map["kv"] = "kom";
0303         map["kw"] = "cor";
0304         map["ky"] = "kir";
0305         map["la"] = "lat";
0306         map["lb"] = "ltz";
0307         map["lg"] = "lug";
0308         map["li"] = "lim";
0309         map["ln"] = "lin";
0310         map["lo"] = "lao";
0311         map["lt"] = "lit";
0312         map["lu"] = "lub";
0313         map["lv"] = "lav";
0314         map["mg"] = "mlg";
0315         map["mh"] = "mah";
0316         map["mi"] = "mao";
0317         map["mk"] = "mac";
0318         map["ml"] = "mal";
0319         map["mn"] = "mon";
0320         map["mr"] = "mar";
0321         map["ms"] = "may";
0322         map["mt"] = "mlt";
0323         map["my"] = "bur";
0324         map["na"] = "nau";
0325         map["nb"] = "nob";
0326         map["nd"] = "nde";
0327         map["ne"] = "nep";
0328         map["ng"] = "ndo";
0329         map["nl"] = "dut";
0330         map["nn"] = "nno";
0331         map["no"] = "nor";
0332         map["nr"] = "nbl";
0333         map["nv"] = "nav";
0334         map["ny"] = "nya";
0335         map["oc"] = "oci";
0336         map["oj"] = "oji";
0337         map["om"] = "orm";
0338         map["or"] = "ori";
0339         map["os"] = "oss";
0340         map["pa"] = "pan";
0341         map["pi"] = "pli";
0342         map["pl"] = "pol";
0343         map["ps"] = "pus";
0344         map["pt"] = "por";
0345         map["qu"] = "que";
0346         map["rm"] = "roh";
0347         map["rn"] = "run";
0348         map["ro"] = "rum";
0349         map["ru"] = "rus";
0350         map["rw"] = "kin";
0351         map["sa"] = "san";
0352         map["sc"] = "srd";
0353         map["sd"] = "snd";
0354         map["se"] = "sme";
0355         map["sg"] = "sag";
0356         map["si"] = "sin";
0357         map["sk"] = "slo";
0358         map["sl"] = "slv";
0359         map["sm"] = "smo";
0360         map["sn"] = "sna";
0361         map["so"] = "som";
0362         map["sq"] = "alb";
0363         map["sr"] = "srp";
0364         map["ss"] = "ssw";
0365         map["st"] = "sot";
0366         map["su"] = "sun";
0367         map["sv"] = "swe";
0368         map["sw"] = "swa";
0369         map["ta"] = "tam";
0370         map["te"] = "tel";
0371         map["tg"] = "tgk";
0372         map["th"] = "tha";
0373         map["ti"] = "tir";
0374         map["tk"] = "tuk";
0375         map["tl"] = "tgl";
0376         map["tn"] = "tsn";
0377         map["to"] = "ton";
0378         map["tr"] = "tur";
0379         map["ts"] = "tso";
0380         map["tt"] = "tat";
0381         map["tw"] = "twi";
0382         map["ty"] = "tah";
0383         map["ug"] = "uig";
0384         map["uk"] = "ukr";
0385         map["ur"] = "urd";
0386         map["uz"] = "uzb";
0387         map["ve"] = "ven";
0388         map["vi"] = "vie";
0389         map["vo"] = "vol";
0390         map["wa"] = "wln";
0391         map["wo"] = "wol";
0392         map["xh"] = "xho";
0393         map["yi"] = "yid";
0394         map["yo"] = "yor";
0395         map["za"] = "zha";
0396         map["zh"] = "chi";
0397         map["zu"] = "zul";
0398     }
0399 
0400     return map.contains(iso2Code) ? map[iso2Code] : QString();
0401 }
0402 
0403 QString
0404 LanguageCode::nameFromIso2(const QString &iso2Code)
0405 {
0406     QString name = QLocale::languageToString(QLocale(iso2Code).language());
0407     return name.isEmpty() ? i18n("Unknown") : name;
0408 }
0409 
0410 QString
0411 LanguageCode::nameFromIso3(const QString &iso3Code)
0412 {
0413     return nameFromIso2(toIso2(iso3Code));
0414 }
0415 
0416 /*static*/ QString
0417 LanguageCode::nameFromIso(const QString &isoCode)
0418 {
0419     return nameFromIso2(isoCode.length() == 2 ? isoCode : toIso2(isoCode));
0420 }