configuration wrapper for i18next translations

Downloads in past


1.1.0a year ago4 years agoMinified + gzip package size for @dhis2/d2-i18n in KB



Provides internationization support for d2 frontend Apps.


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) {
        isLangRTL(locale) ? 'rtl' : 'ltr'


Pass strings to be translated into i18n.t function as below.
import i18n from '@dhis2/d2-i18n'
i18n.t('translation string')


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 ""


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


yarn upgrade --scope @dhis2


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


  • 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: