@dhis2/d2-i18n

中文文档
1.2.0发布于 2025/10/30BSD-3-ClauseTypeScriptESM + CJS近 30 天下载 1.7万

configuration wrapper for i18next translations

原始使用文档
文档目录11 节
  1. Purpose
  2. Installation
  3. Extracting and generating locale strings
  4. In App Code
  5. Changing User Locale
  6. Translations
  7. .gitignore
  8. Upgrade
  9. .travis.yml
  10. Notes
  11. Report an issue

Purpose

Provides internationization support for d2 frontend Apps.

Installation

yarn add @dhis2/d2-i18n

Extracting and generating locale strings

Extraction and generation are performed automatically by the build and start commands of @dhis2/cli-app-scripts. Optionally, they can be run independent of the build as well:

yarn d2-app-scripts i18n extract
yarn d2-app-scripts i18n generate

In App Code

On dev/build phase src/locales directory would be created. It will contain necessary setup for internationalization, date/time, calendar etc. It is auto-generated, so please don't update it, any changes to it will be lost.

At the top of src/index.js (assuming it's the main entry point to your App). Add the following,

import './locales'

Changing User Locale

Create a function changeLocale and isLangRTL as below. You should call this function in App loading phase.

function isLangRTL(code) {
    const langs = ['ar', 'fa', 'ur']
    const prefixed = langs.map(c => `${c}-`)
    return (
        langs.includes(code) ||
        prefixed.filter(c => code.startsWith(c)).length > 0
    )
}

function changeLocale(locale) {
    moment.locale(locale)
    i18n.changeLanguage(locale)
    document.documentElement.setAttribute(
        'dir',
        isLangRTL(locale) ? 'rtl' : 'ltr'
    )
}

Translations

Pass strings to be translated into i18n.t function as below.

import i18n from '@dhis2/d2-i18n'
i18n.t('translation string')

Context

Pass context property on second arg. to i18n.t

i18n.t('hello world', {
    context: 'a customary string to welcome a new born developer',
})

extract-pot will automatically generate msgctxt field before msgid in the en.pot file, giving translators a better context for translations.

msgctxt "a customary string to welcome a new born developer"
msgid "hello world"
msgstr ""

.gitignore

Append directory src/locales at the end. Because on dev/build phase src/locales is auto-generated.

Upgrade

yarn

yarn upgrade --scope @dhis2

.travis.yml

In .travis.yml before build/deploy step add yarn localize

Notes

  • We don't use react-i18next because handling cursor in text editor when ASCII and RTL characters are mixed is impossible to edit. Some ASCII characters move the cursor to one end of the line while RTL characters take cursor to the opposite end. Using variables in RTL language strings do not work because the first {{ might be entered correctly when translating but when we go to type the ending sequence }} the cursor ends up in the wrong place making it impossible to translate.

Report an issue

The issue tracker can be found in DHIS2 JIRA under the LIBS project.

Deep links:

版本与发布记录

@dhis2/d2-i18n 共发布 9 个版本,首次发布于 2018/04/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2025/10/301 年前
1.1.32023/11/202 年前
1.1.22023/09/113 年前
1.1.12023/02/023 年前
1.1.02021/03/015 年前
1.0.62019/10/247 年前

关于 @dhis2/d2-i18n 的常见问题

@dhis2/d2-i18n 是什么?

configuration wrapper for i18next translations

如何安装 @dhis2/d2-i18n?

在项目目录运行 npm install @dhis2/d2-i18n,或使用 pnpm add / yarn add / bun add。

@dhis2/d2-i18n 支持 TypeScript 吗?

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

@dhis2/d2-i18n 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dhis2/d2-i18n 可以商用吗?

@dhis2/d2-i18n 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@dhis2/d2-i18n 的下载量有多少?

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

@dhis2/d2-i18n 还在维护吗?

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