-
|
Could anyone share an example of the Lingui translation function without macro, in pure JS? Something like this: import { I18n } from '@lingui/core'
i18n.load("fr", messages)
i18n.activate("fr")
const translated = i18n.SOMETHING('Hello world')So, what should be used instead of |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
I still can't find a short alternative for this, only composed a long one with a custom function like this: import { I18n } from '@lingui/core'
import { generateMessageId } from "@lingui/message-utils/generateMessageId"
i18n.load("fr", messages)
i18n.activate("fr")
function linguiMsg(message) {
return { id: generateMessageId(message), message };
}
const translated = i18n._(linguiMsg("Hello world"));Is there any API function exist that does this in a simpler way? |
Beta Was this translation helpful? Give feedback.
-
const translated = i18n._({id: 'app.heading', 'Hello world'})Once you ditch the macro you will have to craft ids by yourself. The code you created in the previous message function linguiMsg(message) {
return { id: generateMessageId(message), message };
}
const translated = i18n._(linguiMsg("Hello world"));would not be recognized by the extractor. |
Beta Was this translation helpful? Give feedback.
-
|
After trying different approaches, I settled on this the most simple and clean approach of translating messages in pure JS without macros: const text = i18n.t(/* i18n */ '{subject} on {domain}', {
subject: 'foo',
domain: 'bar,
})It works well and using the |
Beta Was this translation helpful? Give feedback.
After trying different approaches, I settled on this the most simple and clean approach of translating messages in pure JS without macros:
It works well and using the
/* i18n */comment, the extractor detects and extracts the string correctly. The issue is only with complex scenarios where I need to pass the context to the message, for this I created a separate question: #2490