mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Split i18n.json file
This commit is contained in:
@@ -2,23 +2,32 @@ import { registerDebugFunction } from './power-user.js';
|
||||
import { waitUntilCondition } from './utils.js';
|
||||
|
||||
const storageKey = 'language';
|
||||
export const localeData = await fetch('i18n.json').then(response => response.json());
|
||||
const overrideLanguage = localStorage.getItem('language');
|
||||
|
||||
const localeFile = overrideLanguage || navigator.userLanguage || 'lang';
|
||||
export const localeData = await fetch(`./locales/${localeFile}.json`).then(response => {
|
||||
console.log(`Loading locale data from ./locales/${localeFile}.json`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load locale data: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
});
|
||||
|
||||
function getMissingTranslations() {
|
||||
const missingData = [];
|
||||
|
||||
for (const language of localeData.lang) {
|
||||
for (const language of localeData) {
|
||||
$(document).find('[data-i18n]').each(function () {
|
||||
const keys = $(this).data('i18n').split(';'); // Multi-key entries are ; delimited
|
||||
for (const key of keys) {
|
||||
const attributeMatch = key.match(/\[(\S+)\](.+)/); // [attribute]key
|
||||
if (attributeMatch) { // attribute-tagged key
|
||||
const localizedValue = localeData?.[language]?.[attributeMatch[2]];
|
||||
const localizedValue = localeData?.[attributeMatch[2]];
|
||||
if (!localizedValue) {
|
||||
missingData.push({ key, language, value: $(this).attr(attributeMatch[1]) });
|
||||
}
|
||||
} else { // No attribute tag, treat as 'text'
|
||||
const localizedValue = localeData?.[language]?.[key];
|
||||
const localizedValue = localeData?.[key];
|
||||
if (!localizedValue) {
|
||||
missingData.push({ key, language, value: $(this).text().trim() });
|
||||
}
|
||||
@@ -39,12 +48,12 @@ function getMissingTranslations() {
|
||||
uniqueMissingData.sort((a, b) => a.language.localeCompare(b.language) || a.key.localeCompare(b.key));
|
||||
|
||||
// Map to { language: { key: value } }
|
||||
const missingDataMap = {};
|
||||
for (const { key, language, value } of uniqueMissingData) {
|
||||
if (!missingDataMap[language]) {
|
||||
missingDataMap[language] = {};
|
||||
let missingDataMap = {};
|
||||
for (const { key, value } of uniqueMissingData) {
|
||||
if (!missingDataMap) {
|
||||
missingDataMap = {};
|
||||
}
|
||||
missingDataMap[language][key] = value;
|
||||
missingDataMap[key] = value;
|
||||
}
|
||||
|
||||
console.table(uniqueMissingData);
|
||||
@@ -54,12 +63,6 @@ function getMissingTranslations() {
|
||||
}
|
||||
|
||||
export function applyLocale(root = document) {
|
||||
const overrideLanguage = localStorage.getItem('language');
|
||||
var language = overrideLanguage || navigator.language || navigator.userLanguage;
|
||||
language = language.toLowerCase();
|
||||
//load the appropriate language file
|
||||
if (localeData.lang.indexOf(language) < 0) language = 'en';
|
||||
|
||||
const $root = root instanceof Document ? $(root) : $(new DOMParser().parseFromString(root, 'text/html'));
|
||||
|
||||
//find all the elements with `data-i18n` attribute
|
||||
@@ -69,12 +72,12 @@ export function applyLocale(root = document) {
|
||||
for (const key of keys) {
|
||||
const attributeMatch = key.match(/\[(\S+)\](.+)/); // [attribute]key
|
||||
if (attributeMatch) { // attribute-tagged key
|
||||
const localizedValue = localeData?.[language]?.[attributeMatch[2]];
|
||||
const localizedValue = localeData?.[attributeMatch[2]];
|
||||
if (localizedValue) {
|
||||
$(this).attr(attributeMatch[1], localizedValue);
|
||||
}
|
||||
} else { // No attribute tag, treat as 'text'
|
||||
const localizedValue = localeData?.[language]?.[key];
|
||||
const localizedValue = localeData?.[key];
|
||||
if (localizedValue) {
|
||||
$(this).text(localizedValue);
|
||||
}
|
||||
@@ -87,12 +90,11 @@ export function applyLocale(root = document) {
|
||||
}
|
||||
}
|
||||
|
||||
function addLanguagesToDropdown() {
|
||||
if (!Array.isArray(localeData?.lang)) {
|
||||
return;
|
||||
}
|
||||
async function addLanguagesToDropdown() {
|
||||
const langs = await fetch('/locales/lang.json').then(response => response.json());
|
||||
console.log(langs);
|
||||
|
||||
for (const lang of localeData.lang) {
|
||||
for (const lang of langs) {
|
||||
const option = document.createElement('option');
|
||||
option.value = lang;
|
||||
option.innerText = lang;
|
||||
|
Reference in New Issue
Block a user