fix: refactor language.cc implementation

fix #535
This commit is contained in:
xiaoyifang 2023-04-22 13:20:36 +08:00
parent 8c415cb9a6
commit 34652421be

View file

@ -11,26 +11,33 @@ namespace {
using std::map; using std::map;
struct LangData
{
QString english;
QString localized;
QString country;
};
struct Db struct Db
{ {
static Db const & instance(); static Db const & instance();
map< QString, QString > const & getIso2ToEnglish() const [[nodiscard]] QMap< QString, LangData > const & getIso2ToLangData() const
{ return iso2ToEnglish; } {
map< QString, QString > const & getIso2ToLocalized() const return iso2LangData;
{ return iso2ToLocalized; } }
map< QString, QString > const & getIso2ToCountry() const
{ return iso2ToCountry; }
private: private:
map< QString, QString > iso2ToEnglish, iso2ToLocalized, iso2ToCountry; QMap< QString, LangData > iso2LangData;
Db(); Db();
void addEntry( QString const & iso2, QString const & english, void addEntry( QString const & iso2,
QString const & english,
QString const & localized ); QString const & localized );
void addExtraCountry( QString const & iso2, QString const & country );
}; };
Db const & Db::instance() Db const & Db::instance()
@ -43,8 +50,17 @@ Db const & Db::instance()
void Db::addEntry( QString const & iso2, QString const & english, void Db::addEntry( QString const & iso2, QString const & english,
QString const & localized ) QString const & localized )
{ {
iso2ToEnglish[ iso2 ] = english; LangData lang_data;
iso2ToLocalized[ iso2 ] = localized; lang_data.english = english;
lang_data.localized = localized;
iso2LangData.insert( iso2, lang_data );
}
void Db::addExtraCountry( QString const & iso2, QString const & country ) {
if(!iso2LangData.contains( iso2 )) {
return;
}
iso2LangData[ iso2 ].country = country;
} }
Db::Db() Db::Db()
@ -238,129 +254,129 @@ Db::Db()
// Countries // Countries
iso2ToCountry[ "aa" ] = "et"; addExtraCountry( "aa", "et" );
iso2ToCountry[ "af" ] = "za"; addExtraCountry( "af", "za" );
iso2ToCountry[ "am" ] = "et"; addExtraCountry( "am", "et" );
iso2ToCountry[ "an" ] = "es"; addExtraCountry( "an", "es" );
iso2ToCountry[ "ar" ] = "ae"; addExtraCountry( "ar", "ae" );
iso2ToCountry[ "as" ] = "in"; addExtraCountry( "as", "in" );
iso2ToCountry[ "az" ] = "az"; addExtraCountry( "az", "az" );
iso2ToCountry[ "be" ] = "by"; addExtraCountry( "be", "by" );
iso2ToCountry[ "bg" ] = "bg"; addExtraCountry( "bg", "bg" );
iso2ToCountry[ "bn" ] = "bd"; addExtraCountry( "bn", "bd" );
iso2ToCountry[ "bo" ] = "cn"; addExtraCountry( "bo", "cn" );
iso2ToCountry[ "br" ] = "fr"; addExtraCountry( "br", "fr" );
iso2ToCountry[ "bs" ] = "ba"; addExtraCountry( "bs", "ba" );
iso2ToCountry[ "ca" ] = "ad"; addExtraCountry( "ca", "ad" );
iso2ToCountry[ "cs" ] = "cz"; addExtraCountry( "cs", "cz" );
iso2ToCountry[ "cy" ] = "gb"; addExtraCountry( "cy", "gb" );
iso2ToCountry[ "da" ] = "dk"; addExtraCountry( "da", "dk" );
iso2ToCountry[ "de" ] = "de"; addExtraCountry( "de", "de" );
iso2ToCountry[ "dz" ] = "bt"; addExtraCountry( "dz", "bt" );
iso2ToCountry[ "el" ] = "gr"; addExtraCountry( "el", "gr" );
iso2ToCountry[ "en" ] = "gb"; addExtraCountry( "en", "gb" );
iso2ToCountry[ "es" ] = "es"; addExtraCountry( "es", "es" );
iso2ToCountry[ "et" ] = "ee"; addExtraCountry( "et", "ee" );
iso2ToCountry[ "eu" ] = "es"; addExtraCountry( "eu", "es" );
iso2ToCountry[ "fa" ] = "ir"; addExtraCountry( "fa", "ir" );
iso2ToCountry[ "fi" ] = "fi"; addExtraCountry( "fi", "fi" );
iso2ToCountry[ "fo" ] = "fo"; addExtraCountry( "fo", "fo" );
iso2ToCountry[ "fr" ] = "fr"; addExtraCountry( "fr", "fr" );
iso2ToCountry[ "fy" ] = "nl"; addExtraCountry( "fy", "nl" );
iso2ToCountry[ "ga" ] = "ie"; addExtraCountry( "ga", "ie" );
iso2ToCountry[ "gd" ] = "gb"; addExtraCountry( "gd", "gb" );
iso2ToCountry[ "gl" ] = "es"; addExtraCountry( "gl", "es" );
iso2ToCountry[ "gu" ] = "in"; addExtraCountry( "gu", "in" );
iso2ToCountry[ "gv" ] = "gb"; addExtraCountry( "gv", "gb" );
iso2ToCountry[ "ha" ] = "ng"; addExtraCountry( "ha", "ng" );
iso2ToCountry[ "he" ] = "il"; addExtraCountry( "he", "il" );
iso2ToCountry[ "hi" ] = "in"; addExtraCountry( "hi", "in" );
iso2ToCountry[ "hr" ] = "hr"; addExtraCountry( "hr", "hr" );
iso2ToCountry[ "ht" ] = "ht"; addExtraCountry( "ht", "ht" );
iso2ToCountry[ "hu" ] = "hu"; addExtraCountry( "hu", "hu" );
iso2ToCountry[ "hy" ] = "am"; addExtraCountry( "hy", "am" );
iso2ToCountry[ "id" ] = "id"; addExtraCountry( "id", "id" );
iso2ToCountry[ "ig" ] = "ng"; addExtraCountry( "ig", "ng" );
iso2ToCountry[ "ik" ] = "ca"; addExtraCountry( "ik", "ca" );
iso2ToCountry[ "is" ] = "is"; addExtraCountry( "is", "is" );
iso2ToCountry[ "it" ] = "it"; addExtraCountry( "it", "it" );
iso2ToCountry[ "iu" ] = "ca"; addExtraCountry( "iu", "ca" );
iso2ToCountry[ "iw" ] = "il"; addExtraCountry( "iw", "il" );
iso2ToCountry[ "ja" ] = "jp"; addExtraCountry( "ja", "jp" );
iso2ToCountry[ "jb" ] = "jb"; addExtraCountry( "jb", "jb" );
iso2ToCountry[ "ka" ] = "ge"; addExtraCountry( "ka", "ge" );
iso2ToCountry[ "kk" ] = "kz"; addExtraCountry( "kk", "kz" );
iso2ToCountry[ "kl" ] = "gl"; addExtraCountry( "kl", "gl" );
iso2ToCountry[ "km" ] = "kh"; addExtraCountry( "km", "kh" );
iso2ToCountry[ "kn" ] = "in"; addExtraCountry( "kn", "in" );
iso2ToCountry[ "ko" ] = "kr"; addExtraCountry( "ko", "kr" );
iso2ToCountry[ "ku" ] = "tr"; addExtraCountry( "ku", "tr" );
iso2ToCountry[ "kw" ] = "gb"; addExtraCountry( "kw", "gb" );
iso2ToCountry[ "ky" ] = "kg"; addExtraCountry( "ky", "kg" );
iso2ToCountry[ "lg" ] = "ug"; addExtraCountry( "lg", "ug" );
iso2ToCountry[ "li" ] = "be"; addExtraCountry( "li", "be" );
iso2ToCountry[ "lo" ] = "la"; addExtraCountry( "lo", "la" );
iso2ToCountry[ "lt" ] = "lt"; addExtraCountry( "lt", "lt" );
iso2ToCountry[ "lv" ] = "lv"; addExtraCountry( "lv", "lv" );
iso2ToCountry[ "mg" ] = "mg"; addExtraCountry( "mg", "mg" );
iso2ToCountry[ "mi" ] = "nz"; addExtraCountry( "mi", "nz" );
iso2ToCountry[ "mk" ] = "mk"; addExtraCountry( "mk", "mk" );
iso2ToCountry[ "ml" ] = "in"; addExtraCountry( "ml", "in" );
iso2ToCountry[ "mn" ] = "mn"; addExtraCountry( "mn", "mn" );
iso2ToCountry[ "mr" ] = "in"; addExtraCountry( "mr", "in" );
iso2ToCountry[ "ms" ] = "my"; addExtraCountry( "ms", "my" );
iso2ToCountry[ "mt" ] = "mt"; addExtraCountry( "mt", "mt" );
iso2ToCountry[ "nb" ] = "no"; addExtraCountry( "nb", "no" );
iso2ToCountry[ "ne" ] = "np"; addExtraCountry( "ne", "np" );
iso2ToCountry[ "nl" ] = "nl"; addExtraCountry( "nl", "nl" );
iso2ToCountry[ "nn" ] = "no"; addExtraCountry( "nn", "no" );
iso2ToCountry[ "nr" ] = "za"; addExtraCountry( "nr", "za" );
iso2ToCountry[ "oc" ] = "fr"; addExtraCountry( "oc", "fr" );
iso2ToCountry[ "om" ] = "et"; addExtraCountry( "om", "et" );
iso2ToCountry[ "or" ] = "in"; addExtraCountry( "or", "in" );
iso2ToCountry[ "pa" ] = "pk"; addExtraCountry( "pa", "pk" );
iso2ToCountry[ "pl" ] = "pl"; addExtraCountry( "pl", "pl" );
iso2ToCountry[ "pt" ] = "pt"; addExtraCountry( "pt", "pt" );
iso2ToCountry[ "ro" ] = "ro"; addExtraCountry( "ro", "ro" );
iso2ToCountry[ "ru" ] = "ru"; addExtraCountry( "ru", "ru" );
iso2ToCountry[ "rw" ] = "rw"; addExtraCountry( "rw", "rw" );
iso2ToCountry[ "sa" ] = "in"; addExtraCountry( "sa", "in" );
iso2ToCountry[ "sc" ] = "it"; addExtraCountry( "sc", "it" );
iso2ToCountry[ "sd" ] = "in"; addExtraCountry( "sd", "in" );
iso2ToCountry[ "se" ] = "no"; addExtraCountry( "se", "no" );
iso2ToCountry[ "si" ] = "lk"; addExtraCountry( "si", "lk" );
iso2ToCountry[ "sk" ] = "sk"; addExtraCountry( "sk", "sk" );
iso2ToCountry[ "sl" ] = "si"; addExtraCountry( "sl", "si" );
iso2ToCountry[ "so" ] = "so"; addExtraCountry( "so", "so" );
iso2ToCountry[ "sq" ] = "al"; addExtraCountry( "sq", "al" );
iso2ToCountry[ "sr" ] = "rs"; addExtraCountry( "sr", "rs" );
iso2ToCountry[ "ss" ] = "za"; addExtraCountry( "ss", "za" );
iso2ToCountry[ "st" ] = "za"; addExtraCountry( "st", "za" );
iso2ToCountry[ "sv" ] = "se"; addExtraCountry( "sv", "se" );
iso2ToCountry[ "ta" ] = "in"; addExtraCountry( "ta", "in" );
iso2ToCountry[ "te" ] = "in"; addExtraCountry( "te", "in" );
iso2ToCountry[ "tg" ] = "tj"; addExtraCountry( "tg", "tj" );
iso2ToCountry[ "th" ] = "th"; addExtraCountry( "th", "th" );
iso2ToCountry[ "ti" ] = "er"; addExtraCountry( "ti", "er" );
iso2ToCountry[ "tk" ] = "tm"; addExtraCountry( "tk", "tm" );
iso2ToCountry[ "tl" ] = "ph"; addExtraCountry( "tl", "ph" );
iso2ToCountry[ "tn" ] = "za"; addExtraCountry( "tn", "za" );
iso2ToCountry[ "tr" ] = "tr"; addExtraCountry( "tr", "tr" );
iso2ToCountry[ "ts" ] = "za"; addExtraCountry( "ts", "za" );
iso2ToCountry[ "tt" ] = "ru"; addExtraCountry( "tt", "ru" );
iso2ToCountry[ "ug" ] = "cn"; addExtraCountry( "ug", "cn" );
iso2ToCountry[ "uk" ] = "ua"; addExtraCountry( "uk", "ua" );
iso2ToCountry[ "ur" ] = "pk"; addExtraCountry( "ur", "pk" );
iso2ToCountry[ "uz" ] = "uz"; addExtraCountry( "uz", "uz" );
iso2ToCountry[ "ve" ] = "za"; addExtraCountry( "ve", "za" );
iso2ToCountry[ "vi" ] = "vn"; addExtraCountry( "vi", "vn" );
iso2ToCountry[ "wa" ] = "be"; addExtraCountry( "wa", "be" );
iso2ToCountry[ "wo" ] = "sn"; addExtraCountry( "wo", "sn" );
iso2ToCountry[ "xh" ] = "za"; addExtraCountry( "xh", "za" );
iso2ToCountry[ "yi" ] = "us"; addExtraCountry( "yi", "us" );
iso2ToCountry[ "yo" ] = "ng"; addExtraCountry( "yo", "ng" );
iso2ToCountry[ "zh" ] = "cn"; addExtraCountry( "zh", "cn" );
iso2ToCountry[ "zu" ] = "za"; addExtraCountry( "zu", "za" );
} }
} }
@ -409,12 +425,12 @@ QString englishNameForId( Id id )
return BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].englishName; return BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].englishName;
} }
const auto i = const auto i =
Db::instance().getIso2ToEnglish().find( LangCoder::intToCode2( id ) ); Db::instance().getIso2ToLangData().find( LangCoder::intToCode2( id ) );
if ( i == Db::instance().getIso2ToEnglish().end() ) if ( i == Db::instance().getIso2ToLangData().end() )
return {}; return {};
return i->second; return i->english;
} }
QString localizedNameForId( Id id ) QString localizedNameForId( Id id )
@ -423,13 +439,12 @@ QString localizedNameForId( Id id )
{ {
return QCoreApplication::translate( "Language", BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].localizedName ); return QCoreApplication::translate( "Language", BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].localizedName );
} }
const auto i = const auto i = Db::instance().getIso2ToLangData().find( LangCoder::intToCode2( id ) );
Db::instance().getIso2ToLocalized().find( LangCoder::intToCode2( id ) );
if ( i == Db::instance().getIso2ToLocalized().end() ) if ( i == Db::instance().getIso2ToLangData().end() )
return {}; return {};
return i->second; return i->localized;
} }
QString countryCodeForId( Id id ) QString countryCodeForId( Id id )
@ -439,12 +454,12 @@ QString countryCodeForId( Id id )
return BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].contryCode; return BabylonDb[ ( ( id >> 16 ) & 0x0f ) - 1 ].contryCode;
} }
const auto i = const auto i =
Db::instance().getIso2ToCountry().find( LangCoder::intToCode2( id ) ); Db::instance().getIso2ToLangData().find( LangCoder::intToCode2( id ) );
if ( i == Db::instance().getIso2ToCountry().end() ) if ( i == Db::instance().getIso2ToLangData().end() )
return {}; return {};
return i->second; return i->country;
} }
QString localizedStringForId( Id langId ) QString localizedStringForId( Id langId )
@ -458,7 +473,6 @@ QString localizedStringForId( Id langId )
if ( iconId.isEmpty() ) if ( iconId.isEmpty() )
return name; return name;
else
return QString( "<img src=\":/flags/%1.png\"> %2" ).arg( iconId, name ); return QString( "<img src=\":/flags/%1.png\"> %2" ).arg( iconId, name );
} }