【问题标题】:wrap scanned json with "translation" | i18n-scanner用“翻译”包装扫描的 json | i18n-扫描仪
【发布时间】:2020-04-04 12:30:34
【问题描述】:

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

【问题讨论】:

  • 不正确,json中不需要这个前缀。
  • @felixmosh 我将添加代码。如果我没有这个translation 密钥,它就不起作用

标签: i18next react-i18next


【解决方案1】:

由于你使用的是inline资源,所以需要在前面加上“translations”,你可以在使用中做。

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

【讨论】:

  • 也许你可以看看这里的 repo。然后你也可以看看 i18next-scanner.js。您的修复没有被推送,但在本地不起作用。我一定会通过 i18next-xhr-backing 进行检查。 github.com/E-Edu/frontend/tree/feature/auto-parser-i18n i18n 配置在 src/i18n/ 并且 i18n-scanner 在根目录中
  • 好吧,我弄错了,我已经更新了我的答案,基本上添加了 defaultNS 和 ns 属性,(在此之前我将我的 NS 称为复数 translations 而不是 translation
  • 注意你的资源 json 不应该有翻译前缀(因为它是使用 i18next-scanner 生成的)
  • 谢谢。那行得通。我也想投赞成票,但我只有 13 分...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多