mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
use shared globalize
This commit is contained in:
parent
3aefabc66b
commit
ec65069323
29 changed files with 390 additions and 274 deletions
|
@ -15,12 +15,12 @@
|
|||
},
|
||||
"devDependencies": {},
|
||||
"ignore": [],
|
||||
"version": "1.1.19",
|
||||
"_release": "1.1.19",
|
||||
"version": "1.1.22",
|
||||
"_release": "1.1.22",
|
||||
"_resolution": {
|
||||
"type": "version",
|
||||
"tag": "1.1.19",
|
||||
"commit": "703a21b89d8650d4520fb2f5f16d10161721c3e4"
|
||||
"tag": "1.1.22",
|
||||
"commit": "e24e9e35b215c2982601535ccb2496c962dfc1a9"
|
||||
},
|
||||
"_source": "git://github.com/MediaBrowser/emby-webcomponents.git",
|
||||
"_target": "~1.1.5",
|
||||
|
|
245
dashboard-ui/bower_components/emby-webcomponents/globalize.js
vendored
Normal file
245
dashboard-ui/bower_components/emby-webcomponents/globalize.js
vendored
Normal file
|
@ -0,0 +1,245 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
||||
return culture.toLowerCase();
|
||||
}
|
||||
|
||||
function getDictionary(module) {
|
||||
|
||||
if (!module) {
|
||||
module = defaultModule();
|
||||
}
|
||||
|
||||
var translations = allTranslations[module];
|
||||
|
||||
if (!translations) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return translations.dictionaries[getCurrentLocale()];
|
||||
}
|
||||
|
||||
function loadTranslations(options) {
|
||||
|
||||
allTranslations[options.name] = {
|
||||
translations: options.translations,
|
||||
dictionaries: {}
|
||||
};
|
||||
|
||||
var locale = getCurrentLocale();
|
||||
|
||||
return ensureTranslation(allTranslations[options.name], locale);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
resolve({});
|
||||
};
|
||||
|
||||
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,
|
||||
translateHtml: translateHtml,
|
||||
loadStrings: loadTranslations,
|
||||
defaultModule: defaultModule
|
||||
};
|
||||
});
|
|
@ -32,14 +32,14 @@
|
|||
"web-component-tester": "^4.0.0",
|
||||
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
|
||||
},
|
||||
"homepage": "https://github.com/PolymerElements/iron-icon",
|
||||
"homepage": "https://github.com/polymerelements/iron-icon",
|
||||
"_release": "1.0.8",
|
||||
"_resolution": {
|
||||
"type": "version",
|
||||
"tag": "v1.0.8",
|
||||
"commit": "f36b38928849ef3853db727faa8c9ef104d611eb"
|
||||
},
|
||||
"_source": "git://github.com/PolymerElements/iron-icon.git",
|
||||
"_source": "git://github.com/polymerelements/iron-icon.git",
|
||||
"_target": "^1.0.0",
|
||||
"_originalSource": "PolymerElements/iron-icon"
|
||||
"_originalSource": "polymerelements/iron-icon"
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "paper-input",
|
||||
"version": "1.1.8",
|
||||
"version": "1.1.9",
|
||||
"description": "Material design text fields",
|
||||
"authors": [
|
||||
"The Polymer Authors"
|
||||
|
@ -47,11 +47,11 @@
|
|||
"web-component-tester": "^4.0.0",
|
||||
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
|
||||
},
|
||||
"_release": "1.1.8",
|
||||
"_release": "1.1.9",
|
||||
"_resolution": {
|
||||
"type": "version",
|
||||
"tag": "v1.1.8",
|
||||
"commit": "96efaa0f707870d5a5999120467d67b8da806704"
|
||||
"tag": "v1.1.9",
|
||||
"commit": "4cb91098977573135b74121d6f4a2a301f44ce93"
|
||||
},
|
||||
"_source": "git://github.com/polymerelements/paper-input.git",
|
||||
"_target": "^1.0.9",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "paper-input",
|
||||
"version": "1.1.8",
|
||||
"version": "1.1.9",
|
||||
"description": "Material design text fields",
|
||||
"authors": [
|
||||
"The Polymer Authors"
|
||||
|
|
|
@ -31,10 +31,10 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
|
|||
/**
|
||||
* The function called by `<paper-input-container>` when the input value or validity changes.
|
||||
* @param {{
|
||||
* inputElement: (Node|undefined),
|
||||
* inputElement: (Element|undefined),
|
||||
* value: (string|undefined),
|
||||
* invalid: (boolean|undefined)
|
||||
* }} state All properties are optional -
|
||||
* invalid: boolean
|
||||
* }} state -
|
||||
* inputElement: The input element.
|
||||
* value: The input value.
|
||||
* invalid: True if the input value is invalid.
|
||||
|
|
|
@ -65,6 +65,17 @@ Custom property | Description | Default
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* This overrides the update function in PaperInputAddonBehavior.
|
||||
* @param {{
|
||||
* inputElement: (Element|undefined),
|
||||
* value: (string|undefined),
|
||||
* invalid: boolean
|
||||
* }} state -
|
||||
* inputElement: The input element.
|
||||
* value: The input value.
|
||||
* invalid: True if the input value is invalid.
|
||||
*/
|
||||
update: function(state) {
|
||||
if (!state.inputElement) {
|
||||
return;
|
||||
|
@ -72,7 +83,7 @@ Custom property | Description | Default
|
|||
|
||||
state.value = state.value || '';
|
||||
|
||||
var counter = state.value.length;
|
||||
var counter = state.value.length.toString();
|
||||
|
||||
if (state.inputElement.hasAttribute('maxlength')) {
|
||||
counter += '/' + state.inputElement.getAttribute('maxlength');
|
||||
|
|
|
@ -76,6 +76,17 @@ Custom property | Description | Default
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* This overrides the update function in PaperInputAddonBehavior.
|
||||
* @param {{
|
||||
* inputElement: (Element|undefined),
|
||||
* value: (string|undefined),
|
||||
* invalid: boolean
|
||||
* }} state -
|
||||
* inputElement: The input element.
|
||||
* value: The input value.
|
||||
* invalid: True if the input value is invalid.
|
||||
*/
|
||||
update: function(state) {
|
||||
this._setInvalid(state.invalid);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,6 @@
|
|||
"commit": "61fac558012d9ef56ea78ed5435de0c418a4afbb"
|
||||
},
|
||||
"_source": "git://github.com/Polymer/polymer.git",
|
||||
"_target": "^1.2.0",
|
||||
"_target": "^1.0.0",
|
||||
"_originalSource": "Polymer/polymer"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue