2017-11-03 02:09:34 +02:00
|
|
|
const { sprintf } = require('sprintf-js');
|
2017-05-14 23:51:45 +02:00
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
let codeToLanguageE_ = {};
|
|
|
|
codeToLanguageE_["aa"] = "Afar";
|
|
|
|
codeToLanguageE_["ab"] = "Abkhazian";
|
|
|
|
codeToLanguageE_["af"] = "Afrikaans";
|
|
|
|
codeToLanguageE_["am"] = "Amharic";
|
|
|
|
codeToLanguageE_["an"] = "Aragonese";
|
|
|
|
codeToLanguageE_["ar"] = "Arabic";
|
|
|
|
codeToLanguageE_["as"] = "Assamese";
|
|
|
|
codeToLanguageE_["ay"] = "Aymara";
|
|
|
|
codeToLanguageE_["az"] = "Azerbaijani";
|
|
|
|
codeToLanguageE_["ba"] = "Bashkir";
|
|
|
|
codeToLanguageE_["be"] = "Byelorussian";
|
|
|
|
codeToLanguageE_["bg"] = "Bulgarian";
|
|
|
|
codeToLanguageE_["bh"] = "Bihari";
|
|
|
|
codeToLanguageE_["bi"] = "Bislama";
|
|
|
|
codeToLanguageE_["bn"] = "Bangla";
|
|
|
|
codeToLanguageE_["bo"] = "Tibetan";
|
|
|
|
codeToLanguageE_["br"] = "Breton";
|
|
|
|
codeToLanguageE_["ca"] = "Catalan";
|
|
|
|
codeToLanguageE_["co"] = "Corsican";
|
|
|
|
codeToLanguageE_["cs"] = "Czech";
|
|
|
|
codeToLanguageE_["cy"] = "Welsh";
|
|
|
|
codeToLanguageE_["da"] = "Danish";
|
|
|
|
codeToLanguageE_["de"] = "German";
|
|
|
|
codeToLanguageE_["dz"] = "Bhutani";
|
|
|
|
codeToLanguageE_["el"] = "Greek";
|
|
|
|
codeToLanguageE_["en"] = "English";
|
|
|
|
codeToLanguageE_["eo"] = "Esperanto";
|
|
|
|
codeToLanguageE_["es"] = "Spanish";
|
|
|
|
codeToLanguageE_["et"] = "Estonian";
|
|
|
|
codeToLanguageE_["eu"] = "Basque";
|
|
|
|
codeToLanguageE_["fa"] = "Persian";
|
|
|
|
codeToLanguageE_["fi"] = "Finnish";
|
|
|
|
codeToLanguageE_["fj"] = "Fiji";
|
|
|
|
codeToLanguageE_["fo"] = "Faroese";
|
|
|
|
codeToLanguageE_["fr"] = "French";
|
|
|
|
codeToLanguageE_["fy"] = "Frisian";
|
|
|
|
codeToLanguageE_["ga"] = "Irish";
|
|
|
|
codeToLanguageE_["gd"] = "Gaelic";
|
|
|
|
codeToLanguageE_["gl"] = "Galician";
|
|
|
|
codeToLanguageE_["gn"] = "Guarani";
|
|
|
|
codeToLanguageE_["gu"] = "Gujarati";
|
|
|
|
codeToLanguageE_["ha"] = "Hausa";
|
|
|
|
codeToLanguageE_["he"] = "Hebrew";
|
|
|
|
codeToLanguageE_["hi"] = "Hindi";
|
|
|
|
codeToLanguageE_["hr"] = "Croatian";
|
|
|
|
codeToLanguageE_["hu"] = "Hungarian";
|
|
|
|
codeToLanguageE_["hy"] = "Armenian";
|
|
|
|
codeToLanguageE_["ia"] = "Interlingua";
|
|
|
|
codeToLanguageE_["id"] = "Indonesian";
|
|
|
|
codeToLanguageE_["ie"] = "Interlingue";
|
|
|
|
codeToLanguageE_["ik"] = "Inupiak";
|
|
|
|
codeToLanguageE_["is"] = "Icelandic";
|
|
|
|
codeToLanguageE_["it"] = "Italian";
|
|
|
|
codeToLanguageE_["iu"] = "Inuktitut";
|
|
|
|
codeToLanguageE_["ja"] = "Japanese";
|
|
|
|
codeToLanguageE_["jw"] = "Javanese";
|
|
|
|
codeToLanguageE_["ka"] = "Georgian";
|
|
|
|
codeToLanguageE_["kk"] = "Kazakh";
|
|
|
|
codeToLanguageE_["kl"] = "Greenlandic";
|
|
|
|
codeToLanguageE_["km"] = "Cambodian";
|
|
|
|
codeToLanguageE_["kn"] = "Kannada";
|
|
|
|
codeToLanguageE_["ko"] = "Korean";
|
|
|
|
codeToLanguageE_["ks"] = "Kashmiri";
|
|
|
|
codeToLanguageE_["ku"] = "Kurdish";
|
|
|
|
codeToLanguageE_["ky"] = "Kirghiz";
|
|
|
|
codeToLanguageE_["la"] = "Latin";
|
|
|
|
codeToLanguageE_["ln"] = "Lingala";
|
|
|
|
codeToLanguageE_["lo"] = "Laothian";
|
|
|
|
codeToLanguageE_["lt"] = "Lithuanian";
|
|
|
|
codeToLanguageE_["lv"] = "Lettish";
|
|
|
|
codeToLanguageE_["mg"] = "Malagasy";
|
|
|
|
codeToLanguageE_["mi"] = "Maori";
|
|
|
|
codeToLanguageE_["mk"] = "Macedonian";
|
|
|
|
codeToLanguageE_["ml"] = "Malayalam";
|
|
|
|
codeToLanguageE_["mn"] = "Mongolian";
|
|
|
|
codeToLanguageE_["mo"] = "Moldavian";
|
|
|
|
codeToLanguageE_["mr"] = "Marathi";
|
|
|
|
codeToLanguageE_["ms"] = "Malay";
|
|
|
|
codeToLanguageE_["mt"] = "Maltese";
|
|
|
|
codeToLanguageE_["my"] = "Burmese";
|
|
|
|
codeToLanguageE_["na"] = "Nauru";
|
2018-12-06 23:59:08 +02:00
|
|
|
codeToLanguageE_["nb"] = "Norwegian";
|
2017-07-18 20:04:47 +02:00
|
|
|
codeToLanguageE_["ne"] = "Nepali";
|
|
|
|
codeToLanguageE_["nl"] = "Dutch";
|
|
|
|
codeToLanguageE_["no"] = "Norwegian";
|
|
|
|
codeToLanguageE_["oc"] = "Occitan";
|
|
|
|
codeToLanguageE_["om"] = "Oromo";
|
|
|
|
codeToLanguageE_["or"] = "Oriya";
|
|
|
|
codeToLanguageE_["pa"] = "Punjabi";
|
|
|
|
codeToLanguageE_["pl"] = "Polish";
|
|
|
|
codeToLanguageE_["ps"] = "Pushto";
|
|
|
|
codeToLanguageE_["pt"] = "Portuguese";
|
|
|
|
codeToLanguageE_["qu"] = "Quechua";
|
|
|
|
codeToLanguageE_["rm"] = "Rhaeto-Romance";
|
|
|
|
codeToLanguageE_["rn"] = "Kirundi";
|
|
|
|
codeToLanguageE_["ro"] = "Romanian";
|
|
|
|
codeToLanguageE_["ru"] = "Russian";
|
|
|
|
codeToLanguageE_["rw"] = "Kinyarwanda";
|
|
|
|
codeToLanguageE_["sa"] = "Sanskrit";
|
|
|
|
codeToLanguageE_["sd"] = "Sindhi";
|
|
|
|
codeToLanguageE_["sg"] = "Sangho";
|
|
|
|
codeToLanguageE_["sh"] = "Serbo-Croatian";
|
|
|
|
codeToLanguageE_["si"] = "Sinhalese";
|
|
|
|
codeToLanguageE_["sk"] = "Slovak";
|
|
|
|
codeToLanguageE_["sl"] = "Slovenian";
|
|
|
|
codeToLanguageE_["sm"] = "Samoan";
|
|
|
|
codeToLanguageE_["sn"] = "Shona";
|
|
|
|
codeToLanguageE_["so"] = "Somali";
|
|
|
|
codeToLanguageE_["sq"] = "Albanian";
|
|
|
|
codeToLanguageE_["sr"] = "Serbian";
|
|
|
|
codeToLanguageE_["ss"] = "Siswati";
|
|
|
|
codeToLanguageE_["st"] = "Sesotho";
|
|
|
|
codeToLanguageE_["su"] = "Sundanese";
|
|
|
|
codeToLanguageE_["sv"] = "Swedish";
|
|
|
|
codeToLanguageE_["sw"] = "Swahili";
|
|
|
|
codeToLanguageE_["ta"] = "Tamil";
|
|
|
|
codeToLanguageE_["te"] = "Telugu";
|
|
|
|
codeToLanguageE_["tg"] = "Tajik";
|
|
|
|
codeToLanguageE_["th"] = "Thai";
|
|
|
|
codeToLanguageE_["ti"] = "Tigrinya";
|
|
|
|
codeToLanguageE_["tk"] = "Turkmen";
|
|
|
|
codeToLanguageE_["tl"] = "Tagalog";
|
|
|
|
codeToLanguageE_["tn"] = "Setswana";
|
|
|
|
codeToLanguageE_["to"] = "Tonga";
|
|
|
|
codeToLanguageE_["tr"] = "Turkish";
|
|
|
|
codeToLanguageE_["ts"] = "Tsonga";
|
|
|
|
codeToLanguageE_["tt"] = "Tatar";
|
|
|
|
codeToLanguageE_["tw"] = "Twi";
|
|
|
|
codeToLanguageE_["ug"] = "Uighur";
|
|
|
|
codeToLanguageE_["uk"] = "Ukrainian";
|
|
|
|
codeToLanguageE_["ur"] = "Urdu";
|
|
|
|
codeToLanguageE_["uz"] = "Uzbek";
|
|
|
|
codeToLanguageE_["vi"] = "Vietnamese";
|
|
|
|
codeToLanguageE_["vo"] = "Volapuk";
|
|
|
|
codeToLanguageE_["wo"] = "Wolof";
|
|
|
|
codeToLanguageE_["xh"] = "Xhosa";
|
|
|
|
codeToLanguageE_["yi"] = "Yiddish";
|
|
|
|
codeToLanguageE_["yo"] = "Yoruba";
|
|
|
|
codeToLanguageE_["za"] = "Zhuang";
|
|
|
|
codeToLanguageE_["zh"] = "Chinese";
|
|
|
|
codeToLanguageE_["zu"] = "Zulu";
|
|
|
|
|
|
|
|
let codeToLanguage_ = {};
|
|
|
|
codeToLanguage_["an"] = "Aragonés";
|
|
|
|
codeToLanguage_["da"] = "Dansk";
|
|
|
|
codeToLanguage_["de"] = "Deutsch";
|
|
|
|
codeToLanguage_["en"] = "English";
|
|
|
|
codeToLanguage_["es"] = "Español";
|
|
|
|
codeToLanguage_["fr"] = "Français";
|
|
|
|
codeToLanguage_["he"] = "עיברית";
|
|
|
|
codeToLanguage_["it"] = "Italiano";
|
|
|
|
codeToLanguage_["lt"] = "Lietuvių kalba";
|
|
|
|
codeToLanguage_["nl"] = "Nederlands";
|
|
|
|
codeToLanguage_["pl"] = "Polski";
|
|
|
|
codeToLanguage_["pt"] = "Português";
|
|
|
|
codeToLanguage_["ru"] = "Русский";
|
|
|
|
codeToLanguage_["sk"] = "Slovenčina";
|
|
|
|
codeToLanguage_["sq"] = "Shqip";
|
|
|
|
codeToLanguage_["sr"] = "српски језик";
|
|
|
|
codeToLanguage_["tr"] = "Türkçe";
|
|
|
|
codeToLanguage_["ja"] = "日本語";
|
|
|
|
codeToLanguage_["ko"] = "한국말";
|
|
|
|
codeToLanguage_["sv"] = "Svenska";
|
|
|
|
codeToLanguage_["el"] = "ελληνικά";
|
|
|
|
codeToLanguage_["zh"] = "中文";
|
|
|
|
codeToLanguage_["ro"] = "Română";
|
|
|
|
codeToLanguage_["et"] = "Eesti Keel";
|
|
|
|
codeToLanguage_["vi"] = "Tiếng Việt";
|
|
|
|
codeToLanguage_["hu"] = "Magyar";
|
|
|
|
|
|
|
|
let codeToCountry_ = {};
|
|
|
|
codeToCountry_["BR"] = "Brasil";
|
2017-12-17 13:30:32 +02:00
|
|
|
codeToCountry_["CR"] = "Costa Rica";
|
2017-07-18 20:04:47 +02:00
|
|
|
codeToCountry_["CN"] = "中国";
|
2019-04-20 13:26:34 +02:00
|
|
|
codeToCountry_["GB"] = "UK";
|
|
|
|
codeToCountry_["US"] = "US";
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
let supportedLocales_ = null;
|
|
|
|
|
|
|
|
let loadedLocales_ = {};
|
|
|
|
|
2017-07-24 23:29:40 +02:00
|
|
|
const defaultLocale_ = 'en_GB';
|
|
|
|
|
|
|
|
let currentLocale_ = defaultLocale_;
|
|
|
|
|
|
|
|
function defaultLocale() {
|
|
|
|
return defaultLocale_;
|
|
|
|
}
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
function supportedLocales() {
|
2017-07-19 23:26:30 +02:00
|
|
|
if (!supportedLocales_) supportedLocales_ = require('../locales/index.js').locales;
|
|
|
|
|
|
|
|
let output = [];
|
|
|
|
for (let n in supportedLocales_) {
|
|
|
|
if (!supportedLocales_.hasOwnProperty(n)) continue;
|
|
|
|
output.push(n);
|
|
|
|
}
|
|
|
|
return output;
|
2017-07-18 20:04:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-24 23:29:40 +02:00
|
|
|
function supportedLocalesToLanguages() {
|
|
|
|
const locales = supportedLocales();
|
|
|
|
let output = {};
|
|
|
|
for (let i = 0; i < locales.length; i++) {
|
|
|
|
const locale = locales[i];
|
|
|
|
output[locale] = countryDisplayName(locale);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
function closestSupportedLocale(canonicalName, defaultToEnglish = true) {
|
|
|
|
const locales = supportedLocales();
|
|
|
|
if (locales.indexOf(canonicalName) >= 0) return canonicalName;
|
|
|
|
|
|
|
|
const requiredLanguage = languageCodeOnly(canonicalName).toLowerCase();
|
|
|
|
|
|
|
|
for (let i = 0; i < locales.length; i++) {
|
|
|
|
const locale = locales[i];
|
|
|
|
const language = locale.split('_')[0];
|
|
|
|
if (requiredLanguage == language) return locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultToEnglish ? 'en_GB' : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function countryName(countryCode) {
|
|
|
|
return codeToCountry_[countryCode] ? codeToCountry_[countryCode] : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function languageNameInEnglish(languageCode) {
|
|
|
|
return codeToLanguageE_[languageCode] ? codeToLanguageE_[languageCode] : '';
|
2017-05-09 21:17:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
function languageName(languageCode, defaultToEnglish = true) {
|
|
|
|
if (codeToLanguage_[languageCode]) return codeToLanguage_[languageCode];
|
|
|
|
if (defaultToEnglish) return languageNameInEnglish(languageCode)
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function languageCodeOnly(canonicalName) {
|
|
|
|
if (canonicalName.length < 2) return canonicalName;
|
|
|
|
return canonicalName.substr(0, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function countryCodeOnly(canonicalName) {
|
|
|
|
if (canonicalName.length <= 2) return "";
|
|
|
|
return canonicalName.substr(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
function countryDisplayName(canonicalName) {
|
|
|
|
const languageCode = languageCodeOnly(canonicalName);
|
|
|
|
const countryCode = countryCodeOnly(canonicalName);
|
|
|
|
|
|
|
|
let output = languageName(languageCode);
|
|
|
|
|
|
|
|
let extraString;
|
|
|
|
|
2018-02-10 14:43:45 +02:00
|
|
|
if (countryCode) {
|
2017-07-18 20:04:47 +02:00
|
|
|
if (languageCode == "zh" && countryCode == "CN") {
|
|
|
|
extraString = "简体"; // "Simplified" in "Simplified Chinese"
|
|
|
|
} else {
|
|
|
|
extraString = countryName(countryCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (languageCode == "zh" && (countryCode == "" || countryCode == "TW")) extraString = "繁體"; // "Traditional" in "Traditional Chinese"
|
|
|
|
|
2018-02-10 14:43:45 +02:00
|
|
|
if (extraString) output += " (" + extraString + ")";
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function localeStrings(canonicalName) {
|
|
|
|
const locale = closestSupportedLocale(canonicalName);
|
2017-07-17 22:26:19 +02:00
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
if (loadedLocales_[locale]) return loadedLocales_[locale];
|
|
|
|
|
2017-07-19 23:26:30 +02:00
|
|
|
loadedLocales_[locale] = Object.assign({}, supportedLocales_[locale]);
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
return loadedLocales_[locale];
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLocale(canonicalName) {
|
|
|
|
if (currentLocale_ == canonicalName) return;
|
|
|
|
currentLocale_ = closestSupportedLocale(canonicalName);
|
|
|
|
}
|
|
|
|
|
2017-07-28 00:59:34 +02:00
|
|
|
function languageCode() {
|
|
|
|
return languageCodeOnly(currentLocale_);
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
function _(s, ...args) {
|
|
|
|
let strings = localeStrings(currentLocale_);
|
|
|
|
let result = strings[s];
|
|
|
|
if (result === '' || result === undefined) result = s;
|
2018-02-10 14:43:45 +02:00
|
|
|
try {
|
|
|
|
return sprintf(result, ...args);
|
|
|
|
} catch (error) {
|
|
|
|
return result + ' ' + args.join(', ') + ' (Translation error: ' + error.message + ')';
|
|
|
|
}
|
2017-07-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 22:21:54 +02:00
|
|
|
module.exports = { _, supportedLocales, countryDisplayName, localeStrings, setLocale, supportedLocalesToLanguages, defaultLocale, closestSupportedLocale, languageCode, countryCodeOnly };
|