Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escaped curly-braces doesn't preserve in development mode #1913

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions packages/core/src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ describe("I18n", () => {
it("._ should format message from catalog", () => {
const messages = {
Hello: "Salut",
"My name is {name}": "Je m'appelle {name}",
}

const i18n = setupI18n({
Expand All @@ -198,15 +197,24 @@ describe("I18n", () => {
})

expect(i18n._("Hello")).toEqual("Salut")
expect(i18n._("My name is {name}", { name: "Fred" })).toEqual(
"Je m'appelle Fred"
)
expect(
i18n._({
id: "My name is {name}",
message: "Je m'appelle {name}",
values: { name: "Fred" },
})
).toEqual("Je m'appelle Fred")
ninesunsabiu marked this conversation as resolved.
Show resolved Hide resolved

// alias
expect(i18n.t("Hello")).toEqual("Salut")

// missing { name }
expect(i18n._("My name is {name}")).toEqual("Je m'appelle")
expect(
i18n._({
id: "My name is {name}",
message: "Je m'appelle {name}",
})
).toEqual("Je m'appelle")

// Untranslated message
expect(i18n._("Missing message")).toEqual("Missing message")
Expand Down Expand Up @@ -239,16 +247,19 @@ describe("I18n", () => {
})

it("._ allow escaping syntax characters", () => {
const messages = {
"My ''name'' is '{name}'": "Mi ''nombre'' es '{name}'",
}
const messages = {}

const i18n = setupI18n({
locale: "es",
messages: { es: messages },
})

expect(i18n._("My ''name'' is '{name}'")).toEqual("Mi 'nombre' es {name}")
expect(
i18n._({
id: "My ''name'' is '{name}'",
message: "Mi ''nombre'' es '{name}'",
})
).toEqual("Mi 'nombre' es {name}")
})

it("._ shouldn't compile messages in production", () => {
Expand All @@ -270,6 +281,23 @@ describe("I18n", () => {
})
})

it("._ shouldn't compiled message from catalogs in development", () => {
const messages = {
Hello: "Salut",
"My name is {name}": "Je m'appelle {name}",
}

mockEnv("development", () => {
const { setupI18n } = require("@lingui/core")
const i18n = setupI18n({
locale: "fr",
messages: { fr: messages },
})

expect(i18n._("My name is {name}")).toEqual("Je m'appelle {name}")
})
})

it("._ should emit missing event for missing translation", () => {
const i18n = setupI18n({
locale: "en",
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,16 @@ export class I18n extends EventEmitter<Events> {
this.emit("missing", { id, locale: this._locale })
}

let translation = messageForId || message || id

if (process.env.NODE_ENV !== "production") {
translation = isString(translation)
? compileMessage(translation)
: translation
}
// To avoid double compilation, skip compilation for `messageForId`, because message from catalog should be already compiled
// ref: https://github.com/lingui/js-lingui/issues/1901
const translation =
messageForId ||
(() => {
const trans: CompiledMessage | string = message || id
return process.env.NODE_ENV !== "production"
? compileMessage(trans)
: trans
})()

// hack for parsing unicode values inside a string to get parsed in react native environments
if (isString(translation) && UNICODE_REGEX.test(translation))
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Trans.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("Trans component", () => {
"My name is {name}": "Jmenuji se {name}",
Original: "Původní",
Updated: "Aktualizovaný",
"msg.currency": "{value, number, currency}",
ID: "Translation",
},
},
Expand Down Expand Up @@ -290,6 +289,7 @@ describe("Trans component", () => {
const translation = text(
<Trans
id="msg.currency"
message="{value, number, currency}"
values={{ value: 1 }}
formats={{
currency: {
Expand Down
Loading