Add locale data loading for extensions

This commit is contained in:
Cohee
2025-02-27 16:18:10 +00:00
parent c60af6659c
commit e312ae6b3b
3 changed files with 65 additions and 3 deletions

View File

@ -7,7 +7,7 @@ import { renderTemplate, renderTemplateAsync } from './templates.js';
import { delay, isSubsetOf, sanitizeSelector, setValueByPath } from './utils.js'; import { delay, isSubsetOf, sanitizeSelector, setValueByPath } from './utils.js';
import { getContext } from './st-context.js'; import { getContext } from './st-context.js';
import { isAdmin } from './user.js'; import { isAdmin } from './user.js';
import { t } from './i18n.js'; import { addLocaleData, getCurrentLocale, t } from './i18n.js';
import { debounce_timeout } from './constants.js'; import { debounce_timeout } from './constants.js';
import { accountStorage } from './util/AccountStorage.js'; import { accountStorage } from './util/AccountStorage.js';
@ -384,7 +384,7 @@ async function activateExtensions() {
if (meetsModuleRequirements && !isDisabled) { if (meetsModuleRequirements && !isDisabled) {
try { try {
console.debug('Activating extension', name); console.debug('Activating extension', name);
const promise = Promise.all([addExtensionScript(name, manifest), addExtensionStyle(name, manifest)]); const promise = Promise.all([addExtensionScript(name, manifest), addExtensionStyle(name, manifest), addExtensionLocale(name, manifest)]);
await promise await promise
.then(() => activeExtensions.add(name)) .then(() => activeExtensions.add(name))
.catch(err => console.log('Could not activate extension', name, err)); .catch(err => console.log('Could not activate extension', name, err));
@ -576,6 +576,42 @@ function addExtensionScript(name, manifest) {
}); });
} }
/**
* Adds a localization data for an extension.
* @param {string} name Extension name
* @param {object} manifest Manifest object
*/
function addExtensionLocale(name, manifest) {
// No i18n data in the manifest
if (!manifest.i18n || typeof manifest.i18n !== 'object') {
return Promise.resolve();
}
const currentLocale = getCurrentLocale();
const localeFile = manifest.i18n[currentLocale];
// Manifest doesn't provide a locale file for the current locale
if (!localeFile) {
return Promise.resolve();
}
return fetch(`/scripts/extensions/${name}/${localeFile}`)
.then(async response => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
if (data && typeof data === 'object') {
addLocaleData(currentLocale, data);
}
})
.catch(err => {
console.log('Could not load extension locale data for ' + name, err);
});
}
/** /**
* Generates HTML string for displaying an extension in the UI. * Generates HTML string for displaying an extension in the UI.
* *

View File

@ -11,6 +11,30 @@ var localeData;
export const getCurrentLocale = () => localeFile; export const getCurrentLocale = () => localeFile;
/**
* Adds additional localization data to the current locale file.
* @param {string} localeId Locale ID (e.g. 'fr-fr' or 'zh-cn')
* @param {Record<string, string>} data Localization data to add
*/
export function addLocaleData(localeId, data) {
if (!localeData) {
console.warn('Localization data not loaded yet. Additional data will not be added.');
return;
}
if (localeId !== localeFile) {
console.debug('Ignoring addLocaleData call for different locale', localeId);
return;
}
for (const [key, value] of Object.entries(data)) {
// Overrides for default locale data are not allowed
if (!Object.hasOwn(localeData, key)) {
localeData[key] = value;
}
}
}
/** /**
* An observer that will check if any new i18n elements are added to the document * An observer that will check if any new i18n elements are added to the document
* @type {MutationObserver} * @type {MutationObserver}

View File

@ -54,7 +54,7 @@ import {
writeExtensionField, writeExtensionField,
} from './extensions.js'; } from './extensions.js';
import { groups, openGroupChat, selected_group } from './group-chats.js'; import { groups, openGroupChat, selected_group } from './group-chats.js';
import { t, translate } from './i18n.js'; import { addLocaleData, getCurrentLocale, t, translate } from './i18n.js';
import { hideLoader, showLoader } from './loader.js'; import { hideLoader, showLoader } from './loader.js';
import { MacrosParser } from './macros.js'; import { MacrosParser } from './macros.js';
import { getChatCompletionModel, oai_settings } from './openai.js'; import { getChatCompletionModel, oai_settings } from './openai.js';
@ -165,6 +165,8 @@ export function getContext() {
isMobile, isMobile,
t, t,
translate, translate,
getCurrentLocale,
addLocaleData,
tags, tags,
tagMap: tag_map, tagMap: tag_map,
menuType: menu_type, menuType: menu_type,