lingui / js-lingui

🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
https://lingui.dev
MIT License
4.49k stars 378 forks source link

How to embed pluralization within translation #314

Closed sedubois closed 6 years ago

sedubois commented 6 years ago

I'm excited about learning Lingui and wonder how I should use i18n.t etc to produce this kind of translation key with lingui extract:

"{value, plural, one {# result found} other {# results found}} in {ms} ms"

The French translation would be "{value, plural, one {# résultat trouvé} other {# résultats trouvés}} en {ms} ms".

I didn't find info about this in the plural documentation.

tricoder42 commented 6 years ago
i18n.t`${i18n.plural({ value, one: `# result found`, other: `# results found`} )} in {ms} ms`

Although I would rather recommend to move in {ms} ms inside plural, so translator see the whole message in one piece. Both messages result in the same output:

i18n.plural({
  value,
  one: `# result found in {ms} ms`,
  other: `# results found in {ms} ms`
})
tricoder42 commented 6 years ago

Just a note:

You could also use this:

i18n.t`${i18n.plural({ value, one: `# result`, other: `# results`} )} found in {ms} ms`

and still translate it into this:

"{value, plural, one {# résultat trouvé} other {# résultats trouvés}} en {ms} ms"

You don't have to map words in plurals 1:1.

Extreme example, in language without plurals the translation would be:

{value, number} result(s) found in {ms} ms 

That's the power of ICU MessageFormat - it doesn't assume anything about source/target languages.

tricoder42 commented 6 years ago

This is really undocumented :( Some examples are in tutorial https://lingui.js.org/tutorials/javascript.html, but that's not enough...

sedubois commented 6 years ago

Ah thanks, it's great that the calls can be nested and still result in a single translation key 🙂

i18n.t`${i18n.plural({
  value,
  one: `# result found`,
  other: `# results found`
})} in {ms} ms`;

This is exactly what I need for French:

"{value, plural, one {# résultat trouvé} other {# résultats trouvés}} en {ms} ms"