Repository navigation
|
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 |
Replies: 4 comments 7 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? |
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. |
|
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 |
|
Finally invented a pure JS reusable translation function import { i18n } from '@lingui/core'
import { generateMessageId } from '@lingui/message-utils/generateMessageId'
/**
* Translates a variable string using Lingui.
*
* This function generates a message ID from the input string and context,
* then translates it using the Lingui i18n instance. If no translation is found
* (indicated by a 6-character alphanumeric result), it returns the original string
* as a fallback and logs a warning.
*
* @param str - The message string to translate
* @param values - Optional values for interpolation (same as i18n.t)
* @param options - Optional translation options, including:
* - `context`: Optional context string used for message ID generation to disambiguate translations
* - Other options passed to i18n.t (same as i18n.t)
* @returns The translated string, or the original string if translation is missing
*
* @example
* // Simple translation
* ts(/* i18n *\/ 'Hello')
*
* @example
* // With interpolation
* ts(/* i18n *\/ 'Hello {name}', { name: 'John' })
*
* @example
* // With context for disambiguation
* ts('Right', { context: 'direction' })
* Warning: defining messages with context using `/* i18n *\/` does not work
* with Lingui extractor, so you still have to define them manually somewhere
* in the code using a macro:
* ```
* import { defineMessage } from '@lingui/core/macro'
* defineMessage({message: 'Right', context: 'direction'})
* ```
*/
export const ts = (
str: string,
values?: Parameters<typeof i18n.t>[1],
options?: Parameters<typeof i18n.t>[2] & { context?: string },
): string => {
const msgId = generateMessageId(str, options?.context)
const result = i18n.t(msgId, values, options)
if (result.length === 6 && result.match(/^[a-zA-Z0-9]{6}$/)) {
// If the result is a 6-character alphanumeric string, it means the translation is missing.
// Return the original string as a fallback.
console.warn(
`Missing translation for message "${str}" (id '${msgId}'). Returning original string as fallback.`,
)
return str
}
return result
} |
Finally invented a pure JS reusable translation function
ts()(translate string):