Fix getMissingTranslations() and change its behavior

This commit is contained in:
Yokayo 2024-04-24 21:12:40 +07:00 committed by GitHub
parent 2b1aee9e71
commit e799bd3920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 12 deletions

View File

@ -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<Record<string, string>>} 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);
}