Macro docs lack of examples how to use plural() with custom i18n
https://lingui.js.org/ref/macro.html#plural
Documentation on this page shows how to use t`msg` with custom i18n instance. But lacking an example how to use plural() with it.
There is a note:
Use plural inside t macro if you want to add custom id or comment for translators.
But still not clear how to use it, and this note is quite hard to spot when you're reading docs roughly.
Which one would work:
import { plural } from "@lingui/macro"
const message = t(i18n)(plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
}))
or
import { plural } from "@lingui/macro"
const message = t(i18n)({
message: plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})
})
or
import { plural } from "@lingui/macro"
const message = t(i18n)`${plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})}`
just stumbled to this issue myself – the correct form to use is the last one:
const message = t(i18n)`${plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})}`
it did create two localization entries, and a "nested" custom i18n calls when compiled via babel macros:
js
const message = i18n._({
id: "xxx", // --> from `t`
values: {
0: i18n._({
id: "yyy" // --> from `plural`
values: {
0: count,
1: name,
2: name,
}
})
}
})
po
#: some-source:1
msgid "{0}"
msgstr ""
#: some-source:1
msgid "{0, plural, one {{1} - has # friend} other {{2} - has # friends}}"
msgstr ""
The way to provide custom i18n instance is correct, but example of produced code is not. Macro is flattening calls to other macros inside a "root" call, so there should be only one entry in catalog.
I'm pretty sure it works like that in both, swc and babel versions, so either you have something wrong in your code or you find a bug.
just stumbled to this issue myself – the correct form to use is the last one:
const message = t(i18n)`${plural(count, { one: `${name} has # friend`, other: `${name} has # friends` })}`it did create two localization entries, and a "nested" custom i18n calls when compiled via babel macros:
js
const message = i18n._({ id: "xxx", // --> from `t` values: { 0: i18n._({ id: "yyy" // --> from `plural` values: { 0: count, 1: name, 2: name, } }) } })po
#: some-source:1 msgid "{0}" msgstr "" #: some-source:1 msgid "{0, plural, one {{1} - has # friend} other {{2} - has # friends}}" msgstr ""
This format does not lead to the extraction of the message into the .po file. Following format works as a macro:
Edit: This does not seem to hold true, depending on where i use this in my codebase i get different results.. I will investigate this
import {t, plural} from "@lingui/macro"
const message = t(i18n)({
message: plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})
})
@timofei-iatsenko is it still relevant in the context of deprecating custom i18n instance?
No it's not. The actual way of doing so is (Lingui V5)
import { plural, msg } from "@lingui/core/macro"
const message = i18n._(msg`${plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})}`)
Hi @timofei-iatsenko ,
I haven't found yet a working example to make plural work on v4. The examples shown in the documentation could be more explicit. Do you have an example with v4?
@etx121 there are plenty of examples in this thread which will work.
@timofei-iatsenko I tried the version of maman above:
const message = t(i18n)`${plural(count, {
one: `${name} has # friend`,
other: `${name} has # friends`
})}`
I end up having that:
So I gave up on the usage of plural , and I just wrote the plain ICU message instead.
First, please use syntax highlighting for code blocks, it's easier to read then.
Second, you should pass a count value here, and function will return a translated string to you.
First: ok sorry, I am not used to write comments on github 😬
Second: but we should define a count variable? I would like to have a message where i define later the translated message passing my values.