在我的translations.ts代码文件中,我具有:
import i18next from 'i18next';
import Backend from 'i18next-node-fs-backend';
import path from 'path';
i18next.use(Backend).init({
lng: "en",
fallbackLng: "en",
backend: {
loadPath: path.join(__dirname, '/translations/{{lng}}_{{ns}}.json')
}
});
export default i18next;
然后,在我的main.ts文件中:
import { dialog ] from 'electron;
import i18next from './translations';
// If something particular happens I want to show a dialog, like so:
dialog.showErrorBox(i18next.t('error.error'), i18next.t('error.text'));
显示对话框,但是显示的文本实际上是“ error.error”和“ error.text”,而不是en_translation.json
文件中的字符串。
我没有收到错误消息。我觉得我没有正确加载i18next(或后端模块0),但是我不确定自己做错了什么。我在Stack Overflow上查看了其他类似的问题,但是这些问题或答案都无法解决我的问题问题。
i18next具有namespace
的概念,它允许您将应用程序划分为翻译的“区域”。
您需要指定默认名称空间,否则,您需要将其指定为键(i18next.t('translation:error.error')
)的一部分。
import i18next from 'i18next';
import Backend from 'i18next-node-fs-backend';
import path from 'path';
i18next.use(Backend).init({
lng: 'en',
fallbackLng: 'en',
defaultNS: 'translation', // <---- this is your default NS
backend: {
loadPath: path.join(__dirname, '/translations/{{lng}}_{{ns}}.json'),
},
});
export default i18next;