diff --git a/src/qweb/qweb.ts b/src/qweb/qweb.ts index f6b7c830..cf6cce9c 100644 --- a/src/qweb/qweb.ts +++ b/src/qweb/qweb.ts @@ -70,6 +70,7 @@ const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"]; const lineBreakRE = /[\r\n]/; const whitespaceRE = /\s+/g; +const translationRE = /^(\s*)([\s\S]+?)(\s*)$/; const NODE_HOOKS_PARAMS = { create: "(_, n)", @@ -496,7 +497,8 @@ export class QWeb extends EventBus { } if (this.translateFn) { if ((node.parentNode as any).getAttribute("t-translation") !== "off") { - text = this.translateFn(text); + const match = translationRE.exec(text); + text = match[1] + this.translateFn(match[2]) + match[3]; } } if (ctx.parentNode) { diff --git a/tests/qweb/__snapshots__/qweb.test.ts.snap b/tests/qweb/__snapshots__/qweb.test.ts.snap index 9f9a0b75..8016dd76 100644 --- a/tests/qweb/__snapshots__/qweb.test.ts.snap +++ b/tests/qweb/__snapshots__/qweb.test.ts.snap @@ -3924,6 +3924,18 @@ exports[`translation support some attributes are translated 1`] = ` }" `; +exports[`translation support translation is done on the trimmed text, with extra spaces readded after 1`] = ` +"function anonymous(context, extra +) { + // Template name: \\"test\\" + let h = this.h; + let c1 = [], p1 = {key:1}; + let vn1 = h('div', p1, c1); + c1.push({text: \` mot \`}); + return vn1; +}" +`; + exports[`whitespace handling consecutives whitespaces are condensed into a single space 1`] = ` "function anonymous(context, extra ) { diff --git a/tests/qweb/qweb.test.ts b/tests/qweb/qweb.test.ts index 80976074..f624b808 100644 --- a/tests/qweb/qweb.test.ts +++ b/tests/qweb/qweb.test.ts @@ -2154,6 +2154,17 @@ describe("translation support", () => { '

mot

mot

mot

mot

mot

' ); }); + + test("translation is done on the trimmed text, with extra spaces readded after", () => { + const translations = { + word: "mot", + }; + const translateFn = jest.fn((expr) => translations[expr] || expr); + const qweb = new QWeb({ translateFn }); + qweb.addTemplate("test", "
word
"); + expect(renderToString(qweb, "test")).toBe("
mot
"); + expect(translateFn).toHaveBeenCalledWith("word"); + }); }); describe("t-key tests", () => {