js-lingui icon indicating copy to clipboard operation
js-lingui copied to clipboard

Macro docs lack of examples how to use plural() with custom i18n

Open timofei-iatsenko opened this issue 3 years ago • 3 comments

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`
})}`

timofei-iatsenko avatar Dec 16 '22 10:12 timofei-iatsenko

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

maman avatar Sep 02 '24 08:09 maman

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.

timofei-iatsenko avatar Sep 02 '24 11:09 timofei-iatsenko

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`
  })
})

LeonMueller-OneAndOnly avatar Oct 01 '24 09:10 LeonMueller-OneAndOnly

@timofei-iatsenko is it still relevant in the context of deprecating custom i18n instance?

andrii-bodnar avatar Oct 23 '24 09:10 andrii-bodnar

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`
})}`)

timofei-iatsenko avatar Oct 24 '24 07:10 timofei-iatsenko

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 avatar Oct 30 '24 09:10 etx121

@etx121 there are plenty of examples in this thread which will work.

timofei-iatsenko avatar Oct 30 '24 11:10 timofei-iatsenko

@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: Screenshot 2024-10-30 at 12 44 26

So I gave up on the usage of plural , and I just wrote the plain ICU message instead.

etx121 avatar Oct 30 '24 11:10 etx121

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.

timofei-iatsenko avatar Oct 30 '24 11:10 timofei-iatsenko

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.

etx121 avatar Oct 30 '24 12:10 etx121