diff --git a/gui/scripts/i18n-backend.ts b/gui/scripts/i18n-backend.ts index df69cef..c5c36b8 100644 --- a/gui/scripts/i18n-backend.ts +++ b/gui/scripts/i18n-backend.ts @@ -6,33 +6,52 @@ const __dirname = path.dirname(path.fromFileUrl(import.meta.url)); const app = new Application(); const router = new Router(); -router.post('/locales/add/:lng/:ns', async (context) => { - try { - // ns is ignored - const { lng } = context.params; - const body: Record = await context.request.body({ type: 'json' }).value; - const keys = Object.keys(body); +const keys: Set = new Set(); +async function updateFile() { + delayId = null; + for (const lng of ['en', 'zh-CN']) { const translationPath = path.join(__dirname, `../src/i18n/translation/${lng}.json`); let translations: Record = {}; try { translations = JSON.parse(await Deno.readTextFile(translationPath)); } catch (error) {} + const toAdd = [...keys].filter(k => !Object.keys(translations).includes(k)); translations = Object.fromEntries( - [...Object.entries(translations), ...keys.map(i => [i, i] as const)].sort(([a], [b]) => a.localeCompare(b)), + [ + ...Object.entries(translations), + ...toAdd + .map(i => [i, i] as const)] + .sort(([a], [b]) => a.localeCompare(b)), ); + console.log('Add keys:', toAdd, 'for', lng); await Deno.writeTextFile( translationPath, JSON.stringify(translations, null, 2), ); + } + keys.clear(); +} +let delayId: number|null = null; - console.log('Add keys:', keys); +router.post('/locales/add/:lng/:ns', async (context) => { + try { + // ns, lng is ignored + const body: Record = await context.request.body({ type: 'json' }).value; + for (const key of Object.keys(body)) { + keys.add(key); + } + + if (delayId !== null) { + clearTimeout(delayId); + } + delayId = setTimeout(updateFile, 1000); context.response.status = 200; - context.response.body = { message: 'Translation updated.' }; + context.response.body = { message: 'Translation added.' }; } catch (error) { context.response.status = 500; context.response.body = { message: error.message }; diff --git a/gui/src/i18n/config.ts b/gui/src/i18n/config.ts index bc4663f..e574100 100644 --- a/gui/src/i18n/config.ts +++ b/gui/src/i18n/config.ts @@ -1,12 +1,16 @@ import i18next from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import { initReactI18next } from 'react-i18next'; -import translation from './translation/en.json'; +import en from './translation/en.json'; +import zhCN from './translation/zh-CN.json'; import HttpBackend from 'i18next-http-backend' export const resources = { en: { - translation, + translation: en, + }, + 'zh-CN': { + translation: zhCN, } }; @@ -21,6 +25,5 @@ if (import.meta.env.DEV) { instance.init({ debug: import.meta.env.DEV, resources, - saveMissing: true, - fallbackLng: 'en' + // saveMissing: true, }); diff --git a/gui/src/i18n/translation/en.json b/gui/src/i18n/translation/en.json index 30a2bf7..9e26dfe 100644 --- a/gui/src/i18n/translation/en.json +++ b/gui/src/i18n/translation/en.json @@ -1,7 +1 @@ -{ - "title": "Welcome {{name}}, to react using react-i18next fully type-safe", - "description": { - "part1": "This is a simple example.", - "part2": "😉" - } -} \ No newline at end of file +{} \ No newline at end of file diff --git a/gui/src/i18n/translation/zh-CN.json b/gui/src/i18n/translation/zh-CN.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/gui/src/i18n/translation/zh-CN.json @@ -0,0 +1 @@ +{} \ No newline at end of file