lingui-i18n
JS API for I18n using ICU message format
lingui-i18n
provide helpers for writing texts using ICU message format
Install
npm install --save-dev lingui-i18n
yarn add --dev lingui-i18n
Usage
Import i18n
object and load message catalog:
import { i18n } from 'lingui-i18n'
i18n.load({
fr: {
"Hello World!": "Salut le monde!",
"My name is {name}": "Je m'appelle {name}",
"{count, plural, one {# book} other {# books}}": "{count, plural, one {# livre} other {# livres}}"
}
})
i18n.use('fr')
Wrap you text in i18n.t
template literal tag so it's translated into active
language. Variables must be wrapped inside object literal, so the name of variable is preserved in message catalog:
i18n.t`Hello World!`
const name = "Fred"
i18n.t`My name is ${{ name }}`
Plurals and selections are possible using plural
and select
methods (value must be again wrapped inside object literal):
const count = 42
i18n.plural({
value: { count },
one: "# book",
other: "# books"
})
It's also possible to nest message formats. Each message format method in i18n
has a standalone companion, which only returns message without performing the translation:
import { t, plural } from 'lingui-i18n'
i18n.select({
value: { gender },
offset: 1,
female: plural({
value: { numOfGuests },
offset: 1,
0: t`${host} does not give a party.`,
1: t`${host} invites ${guest} to her party.`,
2: t`${host} invites ${guest} and one other person to her party.`,
other: t`${host} invites ${guest} and # other people to her party.`
}),
male: plural({...}),
other: plural({...}),
})