[扫描的json加上“翻译” | i18n-scanner

问题描述 投票:0回答:1

我将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考虑到这一点。

i18next react-i18next
1个回答
0
投票

由于您正在使用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,它将在运行时获取用户所需的语言。

© www.soinside.com 2019 - 2024. All rights reserved.