# @dhis2/d2-i18n

> configuration wrapper for i18next translations

Latest version **1.2.0** (published 2025-10-30) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install @dhis2/d2-i18n
pnpm add @dhis2/d2-i18n
yarn add @dhis2/d2-i18n
bun add @dhis2/d2-i18n
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2025-10-30 |
| First published | 2018-04-30 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Muhammad Adeel |
| Maintainers | dhis2-bot, amcgee |
| Keywords | i18n, translations |

## Links

- npm: https://www.npmjs.com/package/@dhis2/d2-i18n
- Repository: https://github.com/dhis2/d2-i18n
- Homepage: https://github.com/dhis2/d2-i18n#readme
- Issues: https://github.com/dhis2/d2-i18n/issues
- npm.io page: https://npm.io/package/@dhis2/d2-i18n

## Dependencies (3)

- [moment](https://npm.io/package/moment.md) ^2.24.0
- [i18next](https://npm.io/package/i18next.md) ^10.3
- [@types/i18next](https://npm.io/package/@types/i18next.md) ^11.9.0

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2025-10-30
- 1.1.3 — 2023-11-20
- 1.1.2 — 2023-09-11
- 1.1.1 — 2023-02-02
- 1.1.0 — 2021-03-01
- 1.0.6 — 2019-10-24
- 1.0.5 — 2019-06-19
- 1.0.4 — 2018-06-29
- 1.0.3 — 2018-04-30

## README

## Purpose

Provides internationization support for d2 frontend Apps.

-   i18n
-   Date/Time
-   Calendar
-   Monitoring translations [documentation](https://github.com/dhis2/d2-i18n-monitor), [website](https://dhis2.github.io/d2-i18n-monitor/#/)

## Installation

```bash
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](https://platform.dhis2.nu). Optionally, they can be run independent of the build as well:

```sh
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,

```js
import './locales'
```

### Changing User Locale

Create a function _changeLocale_ and _isLangRTL_ as below. You should call this function in App loading phase.

```js
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.

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

#### Context

Pass _context_ property on second arg. to i18n.t

```js
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**

```bash
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](https://jira.dhis2.org)
under the [LIBS](https://jira.dhis2.org/projects/LIBS) project.

Deep links:

-   [Bug](https://jira.dhis2.org/secure/CreateIssueDetails!init.jspa?pid=10700&issuetype=10006&components=11013)
-   [Feature](https://jira.dhis2.org/secure/CreateIssueDetails!init.jspa?pid=10700&issuetype=10300&components=11013)
-   [Task](https://jira.dhis2.org/secure/CreateIssueDetails!init.jspa?pid=10700&issuetype=10003&components=11013)

---
_Source: https://npm.io/package/@dhis2/d2-i18n · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
