mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
use en-us as default translation if language or key is missing
This commit is contained in:
parent
b4d9b37cbd
commit
c1a8f91a2b
1 changed files with 10 additions and 43 deletions
|
@ -6,7 +6,6 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
var currentDateTimeCulture;
|
var currentDateTimeCulture;
|
||||||
|
|
||||||
function getCurrentLocale() {
|
function getCurrentLocale() {
|
||||||
|
|
||||||
return currentCulture;
|
return currentCulture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,9 +14,7 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDefaultLanguage() {
|
function getDefaultLanguage() {
|
||||||
|
|
||||||
var culture = document.documentElement.getAttribute('data-culture');
|
var culture = document.documentElement.getAttribute('data-culture');
|
||||||
|
|
||||||
if (culture) {
|
if (culture) {
|
||||||
return culture;
|
return culture;
|
||||||
}
|
}
|
||||||
|
@ -36,12 +33,11 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCurrentCulture() {
|
function updateCurrentCulture() {
|
||||||
|
|
||||||
var culture;
|
var culture;
|
||||||
try {
|
try {
|
||||||
culture = userSettings.language();
|
culture = userSettings.language();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('no language set in user settings');
|
||||||
}
|
}
|
||||||
culture = culture || getDefaultLanguage();
|
culture = culture || getDefaultLanguage();
|
||||||
|
|
||||||
|
@ -51,43 +47,37 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
try {
|
try {
|
||||||
dateTimeCulture = userSettings.dateTimeLocale();
|
dateTimeCulture = userSettings.dateTimeLocale();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('no date format set in user settings');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dateTimeCulture) {
|
if (dateTimeCulture) {
|
||||||
currentDateTimeCulture = normalizeLocaleName(dateTimeCulture);
|
currentDateTimeCulture = normalizeLocaleName(dateTimeCulture);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
currentDateTimeCulture = currentCulture;
|
currentDateTimeCulture = currentCulture;
|
||||||
}
|
}
|
||||||
|
|
||||||
ensureTranslations(currentCulture);
|
ensureTranslations(currentCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureTranslations(culture) {
|
function ensureTranslations(culture) {
|
||||||
|
|
||||||
for (var i in allTranslations) {
|
for (var i in allTranslations) {
|
||||||
ensureTranslation(allTranslations[i], culture);
|
ensureTranslation(allTranslations[i], culture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureTranslation(translationInfo, culture) {
|
function ensureTranslation(translationInfo, culture) {
|
||||||
|
|
||||||
if (translationInfo.dictionaries[culture]) {
|
if (translationInfo.dictionaries[culture]) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
return loadTranslation(translationInfo.translations, culture).then(function (dictionary) {
|
return loadTranslation(translationInfo.translations, culture).then(function (dictionary) {
|
||||||
|
|
||||||
translationInfo.dictionaries[culture] = dictionary;
|
translationInfo.dictionaries[culture] = dictionary;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeLocaleName(culture) {
|
function normalizeLocaleName(culture) {
|
||||||
|
|
||||||
culture = culture.replace('_', '-');
|
culture = culture.replace('_', '-');
|
||||||
|
|
||||||
// If it's de-DE, convert to just de
|
// convert de-DE to de
|
||||||
var parts = culture.split('-');
|
var parts = culture.split('-');
|
||||||
if (parts.length === 2) {
|
if (parts.length === 2) {
|
||||||
if (parts[0].toLowerCase() === parts[1].toLowerCase()) {
|
if (parts[0].toLowerCase() === parts[1].toLowerCase()) {
|
||||||
|
@ -96,7 +86,6 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
var lower = culture.toLowerCase();
|
var lower = culture.toLowerCase();
|
||||||
|
|
||||||
if (lower === 'ca-es') {
|
if (lower === 'ca-es') {
|
||||||
return 'ca';
|
return 'ca';
|
||||||
}
|
}
|
||||||
|
@ -109,23 +98,20 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
return lower;
|
return lower;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDictionary(module) {
|
function getDictionary(module, locale) {
|
||||||
|
|
||||||
if (!module) {
|
if (!module) {
|
||||||
module = defaultModule();
|
module = defaultModule();
|
||||||
}
|
}
|
||||||
|
|
||||||
var translations = allTranslations[module];
|
var translations = allTranslations[module];
|
||||||
|
|
||||||
if (!translations) {
|
if (!translations) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return translations.dictionaries[getCurrentLocale()];
|
return translations.dictionaries[locale];
|
||||||
}
|
}
|
||||||
|
|
||||||
function register(options) {
|
function register(options) {
|
||||||
|
|
||||||
allTranslations[options.name] = {
|
allTranslations[options.name] = {
|
||||||
translations: options.strings || options.translations,
|
translations: options.strings || options.translations,
|
||||||
dictionaries: {}
|
dictionaries: {}
|
||||||
|
@ -133,9 +119,7 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadStrings(options) {
|
function loadStrings(options) {
|
||||||
|
|
||||||
var locale = getCurrentLocale();
|
var locale = getCurrentLocale();
|
||||||
|
|
||||||
if (typeof options === 'string') {
|
if (typeof options === 'string') {
|
||||||
return ensureTranslation(allTranslations[options], locale);
|
return ensureTranslation(allTranslations[options], locale);
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,9 +130,7 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
|
|
||||||
var cacheParam = new Date().getTime();
|
var cacheParam = new Date().getTime();
|
||||||
function loadTranslation(translations, lang) {
|
function loadTranslation(translations, lang) {
|
||||||
|
|
||||||
lang = normalizeLocaleName(lang);
|
lang = normalizeLocaleName(lang);
|
||||||
|
|
||||||
var filtered = translations.filter(function (t) {
|
var filtered = translations.filter(function (t) {
|
||||||
return normalizeLocaleName(t.lang) === lang;
|
return normalizeLocaleName(t.lang) === lang;
|
||||||
});
|
});
|
||||||
|
@ -160,7 +142,6 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
|
|
||||||
if (!filtered.length) {
|
if (!filtered.length) {
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
|
@ -190,7 +171,6 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateKey(key) {
|
function translateKey(key) {
|
||||||
|
|
||||||
var parts = key.split('#');
|
var parts = key.split('#');
|
||||||
var module;
|
var module;
|
||||||
|
|
||||||
|
@ -203,53 +183,43 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateKeyFromModule(key, module) {
|
function translateKeyFromModule(key, module) {
|
||||||
|
var dictionary = getDictionary(module, getCurrentLocale());
|
||||||
var dictionary = getDictionary(module);
|
if (!dictionary || !dictionary[key]) {
|
||||||
|
dictionary = getDictionary(module, 'en-us');
|
||||||
|
}
|
||||||
if (!dictionary) {
|
if (!dictionary) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dictionary[key] || key;
|
return dictionary[key] || key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceAll(str, find, replace) {
|
function replaceAll(str, find, replace) {
|
||||||
|
|
||||||
return str.split(find).join(replace);
|
return str.split(find).join(replace);
|
||||||
}
|
}
|
||||||
|
|
||||||
function translate(key) {
|
function translate(key) {
|
||||||
|
|
||||||
var val = translateKey(key);
|
var val = translateKey(key);
|
||||||
|
|
||||||
for (var i = 1; i < arguments.length; i++) {
|
for (var i = 1; i < arguments.length; i++) {
|
||||||
|
|
||||||
val = replaceAll(val, '{' + (i - 1) + '}', arguments[i]);
|
val = replaceAll(val, '{' + (i - 1) + '}', arguments[i]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateHtml(html, module) {
|
function translateHtml(html, module) {
|
||||||
|
|
||||||
if (!module) {
|
if (!module) {
|
||||||
module = defaultModule();
|
module = defaultModule();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!module) {
|
if (!module) {
|
||||||
throw new Error('module cannot be null or empty');
|
throw new Error('module cannot be null or empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
var startIndex = html.indexOf('${');
|
var startIndex = html.indexOf('${');
|
||||||
|
|
||||||
if (startIndex === -1) {
|
if (startIndex === -1) {
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
startIndex += 2;
|
startIndex += 2;
|
||||||
var endIndex = html.indexOf('}', startIndex);
|
var endIndex = html.indexOf('}', startIndex);
|
||||||
|
|
||||||
if (endIndex === -1) {
|
if (endIndex === -1) {
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
@ -263,12 +233,9 @@ define(['connectionManager', 'userSettings', 'events'], function (connectionMana
|
||||||
|
|
||||||
var _defaultModule;
|
var _defaultModule;
|
||||||
function defaultModule(val) {
|
function defaultModule(val) {
|
||||||
|
|
||||||
if (val) {
|
if (val) {
|
||||||
|
|
||||||
_defaultModule = val;
|
_defaultModule = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _defaultModule;
|
return _defaultModule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue