@connectedcars/react-i18n

中文文档
2.4.0发布于 2025/02/26MITTypeScriptCommonJS近 30 天下载 2059

翻译!

中文使用文档
文档目录6 节
  1. 安装
  2. 使用量
  3. 进出口
  4. 中情局
  5. 线性
  6. 示例

npm version

@ connectedcars/ react-i18n (英语)

安装

npm install @connectedcars/react-i18n

使用量

见 example 位于 example 文件夹。 特别是背景是如何设置的.

后从上下文取出翻译.

const {t} = this.context

何处 t 返回已翻译的字符串.

翻译一个简单的文本:

t('This text should be translated')

翻译包含变量的文本 :

t('This {word} is in a translated sentence.', { word: 'Variable'})

翻译复数( E)n 被注入到数据对象中,但可以覆盖:

tn(count, '{n} time', '{n} times')

多个行也被支持使用模板字符串(然而, 别 使用情况 ${} 变量 :

t(`Foo
   bar
   baz`)

但请注意,这些翻译不会在HTML中多行提供!

用反应翻译 :

// Use `tnx` for plurals with JSX!
tx(`
  <p>Hello, <strong>{name}</strong></p>
  <p>Today is {day}</p>
`, {
  p: content => <p>{content}</p>,
  strong: content => <strong>{content}</strong>,
  name: 'John Doe',
  day: 'Monday'
})

请注意, 默认翻译为严格模式, 也就是说, 未定义的类型将出现错误 请注意,可以通过设置添加白名单/默认 jsxWhitelist 编辑 I18nPovider。 。 。 .

您也可以在翻译中添加上下文 :

t('Hello', null, 'context here')

进出口

您首先需要设置您的导入和导出脚本 package.json

...

"scripts": {
  "import": "i18n-import",
  "export": "i18n-export",
}

...

然后可以运行以下命令:

  • npm run export 创建 POT 文件,其中包含已提取的翻译 ./locales/template.pot
  • npm run import 创建包含您全部翻译的 json 文件 ./src/translations.json

运行 npm run import --stripCountry 将删除国家代码 在相应的文件中, ie。 键 da_DK 将变成 da。 。 。 .

中情局

要获得 CI 的输出,请添加以下脚本:

  "ci-translation": "i18n-translation-status"

线性

见我们的 Eslint 插件 : https://github.com/接通车站/eslint-plugin-react-i18n

示例

import React from 'react'
import ReactDOM from 'react-dom'
import {
  I18nStore,
  I18nProvider,
  I18nContext,
  I18nConsumer,
  useTranslate,
  withTranslate,
} from '@connectedcars/react-i18n'
import './index.css'

const translations = {
  "da": {
    "": {
      "content-type": "text/plain; charset=UTF-8",
      "project-id-version": "",
      "pot-creation-date": "",
      "po-revision-date": "",
      "language-team": "",
      "mime-version": "1.0",
      "content-transfer-encoding": "8bit",
      "x-generator": "Poedit 2.0.6",
      "last-translator": "",
      "plural-forms": "nplurals=2; plural=(n != 1);",
      "language": "da"
    },
    "Hello {name}": [
      null,
      "Hej {name}"
    ],
    "Set language to <lang />": [
      null,
      "Skift sprog til <lang />"
    ]
  }
}

const store = new I18nStore({
  translations,
  locale: 'da',
  defaultLocale: 'en',
})

class ExampleA extends React.Component {
  static contextType = I18nContext

  render() {
    return <div>{this.context.t('Hello {name}', { name: 'World' })}</div>
  }
}

class ExampleB extends React.Component {
  render() {
    return (
      <I18nConsumer>
        {i18n => {
          return <div>{i18n.t('Hello {name}', { name: 'World' })}</div>
        }}
      </I18nConsumer>
    )
  }
}

const ExampleC = withTranslate(props => {
  return <div>{props.t('Hello {name}', { name: 'World' })}</div>
})

const ExampleD: React.FC = props => {
  const { tx } = useTranslate()

  return (
    <div>
      {tx('<strong>Hello</strong> there <link>test</link>', {
        strong: content => <strong>{content}</strong>,
        link: content => <a href="https://example.com">{content}</a>
      })}
    </div>
  )
}

const ToggleLocale: React.FC = () => {
  const { tx, setLocale, locale } = useTranslate()
  const swapLocale = locale === 'da' ? 'en' : 'da'

  return (
    <button onClick={() => setLocale(swapLocale)}>
      {tx('Set language to <lang />', {
        lang: (content, attr) => <strong>{swapLocale}</strong>,
      })}
    </button>
  )
}

ReactDOM.render(
  <I18nProvider store={store}>
    <ExampleA />
    <ExampleB />
    <ExampleC />
    <ExampleD />
    <ToggleLocale />
  </I18nProvider>,
  document.getElementById('root')
)

版本与发布记录

@connectedcars/react-i18n 共发布 35 个版本,首次发布于 2017/12/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.4.0latest2025/02/261.7 年前
2.3.62025/01/281.8 年前
2.3.52024/12/091.9 年前
2.3.42024/02/092 年前
2.3.32023/12/192 年前
2.3.22023/12/192 年前

其他发布通道:next → 2.0.2。

关于 @connectedcars/react-i18n 的常见问题

@connectedcars/react-i18n 是什么?

翻译!

如何安装 @connectedcars/react-i18n?

在项目目录运行 npm install @connectedcars/react-i18n,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-dom(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 i18n-export、i18n-import、i18n-translation-status,可以用 npx i18n-export 直接运行。

@connectedcars/react-i18n 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@connectedcars/react-i18n 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@connectedcars/react-i18n 可以商用吗?

@connectedcars/react-i18n 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@connectedcars/react-i18n 的下载量有多少?

根据 npm 官方下载统计,@connectedcars/react-i18n 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,059 次,日均约 69 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@connectedcars/react-i18n 还在维护吗?

最新版本 2.4.0 发布于 2025/02/26(1.7 年前);至今共发布 35 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/12/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。