我将React与react-i18next
一起使用,并生成我使用i18next-scanner
的i18n json文件。
正如我所看到的,react-i18next
json文件需要键"translation":{...}
用于其他所有内容。
如何自动添加i18next-scanner
?
编辑:
i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import de from './de/resource.json';
import en from './en/resource.json';
i18n.use(initReactI18next).init({
debug: true,
fallbackLng: 'de',
lng: 'en',
load: 'all',
ns: false,
resources: { de, en },
react: { wait: true },
interpolation: { escapeValue: false },
});
// eslint-disable-next-line
export const t = i18n;
这是我需要它如何工作:src / i18n / en / resource.json
{
"translation": {
"component": {
这是i18next-scanner整理文件的方式:src / i18n / en / resource.json:
{
"component": {
我需要的是react-next
不需要此翻译,或者i18next-scanner
考虑到这一点。
由于您正在使用inline资源,因此需要在“翻译”前面加上前缀,您可以在用法中使用它。
// i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import de from './de/resource.json';
import en from './en/resource.json';
i18n.use(initReactI18next).init({
debug: true,
fallbackLng: 'de',
lng: 'en',
load: 'all',
ns: ["translation"], // <-- specify the NS's exists
defaultNS: "translation", // <-- what is the default namespace
resources: { de: {translation: de}, en: {translation: en} }, // <-- this is the change
react: { wait: true },
interpolation: { escapeValue: false },
});
// eslint-disable-next-line
export const t = i18n;
此“翻译”前缀在i18next空间中称为namespace。这意味着您的翻译文件可以拆分为单独的文件。
请注意,当您使用内联资源时,这意味着当用户仅使用一种语言时,捆绑文件将包含所有翻译。签出i18next-xhr-backend,它将在运行时获取用户所需的语言。