Skip to content

Commit

Permalink
add warning if no compiler and message is not compiled
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Sep 23, 2024
1 parent fadef7b commit afc1895
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 22 additions & 2 deletions packages/core/src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe("I18n", () => {
})
})

it("._ shouldn't use compiled message in production", () => {
it("._ should use compiled message in production", () => {
const messages = {
Hello: "Salut",
"My name is {name}": compileMessage("Je m'appelle {name}"),
Expand All @@ -304,7 +304,7 @@ describe("I18n", () => {
it("._ shouldn't double compile message in development", () => {
const messages = {
Hello: "Salut",
"My name is {name}": ["Je m'appelle {name}"],
"My name is {name}": compileMessage("Je m'appelle '{name}'"),
}

const { setupI18n } = require("@lingui/core")
Expand Down Expand Up @@ -338,6 +338,26 @@ describe("I18n", () => {
})
})

it("should print warning if uncompiled message is used", () => {
expect.assertions(1)

const messages = {
Hello: "Salut",
}

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

i18n._("Hello")
expect(console.warn).toBeCalled()
})
})
})
it("._ should emit missing event for missing translation", () => {
const i18n = setupI18n({
locale: "en",
Expand Down
18 changes: 15 additions & 3 deletions packages/core/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,21 @@ export class I18n extends EventEmitter<Events> {
let translation = messageForId || message || id

// Compiled message is always an array (`["Ola!"]`).
// If message comes as string - it's not compiled, and we need to compile it beforehand.
if (isString(translation) && this._messageCompiler) {
translation = this._messageCompiler(translation)
// If a message comes as string - it's not compiled, and we need to compile it beforehand.
if (isString(translation)) {
if (this._messageCompiler) {
translation = this._messageCompiler(translation)
} else {
console.warn(`Uncompiled message detected! Message:
> ${translation}
That means you use raw catalog or your catalog doesn't have a translation for the message and fallback was used.
ICU features such as interpolation and plurals will not work properly for that message.
Please compile your catalog first.
`)
}
}

// hack for parsing unicode values inside a string to get parsed in react native environments
Expand Down

0 comments on commit afc1895

Please sign in to comment.