fix: i18n-backend
parent
7532fde754
commit
16a4546710
|
|
@ -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<string,string> = await context.request.body({ type: 'json' }).value;
|
||||
const keys = Object.keys(body);
|
||||
const keys: Set<string> = 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<string, string> = {};
|
||||
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<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.body = { message: 'Translation updated.' };
|
||||
context.response.body = { message: 'Translation added.' };
|
||||
} catch (error) {
|
||||
context.response.status = 500;
|
||||
context.response.body = { message: error.message };
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1 @@
|
|||
{
|
||||
"title": "Welcome {{name}}, to react using react-i18next fully type-safe",
|
||||
"description": {
|
||||
"part1": "This is a simple example.",
|
||||
"part2": "😉"
|
||||
}
|
||||
}
|
||||
{}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
Loading…
Reference in New Issue