mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add locale data loading for extensions
This commit is contained in:
@ -7,7 +7,7 @@ import { renderTemplate, renderTemplateAsync } from './templates.js';
|
||||
import { delay, isSubsetOf, sanitizeSelector, setValueByPath } from './utils.js';
|
||||
import { getContext } from './st-context.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 { accountStorage } from './util/AccountStorage.js';
|
||||
|
||||
@ -384,7 +384,7 @@ async function activateExtensions() {
|
||||
if (meetsModuleRequirements && !isDisabled) {
|
||||
try {
|
||||
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
|
||||
.then(() => activeExtensions.add(name))
|
||||
.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.
|
||||
*
|
||||
|
@ -11,6 +11,30 @@ var localeData;
|
||||
|
||||
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
|
||||
* @type {MutationObserver}
|
||||
|
@ -54,7 +54,7 @@ import {
|
||||
writeExtensionField,
|
||||
} from './extensions.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 { MacrosParser } from './macros.js';
|
||||
import { getChatCompletionModel, oai_settings } from './openai.js';
|
||||
@ -165,6 +165,8 @@ export function getContext() {
|
||||
isMobile,
|
||||
t,
|
||||
translate,
|
||||
getCurrentLocale,
|
||||
addLocaleData,
|
||||
tags,
|
||||
tagMap: tag_map,
|
||||
menuType: menu_type,
|
||||
|
Reference in New Issue
Block a user