This commit is contained in:
Mateusz Gruszczyński
2026-09-16 09:04:43 +02:00
parent 3d69e74319
commit 0437ef7b6f
28 changed files with 1254 additions and 221 deletions
+40 -15
View File
@@ -261,11 +261,36 @@ function applyTranslations() {
if (app.currentView === 'history' && app.zones.length) loadHistory();
}
function setLanguage(language) {
async function loadLanguagePack(language) {
if (app.translations[language]) return;
const item = app.languages.find(entry => entry.code === language);
if (!item) throw new Error(`Unknown language: ${language}`);
const response = await fetch(withBase(item.path || `/lang/${encodeURIComponent(item.code)}.json`));
if (!response.ok) throw new Error(`Language ${item.code} HTTP ${response.status}`);
const pack = await response.json();
app.translations[item.code] = pack.translations || {};
app.locales[item.code] = pack.meta?.locale || item.locale || item.code;
}
async function setLanguage(language) {
const available = app.languages.some(item => item.code === language);
app.language = available ? language : DEFAULT_LANGUAGE;
setCookie('gree_controller_language', app.language);
applyTranslations();
const nextLanguage = available ? language : DEFAULT_LANGUAGE;
const previousLanguage = app.language;
const select = $('#languageSelect');
if (nextLanguage === previousLanguage && app.translations[nextLanguage]) return;
if (select) select.disabled = true;
try {
await loadLanguagePack(nextLanguage);
app.language = nextLanguage;
setCookie('gree_controller_language', app.language);
applyTranslations();
} catch (error) {
console.error(`Unable to load language ${nextLanguage}:`, error);
if (select) select.value = previousLanguage;
toast(error.message, true);
} finally {
if (select) select.disabled = false;
}
}
function renderLanguageOptions() {
@@ -286,21 +311,21 @@ async function loadLanguages() {
const manifest = await response.json();
const languages = Array.isArray(manifest.languages) ? manifest.languages : [];
if (!languages.some(item => item.code === DEFAULT_LANGUAGE)) throw new Error('Default English language pack is missing');
const loaded = await Promise.all(languages.map(async item => {
const packResponse = await fetch(withBase(item.path || `/lang/${encodeURIComponent(item.code)}.json`));
if (!packResponse.ok) throw new Error(`Language ${item.code} HTTP ${packResponse.status}`);
const pack = await packResponse.json();
return { item, pack };
}));
app.languages = loaded.map(({ item }) => item);
app.translations = Object.fromEntries(loaded.map(({ item, pack }) => [item.code, pack.translations || {}]));
app.locales = Object.fromEntries(loaded.map(({ item, pack }) => [item.code, pack.meta?.locale || item.locale || item.code]));
app.languages = languages;
app.translations = {};
app.locales = Object.fromEntries(languages.map(item => [item.code, item.locale || item.code]));
app.language = app.languages.some(item => item.code === preferredLanguage)
? preferredLanguage
: (manifest.default || DEFAULT_LANGUAGE);
if (!app.languages.some(item => item.code === app.language)) app.language = DEFAULT_LANGUAGE;
try {
await loadLanguagePack(app.language);
} catch (error) {
if (app.language === DEFAULT_LANGUAGE) throw error;
console.error(`Unable to load preferred language ${app.language}:`, error);
app.language = DEFAULT_LANGUAGE;
await loadLanguagePack(DEFAULT_LANGUAGE);
}
renderLanguageOptions();
} catch (error) {
console.error('Unable to load language packs:', error);