jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/globalize.js

268 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-03-05 11:51:19 -07:00
define(['connectionManager', 'userSettings', 'events'], function (connectionManager, userSettings, events) {
var allTranslations = {};
var currentCulture;
function getCurrentLocale() {
return currentCulture;
}
function getDefaultLanguage() {
var culture = document.documentElement.getAttribute('data-culture');
if (culture) {
return culture;
}
if (navigator.language) {
return navigator.language;
}
if (navigator.userLanguage) {
return navigator.userLanguage;
}
if (navigator.languages && navigator.languages.length) {
return navigator.languages[0];
}
return 'en-us';
}
function updateCurrentCulture() {
var culture;
try {
culture = userSettings.get('language');
} catch (err) {
}
culture = culture || getDefaultLanguage();
currentCulture = normalizeLocaleName(culture);
ensureTranslations(currentCulture);
}
function ensureTranslations(culture) {
for (var i in allTranslations) {
ensureTranslation(allTranslations[i], culture);
}
}
function ensureTranslation(translationInfo, culture) {
if (translationInfo.dictionaries[culture]) {
return Promise.resolve();
}
return loadTranslation(translationInfo.translations, culture).then(function (dictionary) {
translationInfo.dictionaries[culture] = dictionary;
});
}
function normalizeLocaleName(culture) {
culture = culture.replace('_', '-');
// If it's de-DE, convert to just de
var parts = culture.split('-');
if (parts.length == 2) {
if (parts[0].toLowerCase() == parts[1].toLowerCase()) {
culture = parts[0].toLowerCase();
}
}
2016-03-25 09:35:19 -07:00
var lower = culture.toLowerCase();
if (lower == 'ca-es') {
return 'ca';
}
2016-09-04 09:45:32 -07:00
// normalize Swedish
if (lower == 'sv-se') {
return 'sv';
}
2016-03-25 09:35:19 -07:00
return lower;
2016-03-05 11:51:19 -07:00
}
function getDictionary(module) {
if (!module) {
module = defaultModule();
}
var translations = allTranslations[module];
if (!translations) {
return {};
}
return translations.dictionaries[getCurrentLocale()];
}
2016-07-26 22:19:56 -07:00
function register(options) {
2016-03-05 11:51:19 -07:00
allTranslations[options.name] = {
2016-07-26 22:19:56 -07:00
translations: options.strings || options.translations,
2016-03-05 11:51:19 -07:00
dictionaries: {}
};
2016-07-26 22:19:56 -07:00
}
function loadStrings(options) {
2016-03-05 11:51:19 -07:00
var locale = getCurrentLocale();
2016-07-26 22:19:56 -07:00
if (typeof options === 'string') {
return ensureTranslation(allTranslations[options], locale);
} else {
register(options);
return ensureTranslation(allTranslations[options.name], locale);
}
2016-03-05 11:51:19 -07:00
}
var cacheParam = new Date().getTime();
function loadTranslation(translations, lang) {
lang = normalizeLocaleName(lang);
var filtered = translations.filter(function (t) {
return normalizeLocaleName(t.lang) == lang;
});
if (!filtered.length) {
filtered = translations.filter(function (t) {
return normalizeLocaleName(t.lang) == 'en-us';
});
}
return new Promise(function (resolve, reject) {
if (!filtered.length) {
resolve();
return;
}
var url = filtered[0].path;
url += url.indexOf('?') == -1 ? '?' : '&';
url += 'v=' + cacheParam;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
if (this.status < 400) {
resolve(JSON.parse(this.response));
2016-08-19 23:27:48 -07:00
} else {
resolve({});
2016-03-05 11:51:19 -07:00
}
};
xhr.onerror = function () {
resolve({});
};
xhr.send();
});
}
function translateKey(key) {
var parts = key.split('#');
var module;
if (parts.length > 1) {
module = parts[0];
key = parts[1];
}
return translateKeyFromModule(key, module);
}
function translateKeyFromModule(key, module) {
return getDictionary(module)[key] || key;
}
function replaceAll(str, find, replace) {
return str.split(find).join(replace);
}
function translate(key) {
var val = translateKey(key);
for (var i = 1; i < arguments.length; i++) {
val = replaceAll(val, '{' + (i - 1) + '}', arguments[i]);
}
return val;
}
function translateHtml(html, module) {
if (!module) {
module = defaultModule();
}
if (!module) {
throw new Error('module cannot be null or empty');
}
var startIndex = html.indexOf('${');
if (startIndex == -1) {
return html;
}
startIndex += 2;
var endIndex = html.indexOf('}', startIndex);
if (endIndex == -1) {
return html;
}
var key = html.substring(startIndex, endIndex);
var val = translateKeyFromModule(key, module);
html = html.replace('${' + key + '}', val);
return translateHtml(html, module);
}
var _defaultModule;
function defaultModule(val) {
if (val) {
_defaultModule = val;
}
return _defaultModule;
}
updateCurrentCulture();
events.on(connectionManager, 'localusersignedin', updateCurrentCulture);
events.on(userSettings, 'change', function (e, name) {
if (name == 'language') {
updateCurrentCulture();
}
});
return {
getString: translate,
translate: translate,
2016-03-05 12:07:58 -07:00
translateDocument: translateHtml,
2016-03-05 11:51:19 -07:00
translateHtml: translateHtml,
2016-07-26 22:19:56 -07:00
loadStrings: loadStrings,
2016-04-16 09:51:35 -07:00
defaultModule: defaultModule,
2016-07-26 22:19:56 -07:00
getCurrentLocale: getCurrentLocale,
register: register
2016-03-05 11:51:19 -07:00
};
});