2017-11-03 02:09:34 +02:00
|
|
|
const { sprintf } = require('sprintf-js');
|
2017-05-14 23:51:45 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const codeToLanguageE_ = {};
|
2019-07-29 15:43:53 +02:00
|
|
|
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';
|
2019-10-30 20:12:06 +02:00
|
|
|
codeToLanguageE_['bs'] = 'Bosnian';
|
2019-07-29 15:43:53 +02:00
|
|
|
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';
|
|
|
|
codeToLanguageE_['nb'] = 'Norwegian';
|
|
|
|
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';
|
2017-07-18 20:04:47 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const codeToLanguage_ = {};
|
2019-07-29 15:43:53 +02:00
|
|
|
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'] = '日本語';
|
2020-04-05 22:14:31 +02:00
|
|
|
codeToLanguage_['ko'] = '한국어';
|
2019-07-29 15:43:53 +02:00
|
|
|
codeToLanguage_['sv'] = 'Svenska';
|
2019-11-12 18:01:56 +02:00
|
|
|
codeToLanguage_['el'] = 'Ελληνικά';
|
2019-07-29 15:43:53 +02:00
|
|
|
codeToLanguage_['zh'] = '中文';
|
|
|
|
codeToLanguage_['ro'] = 'Română';
|
|
|
|
codeToLanguage_['et'] = 'Eesti Keel';
|
|
|
|
codeToLanguage_['vi'] = 'Tiếng Việt';
|
|
|
|
codeToLanguage_['hu'] = 'Magyar';
|
2017-07-18 20:04:47 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const codeToCountry_ = {};
|
2019-07-29 15:43:53 +02:00
|
|
|
codeToCountry_['BR'] = 'Brasil';
|
|
|
|
codeToCountry_['CR'] = 'Costa Rica';
|
|
|
|
codeToCountry_['CN'] = '中国';
|
|
|
|
codeToCountry_['GB'] = 'UK';
|
|
|
|
codeToCountry_['US'] = 'US';
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
let supportedLocales_ = null;
|
2019-07-29 11:47:50 +02:00
|
|
|
let localeStats_ = null;
|
2017-07-18 20:04:47 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const loadedLocales_ = {};
|
2017-07-18 20:04:47 +02:00
|
|
|
|
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
|
|
|
|
2019-07-29 11:47:50 +02:00
|
|
|
function localeStats() {
|
|
|
|
if (!localeStats_) localeStats_ = require('../locales/index.js').stats;
|
|
|
|
return localeStats_;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = [];
|
|
|
|
for (const n in supportedLocales_) {
|
2017-07-19 23:26:30 +02:00
|
|
|
if (!supportedLocales_.hasOwnProperty(n)) continue;
|
|
|
|
output.push(n);
|
|
|
|
}
|
|
|
|
return output;
|
2017-07-18 20:04:47 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 11:47:50 +02:00
|
|
|
function supportedLocalesToLanguages(options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
const stats = localeStats();
|
2017-07-24 23:29:40 +02:00
|
|
|
const locales = supportedLocales();
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = {};
|
2017-07-24 23:29:40 +02:00
|
|
|
for (let i = 0; i < locales.length; i++) {
|
|
|
|
const locale = locales[i];
|
|
|
|
output[locale] = countryDisplayName(locale);
|
2019-07-29 11:47:50 +02:00
|
|
|
|
|
|
|
const stat = stats[locale];
|
|
|
|
if (options.includeStats && stat) {
|
2019-09-19 23:51:18 +02:00
|
|
|
output[locale] += ` (${stat.percentDone}%)`;
|
2019-07-29 11:47:50 +02:00
|
|
|
}
|
2017-07-24 23:29:40 +02:00
|
|
|
}
|
|
|
|
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];
|
2019-07-29 15:43:53 +02:00
|
|
|
if (defaultToEnglish) return languageNameInEnglish(languageCode);
|
2017-07-18 20:04:47 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function languageCodeOnly(canonicalName) {
|
|
|
|
if (canonicalName.length < 2) return canonicalName;
|
|
|
|
return canonicalName.substr(0, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function countryCodeOnly(canonicalName) {
|
2019-07-29 15:43:53 +02:00
|
|
|
if (canonicalName.length <= 2) return '';
|
2017-07-18 20:04:47 +02:00
|
|
|
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) {
|
2019-07-29 15:43:53 +02:00
|
|
|
if (languageCode == 'zh' && countryCode == 'CN') {
|
|
|
|
extraString = '简体'; // "Simplified" in "Simplified Chinese"
|
2017-07-18 20:04:47 +02:00
|
|
|
} else {
|
|
|
|
extraString = countryName(countryCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
if (languageCode == 'zh' && (countryCode == '' || countryCode == 'TW')) extraString = '繁體'; // "Traditional" in "Traditional Chinese"
|
2017-07-18 20:04:47 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
if (extraString) output += ` (${extraString})`;
|
2017-07-18 20:04:47 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function localeStrings(canonicalName) {
|
|
|
|
const locale = closestSupportedLocale(canonicalName);
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2017-07-18 20:04:47 +02:00
|
|
|
if (loadedLocales_[locale]) return loadedLocales_[locale];
|
|
|
|
|
2019-07-29 15:43:53 +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) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const strings = localeStrings(currentLocale_);
|
2017-07-18 20:04:47 +02:00
|
|
|
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) {
|
2019-09-19 23:51:18 +02:00
|
|
|
return `${result} ${args.join(', ')} (Translation error: ${error.message})`;
|
2018-02-10 14:43:45 +02:00
|
|
|
}
|
2017-07-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2019-12-10 23:10:47 +02:00
|
|
|
function _n(singular, plural, n, ...args) {
|
|
|
|
if (n > 1) return _(plural, ...args);
|
|
|
|
return _(singular, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { _, _n, supportedLocales, countryDisplayName, localeStrings, setLocale, supportedLocalesToLanguages, defaultLocale, closestSupportedLocale, languageCode, countryCodeOnly };
|