fix: i18n-backend

main
imspace 2023-03-08 23:25:48 +08:00
parent 7532fde754
commit 16a4546710
4 changed files with 37 additions and 20 deletions

View File

@ -6,33 +6,52 @@ const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
const app = new Application(); const app = new Application();
const router = new Router(); const router = new Router();
router.post('/locales/add/:lng/:ns', async (context) => { const keys: Set<string> = new Set();
try {
// ns is ignored
const { lng } = context.params;
const body: Record<string,string> = await context.request.body({ type: 'json' }).value;
const keys = Object.keys(body);
async function updateFile() {
delayId = null;
for (const lng of ['en', 'zh-CN']) {
const translationPath = path.join(__dirname, `../src/i18n/translation/${lng}.json`); const translationPath = path.join(__dirname, `../src/i18n/translation/${lng}.json`);
let translations: Record<string, string> = {}; let translations: Record<string, string> = {};
try { try {
translations = JSON.parse(await Deno.readTextFile(translationPath)); translations = JSON.parse(await Deno.readTextFile(translationPath));
} catch (error) {} } catch (error) {}
const toAdd = [...keys].filter(k => !Object.keys(translations).includes(k));
translations = Object.fromEntries( 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( await Deno.writeTextFile(
translationPath, translationPath,
JSON.stringify(translations, null, 2), 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<string,string> = 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.status = 200;
context.response.body = { message: 'Translation updated.' }; context.response.body = { message: 'Translation added.' };
} catch (error) { } catch (error) {
context.response.status = 500; context.response.status = 500;
context.response.body = { message: error.message }; context.response.body = { message: error.message };

View File

@ -1,12 +1,16 @@
import i18next from 'i18next'; import i18next from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector'; import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next'; 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' import HttpBackend from 'i18next-http-backend'
export const resources = { export const resources = {
en: { en: {
translation, translation: en,
},
'zh-CN': {
translation: zhCN,
} }
}; };
@ -21,6 +25,5 @@ if (import.meta.env.DEV) {
instance.init({ instance.init({
debug: import.meta.env.DEV, debug: import.meta.env.DEV,
resources, resources,
saveMissing: true, // saveMissing: true,
fallbackLng: 'en'
}); });

View File

@ -1,7 +1 @@
{ {}
"title": "Welcome {{name}}, to react using react-i18next fully type-safe",
"description": {
"part1": "This is a simple example.",
"part2": "😉"
}
}

View File

@ -0,0 +1 @@
{}