feat: implement languages endpoint and refactor language handling to use languages.json

This commit is contained in:
Jokob @NetAlertX
2026-02-28 01:51:12 +00:00
parent e57fd2e81e
commit 814ba02d1c
12 changed files with 377 additions and 115 deletions

View File

@@ -387,78 +387,15 @@ function getString(key) {
}
// -----------------------------------------------------------------------------
// Get current language ISO code
// below has to match exactly the values in /front/php/templates/language/lang.php & /front/js/common.js
// Get current language ISO code.
// The UI_LANG setting value is always in the form "Name (code)", e.g. "English (en_us)".
// Extracting the code with a regex means this function never needs updating when a
// new language is added — the single source of truth is languages.json.
function getLangCode() {
UI_LANG = getSetting("UI_LANG");
let lang_code = 'en_us';
switch (UI_LANG) {
case 'English (en_us)':
lang_code = 'en_us';
break;
case 'Spanish (es_es)':
lang_code = 'es_es';
break;
case 'German (de_de)':
lang_code = 'de_de';
break;
case 'Farsi (fa_fa)':
lang_code = 'fa_fa';
break;
case 'French (fr_fr)':
lang_code = 'fr_fr';
break;
case 'Norwegian (nb_no)':
lang_code = 'nb_no';
break;
case 'Polish (pl_pl)':
lang_code = 'pl_pl';
break;
case 'Portuguese (pt_br)':
lang_code = 'pt_br';
break;
case 'Portuguese (pt_pt)':
lang_code = 'pt_pt';
break;
case 'Turkish (tr_tr)':
lang_code = 'tr_tr';
break;
case 'Swedish (sv_sv)':
lang_code = 'sv_sv';
break;
case 'Italian (it_it)':
lang_code = 'it_it';
break;
case 'Japanese (ja_jp)':
lang_code = 'ja_jp';
break;
case 'Russian (ru_ru)':
lang_code = 'ru_ru';
break;
case 'Chinese (zh_cn)':
lang_code = 'zh_cn';
break;
case 'Czech (cs_cz)':
lang_code = 'cs_cz';
break;
case 'Arabic (ar_ar)':
lang_code = 'ar_ar';
break;
case 'Catalan (ca_ca)':
lang_code = 'ca_ca';
break;
case 'Ukrainian (uk_uk)':
lang_code = 'uk_ua';
break;
case 'Vietnamese (vi_vn)':
lang_code = 'vi_vn';
break;
}
return lang_code;
const match = (UI_LANG || '').match(/\(([a-z]{2}_[a-z]{2})\)\s*$/i);
return match ? match[1].toLowerCase() : 'en_us';
}
// -----------------------------------------------------------------------------

View File

@@ -12,11 +12,9 @@ var timerRefreshData = ''
var emptyArr = ['undefined', "", undefined, null, 'null'];
var UI_LANG = "English (en_us)";
const allLanguages = ["ar_ar","ca_ca","cs_cz","de_de",
"en_us","es_es","fa_fa","fr_fr",
"it_it","ja_jp","nb_no","pl_pl",
"pt_br","pt_pt","ru_ru","sv_sv",
"tr_tr","uk_ua","vi_vn","zh_cn"]; // needs to be same as in lang.php
// allLanguages is populated at init via fetchAllLanguages() from GET /languages.
// Do not hardcode this list — add new languages to languages.json instead.
let allLanguages = [];
var settingsJSON = {}
// NAX_CACHE_VERSION and CACHE_KEYS moved to cache.js
@@ -24,6 +22,25 @@ var settingsJSON = {}
// getCache, setCache, fetchJson, getAuthContext moved to cache.js
// -----------------------------------------------------------------------------
// Fetch the canonical language list from GET /languages and populate allLanguages.
// Must be called after the API token is available (e.g. alongside cacheStrings).
// -----------------------------------------------------------------------------
function fetchAllLanguages(apiToken) {
return fetch('/languages', {
headers: { 'Authorization': 'Bearer ' + apiToken }
})
.then(function(resp) { return resp.json(); })
.then(function(data) {
if (data && data.success && Array.isArray(data.languages)) {
allLanguages = data.languages.map(function(l) { return l.code; });
}
})
.catch(function(err) {
console.warn('[fetchAllLanguages] Failed to load language list:', err);
});
}
// -----------------------------------------------------------------------------
function setCookie (cookie, value, expirationMinutes='') {