Skip to content

Commit

Permalink
fix(core): remove runtime whitespace trimming aligned with v5 (#2169)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Jan 30, 2025
1 parent f7b2392 commit 47a1ad7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
19 changes: 18 additions & 1 deletion packages/core/src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe("I18n", () => {
id: "My name is {name}",
message: "Je m'appelle {name}",
})
).toEqual("Je m'appelle")
).toEqual("Je m'appelle ")

// Untranslated message
expect(i18n._("Missing message")).toEqual("Missing message")
Expand Down Expand Up @@ -263,6 +263,23 @@ describe("I18n", () => {
).toEqual("Mi 'nombre' es {name}")
})

it("._ should not trim whitespaces in translated messages", () => {
const messages = {}

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

expect(
i18n._({
id: "msg",
/* note the space at the end */
message: " Hello ",
})
).toEqual(" Hello ")
})

it("._ shouldn't compile uncompiled messages in production", () => {
const messages = {
Hello: "Salut",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interpolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("interpolate", () => {
expect(plural({ value: 1 })).toEqual("1 Book")
expect(plural({ value: 2 })).toEqual("2 Books")
expect(plural({ value: 4 })).toEqual("Four books")
expect(plural({ value: 99 })).toEqual("Books with problems")
expect(plural({ value: 99 })).toEqual(" Books with problems ")

const offset = prepare(
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books}}"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ export function interpolate(
// convert raw unicode sequences back to normal strings
// note JSON.parse hack is not working as you might expect https://stackoverflow.com/a/57560631/2210610
// that's why special library for that purpose is used
return unraw(result.trim())
return unraw(result)
}
if (isString(result)) return result.trim()
if (isString(result)) return result
return result ? String(result) : ""
}
}

0 comments on commit 47a1ad7

Please sign in to comment.