国际化反应应用程序.如何提取消息



我有一个使用typescript、babel、react的应用程序,我想将国际化添加到我的应用程序中。例如,如何将我的en.json提取到其他地区。

我有翻译/src/translations

en.json

{
"page.login.title: "Login",
"page.login.link.forgot.passowrd: "Forgot password"
}

ru.json

{
"page.login.title: "Страница входа",
}

ua.json

{
"page.login.link.forgot.passowrd: "Забув пароль"
}

如何在文件夹中获取文件/提取后的src/翻译如下

en.json

{
"page.login.title: "Login",
"page.login.link.forgot.passowrd: "Forgot password"
}

ru.json

{
"page.login.title: "Страница входа",
"page.login.link.forgot.passowrd: "Forgot password"
}

ua.json

{
"page.login.title: "Login",
"page.login.link.forgot.passowrd: "Забув пароль"
}

我已经在nodejs 上编写了脚本

const fs = require('fs');
const MAIN_LANGUAGE = './src/translations/en.json';
const LANG_DIR = './src/translations/';
const mainLocale = JSON.parse(
fs.readFileSync(MAIN_LANGUAGE, 'utf8', error => {
if (error) throw error;
})
);
fs.readdirSync(LANG_DIR).forEach(file => {
const currentLocale = JSON.parse(
fs.readFileSync(`${LANG_DIR}/${file}`, 'utf8', error => {
if (error) throw error;
})
);
const updatedCurrentLocale = JSON.stringify(
{ ...mainLocale, ...currentLocale },
null,
2
);
fs.writeFileSync(
`${LANG_DIR}/${file}`,
updatedCurrentLocale,
'utf8',
error => {
if (error) throw error;
}
);
console.log(`READY LOCALE: ${file}`);
});

最新更新