Refactor i18n language loading

This commit is contained in:
Lord-Grey 2021-05-10 22:52:06 +02:00
parent 1311fcdada
commit 3cf9d2d597
5 changed files with 1127 additions and 1134 deletions

View File

@ -5,8 +5,8 @@
"InfoDialog_changePassword_title": "Change Password", "InfoDialog_changePassword_title": "Change Password",
"InfoDialog_iswitch_text": "If you run Hyperion more than once in your local network, you could switch between the web configurations. Select the Hyperion instance below and switch!", "InfoDialog_iswitch_text": "If you run Hyperion more than once in your local network, you could switch between the web configurations. Select the Hyperion instance below and switch!",
"InfoDialog_iswitch_title": "Hyperion switcher", "InfoDialog_iswitch_title": "Hyperion switcher",
"InfoDialog_lang_text": "If you don't like the result of the automatic language detection you could overwrite it here.", "InfoDialog_nostorage_text": "Your browser does not support localStorage. You cannot save a specific language setting (fallback to 'auto detection') and access level (fallback to 'default'). Some wizards may be hidden. You could still use the webinterface without further issues",
"InfoDialog_lang_title": "Language setting", "InfoDialog_nostorage_title": "Cannot store settings",
"InfoDialog_nowrite_foottext": "The WebUI will be unlocked automatically after you solved the problem!", "InfoDialog_nowrite_foottext": "The WebUI will be unlocked automatically after you solved the problem!",
"InfoDialog_nowrite_text": "Hyperion can't write to your current loaded configuration file. Please repair the file permissions to proceed.", "InfoDialog_nowrite_text": "Hyperion can't write to your current loaded configuration file. Please repair the file permissions to proceed.",
"InfoDialog_nowrite_title": "write permission error!", "InfoDialog_nowrite_title": "write permission error!",

View File

@ -51,6 +51,8 @@
<script src="js/lib/jquery.i18n/jquery.i18n.language.js"></script> <script src="js/lib/jquery.i18n/jquery.i18n.language.js"></script>
<script src="js/lib/jquery.i18n/CLDRPluralRuleParser.js"></script> <script src="js/lib/jquery.i18n/CLDRPluralRuleParser.js"></script>
<script src="js/languages.js"></script>
<!-- Bootstrap Core CSS --> <!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/bootstrap.min.css" rel="stylesheet">
@ -82,7 +84,6 @@
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]--> <![endif]-->
</head> </head>
<body> <body>
<noscript> <noscript>
<div style="color:red;margin: 40px 0;text-align:center"> <div style="color:red;margin: 40px 0;text-align:center">

View File

@ -0,0 +1,33 @@
var storedLang;
var availLang = ['cs', 'de', 'en', 'es', 'fr', 'it', 'nl', 'nb', 'pl', 'pt', 'ro', 'sv', 'vi', 'ru', 'tr', 'zh-CN'];
var availLangText = ['Čeština', 'Deutsch', 'English', 'Español', 'Français', 'Italiano', 'Nederlands', 'Norsk Bokmål', 'Polski', 'Português', 'Română', 'Svenska', 'Tiếng Việt', 'русский', 'Türkçe', '汉语'];
//$.i18n.debug = true;
//i18n
function initTrans(lc) {
$.i18n().load("i18n", lc).done(
function () {
$.i18n().locale = lc;
performTranslation();
});
}
storedLang = getStorage("langcode");
if (storedLang == null || storedLang === "undefined") {
var langLocale = $.i18n().locale.substring(0, 2);
//Test, if language is supported by hyperion
var langIdx = availLang.indexOf(langLocale);
if (langIdx === -1) {
// If language is not supported by hyperion, try fallback language
langLocale = $.i18n().options.fallbackLocale.substring(0, 2);
langIdx = availLang.indexOf(langLocale);
if (langIdx === -1) {
langLocale = 'en';
}
}
storedLang = langLocale;
setStorage("langcode", storedLang);
}
initTrans(storedLang);

View File

@ -1,10 +1,5 @@
var storedAccess;
var storedLang;
var availLang = ['cs', 'de', 'en', 'es', 'fr', 'it', 'nl', 'nb', 'pl', 'pt', 'ro', 'sv', 'vi', 'ru', 'tr', 'zh-CN'];
var availLangText = ['Čeština', 'Deutsch', 'English', 'Español', 'Français', 'Italiano', 'Nederlands', 'Norsk Bokmål', 'Polski', 'Português', 'Română', 'Svenska', 'Tiếng Việt', 'русский', 'Türkçe', '汉语'];
var availAccess = ['default', 'advanced', 'expert']; var availAccess = ['default', 'advanced', 'expert'];
var storedAccess;
//$.i18n.debug = true;
//Change Password //Change Password
function changePassword(){ function changePassword(){
@ -28,46 +23,9 @@ function changePassword(){
$(document).ready(function () { $(document).ready(function () {
//i18n if (!storageComp()) {
function initTrans(lc){
if (lc == 'auto')
{
$.i18n().load().done(
function() {
performTranslation();
});
}
else
{
$.i18n().locale = lc;
$.i18n().load( "i18n", lc ).done(
function() {
performTranslation();
});
}
}
if (storageComp())
{
storedLang = getStorage("langcode");
if (storedLang == null)
{
setStorage("langcode", 'auto');
storedLang = 'auto';
initTrans(storedLang);
}
else
{
initTrans(storedLang);
}
}
else
{
showInfoDialog('warning', "Can't store settings", "Your browser doesn't support localStorage. You can't save a specific language setting (fallback to 'auto detection') and access level (fallback to 'default'). Some wizards may be hidden. You could still use the webinterface without further issues"); showInfoDialog('warning', "Can't store settings", "Your browser doesn't support localStorage. You can't save a specific language setting (fallback to 'auto detection') and access level (fallback to 'default'). Some wizards may be hidden. You could still use the webinterface without further issues");
initTrans('auto'); $('#language-select').attr("disabled", true);
storedLang = 'auto';
storedAccess = "default";
$('#btn_setlang').attr("disabled", true);
$('#btn_setaccess').attr("disabled", true); $('#btn_setaccess').attr("disabled", true);
} }
@ -75,10 +33,14 @@ $(document).ready( function() {
//access //access
storedAccess = getStorage("accesslevel"); storedAccess = getStorage("accesslevel");
if (storedAccess == null) if (storedAccess == null) {
{
setStorage("accesslevel", "default");
storedAccess = "default"; storedAccess = "default";
setStorage("accesslevel", storedAccess);
}
if (!storageComp()) {
showInfoDialog('warning', $.i18n('InfoDialog_nostorage_title'), $.i18n('InfoDialog_nostorage_text'));
$('#btn_setlang').attr("disabled", true);
} }
$('#btn_setaccess').off().on('click',function() { $('#btn_setaccess').off().on('click',function() {

View File

@ -162,25 +162,22 @@ function initLanguageSelection() {
var langLocale = storedLang; var langLocale = storedLang;
// If no language has been set, resolve browser locale
if (langLocale === 'auto') {
langLocale = $.i18n().locale.substring(0, 2);
}
// Resolve text for language code
var langText = 'Please Select';
//Test, if language is supported by hyperion //Test, if language is supported by hyperion
var langIdx = availLang.indexOf(langLocale); var langIdx = availLang.indexOf(langLocale);
if (langIdx > -1) { if (langIdx > -1) {
langText = availLangText[langIdx]; langText = availLangText[langIdx];
} } else {
else {
// If language is not supported by hyperion, try fallback language // If language is not supported by hyperion, try fallback language
langLocale = $.i18n().options.fallbackLocale.substring(0, 2); langLocale = $.i18n().options.fallbackLocale.substring(0, 2);
langIdx = availLang.indexOf(langLocale); langIdx = availLang.indexOf(langLocale);
if (langIdx > -1) { if (langIdx > -1) {
langText = availLangText[langIdx]; langText = availLangText[langIdx];
} else {
langLocale = 'en';
langIdx = availLang.indexOf(langLocale);
if (langIdx > -1) {
langText = availLangText[langIdx];
}
} }
} }