From e799bd392062f5a6d9f366dadb8d27fcd4fe0ffa Mon Sep 17 00:00:00 2001 From: Yokayo <52032299+Yokayo@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:12:40 +0700 Subject: [PATCH] Fix getMissingTranslations() and change its behavior --- public/scripts/i18n.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/public/scripts/i18n.js b/public/scripts/i18n.js index cf5e0bd9a..649b17cdb 100644 --- a/public/scripts/i18n.js +++ b/public/scripts/i18n.js @@ -1,4 +1,5 @@ import { registerDebugFunction } from './power-user.js'; +import { updateSecretDisplay } from './secrets.js' const storageKey = 'language'; const overrideLanguage = localStorage.getItem(storageKey); @@ -12,12 +13,10 @@ const localeData = await getLocaleData(localeFile); * @returns {Promise>} Locale data */ async function getLocaleData(language) { - let supportedLang = langs.find(x => x.lang === language); - - if (!supportedLang) { - console.warn(`Unsupported language: ${language}`); - return {}; - } + let supportedLang = findLang(language); + if (!supportedLang) { + return {}; + } const data = await fetch(`./locales/${language}.json`).then(response => { console.log(`Loading locale data from ./locales/${language}.json`); @@ -30,11 +29,23 @@ async function getLocaleData(language) { return data; } +function findLang(language) { + var supportedLang = langs.find(x => x.lang === language); + + if (!supportedLang) { + console.warn(`Unsupported language: ${language}`); + } + return supportedLang; +} + async function getMissingTranslations() { const missingData = []; - - for (const language of langs) { - const localeData = await getLocaleData(language); + + // Determine locales to search for untranslated strings + const langsToProcess = localeFile == 'en' ? langs : [findLang(localeFile)]; + + for (const language of langsToProcess) { + const localeData = await getLocaleData(language.lang); $(document).find('[data-i18n]').each(function () { const keys = $(this).data('i18n').split(';'); // Multi-key entries are ; delimited for (const key of keys) { @@ -42,12 +53,12 @@ async function getMissingTranslations() { if (attributeMatch) { // attribute-tagged key const localizedValue = localeData?.[attributeMatch[2]]; if (!localizedValue) { - missingData.push({ key, language, value: $(this).attr(attributeMatch[1]) }); + missingData.push({ key, language: language.lang, value: $(this).attr(attributeMatch[1]) }); } } else { // No attribute tag, treat as 'text' const localizedValue = localeData?.[key]; if (!localizedValue) { - missingData.push({ key, language, value: $(this).text().trim() }); + missingData.push({ key, language: language.lang, value: $(this).text().trim() }); } } } @@ -130,6 +141,7 @@ function addLanguagesToDropdown() { export function initLocales() { applyLocale(); addLanguagesToDropdown(); + updateSecretDisplay(); $('#ui_language_select').on('change', async function () { const language = String($(this).val()); @@ -143,6 +155,6 @@ export function initLocales() { location.reload(); }); - registerDebugFunction('getMissingTranslations', 'Get missing translations', 'Detects missing localization data and dumps the data into the browser console.', getMissingTranslations); + registerDebugFunction('getMissingTranslations', 'Get missing translations', 'Detects missing localization data in the current locale and dumps the data into the browser console. If the current locale is English, searches all other locales.', getMissingTranslations); registerDebugFunction('applyLocale', 'Apply locale', 'Reapplies the currently selected locale to the page.', applyLocale); }