i18next-parser 使用教程

i18next-parser 使用教程

i18next-parserParse your code to extract translation keys/values and manage your catalog files项目地址:https://gitcode.com/gh_mirrors/i1/i18next-parser

项目介绍

i18next-parser 是一个用于自动提取和更新 i18next 国际化框架中翻译键的工具。它可以帮助开发者从代码中提取需要翻译的字符串,并生成或更新翻译文件,从而简化国际化流程。

项目快速启动

安装

首先,你需要安装 i18next-parser。你可以通过 npm 或 yarn 进行安装:

npm install i18next-parser --save-dev

或者

yarn add i18next-parser --dev

配置

在你的项目根目录下创建一个 i18next-parser.config.js 文件,并添加以下基本配置:

module.exports = {
  input: [
    'src/**/*.{js,jsx,ts,tsx}',
    // 忽略的文件路径
    '!src/**/*.spec.{js,jsx,ts,tsx}',
    '!src/i18n/**',
    '!**/node_modules/**',
  ],
  output: './',
  options: {
    debug: true,
    removeUnusedKeys: true,
    sort: true,
    func: {
      list: ['i18next.t', 'i18n.t'],
      extensions: ['.js', '.jsx']
    },
    trans: {
      component: 'Trans',
      i18nKey: 'i18nKey',
      defaultsKey: 'defaults',
      extensions: [],
      fallbackKey: function(ns, value) {
        return value;
      },
    },
    lngs: ['en', 'de'],
    ns: [
      'translation',
      'resource'
    ],
    defaultLng: 'en',
    defaultNs: 'translation',
    defaultValue: function(lng, ns, key) {
      return key;
    },
    resource: {
      loadPath: 'i18n/{{lng}}/{{ns}}.json',
      savePath: 'i18n/{{lng}}/{{ns}}.json',
      jsonIndent: 2,
      lineEnding: '\n'
    },
    nsSeparator: false,
    keySeparator: false,
    interpolation: {
      prefix: '{{',
      suffix: '}}'
    }
  },
};

运行

配置完成后,你可以通过以下命令运行 i18next-parser:

npx i18next-parser

应用案例和最佳实践

应用案例

假设你有一个 React 项目,并且你希望将所有用户界面字符串国际化。你可以使用 i18next-parser 来提取这些字符串并生成翻译文件。

例如,在你的组件中:

import i18n from 'i18next';

function MyComponent() {
  return (
    <div>
      {i18n.t('welcome_message')}
    </div>
  );
}

运行 i18next-parser 后,你将得到一个 i18n/en/translation.json 文件,其中包含 welcome_message 键。

最佳实践

  1. 定期运行 i18next-parser:确保每次代码变更后都运行 i18next-parser,以保持翻译文件的最新状态。
  2. 使用默认值:在配置中设置 defaultValue 函数,以便在翻译文件中自动填充默认值。
  3. 忽略不必要的文件:在配置中指定需要忽略的文件路径,避免提取不必要的翻译键。

典型生态项目

i18next-parser 是 i18next 生态系统的一部分,i18next 是一个强大的国际化框架,支持多种前端和后端技术。以下是一些与 i18next 相关的典型项目:

  1. i18next:核心国际化框架,支持多种语言和命名空间。
  2. react-i18next:为 React 应用提供 i18next 的集成。
  3. i18next-http-backend:用于从 HTTP 后端加载翻译文件。
  4. i18next-browser-languagedetector:自动检测浏览器语言并加载相应的翻译文件。

i18next-parserParse your code to extract translation keys/values and manage your catalog files项目地址:https://gitcode.com/gh_mirrors/i1/i18next-parser

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌崧铖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值