Skip to content

Commit

Permalink
[REF] translationCtx always a string?
Browse files Browse the repository at this point in the history
  • Loading branch information
Polymorphe57 committed Jan 14, 2025
1 parent 15fdf47 commit b4691c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
5 changes: 3 additions & 2 deletions doc/reference/translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

If properly setup, Owl can translate all rendered templates. To do
so, it needs a translate function, which takes

- a string (the term to translate)
- a string or null (the translation context of the term)
and returns a string.
- a string (the translation context of the term)
and returns a string.

For example:

Expand Down
14 changes: 7 additions & 7 deletions src/compiler/code_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type BlockType = "block" | "text" | "multi" | "list" | "html" | "comment";
const whitespaceRE = /\s+/g;

export interface Config {
translateFn?: (s: string, translationCtx: string | null) => string;
translateFn?: (s: string, translationCtx: string) => string;
translatableAttributes?: string[];
dev?: boolean;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ interface Context {
forceNewBlock: boolean;
isLast?: boolean;
translate: boolean;
translationCtx: string | null;
translationCtx: string;
tKeyExpr: string | null;
nameSpace?: string;
tModelSelectedExpr?: string;
Expand Down Expand Up @@ -266,7 +266,7 @@ export class CodeGenerator {
target = new CodeTarget("template");
templateName?: string;
dev: boolean;
translateFn: (s: string, translationCtx: string | null) => string;
translateFn: (s: string, translationCtx: string) => string;
translatableAttributes: string[] = TRANSLATABLE_ATTRS;
ast: AST;
staticDefs: { id: string; expr: string }[] = [];
Expand Down Expand Up @@ -306,7 +306,7 @@ export class CodeGenerator {
forceNewBlock: false,
isLast: true,
translate: true,
translationCtx: null,
translationCtx: "",
tKeyExpr: null,
});
// define blocks and utility functions
Expand Down Expand Up @@ -461,7 +461,7 @@ export class CodeGenerator {
.join("");
}

translate(str: string, translationCtx: string | null): string {
translate(str: string, translationCtx: string): string {
const match = translationRE.exec(str) as any;
return match[1] + this.translateFn(match[2], translationCtx) + match[3];
}
Expand Down Expand Up @@ -1150,7 +1150,7 @@ export class CodeGenerator {
name: string,
value: string,
attrsTranslationCtx: { [name: string]: string } | null,
translationCtx: string | null
translationCtx: string
): string {
if (name.endsWith(".translate")) {
const attrTranslationCtx = attrsTranslationCtx?.[name] || translationCtx;
Expand Down Expand Up @@ -1179,7 +1179,7 @@ export class CodeGenerator {
formatPropObject(
obj: { [prop: string]: any },
attrsTranslationCtx: { [name: string]: string } | null,
translationCtx: string | null
translationCtx: string
): string[] {
return Object.entries(obj).map(([k, v]) =>
this.formatProp(k, v, attrsTranslationCtx, translationCtx)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/template_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const bdom = { text, createBlock, list, multi, html, toggler, comment };
export interface TemplateSetConfig {
dev?: boolean;
translatableAttributes?: string[];
translateFn?: (s: string, ctx: string | null) => string;
translateFn?: (s: string, translationCtx: string) => string;
templates?: string | Document | Record<string, string>;
getTemplate?: (s: string) => Element | Function | string | void;
customDirectives?: customDirectives;
Expand All @@ -27,7 +27,7 @@ export class TemplateSet {
rawTemplates: typeof globalTemplates = Object.create(globalTemplates);
templates: { [name: string]: Template } = {};
getRawTemplate?: (s: string) => Element | Function | string | void;
translateFn?: (s: string, ctx: string | null) => string;
translateFn?: (s: string, translationCtx: string) => string;
translatableAttributes?: string[];
Portal = Portal;
customDirectives: customDirectives;
Expand Down
30 changes: 15 additions & 15 deletions tests/compiler/translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("translation support", () => {

await mount(SomeComponent, fixture, { translateFn });
expect(fixture.innerHTML).toBe("<div> mot </div>");
expect(translateFn).toHaveBeenCalledWith("word", null);
expect(translateFn).toHaveBeenCalledWith("word", "");
});

test("translation works, even if initial string has inner consecutive white space", async () => {
Expand All @@ -97,7 +97,7 @@ describe("translation support", () => {
const translateFn = jest.fn((expr: string) => (expr === "some word" ? "un mot" : expr));

await mount(SomeComponent, fixture, { translateFn });
expect(translateFn).toHaveBeenCalledWith("some word", null);
expect(translateFn).toHaveBeenCalledWith("some word", "");
expect(fixture.innerHTML).toBe("<div>un mot</div>");
});

Expand Down Expand Up @@ -181,13 +181,13 @@ describe("translation context", () => {
`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "fr" ? (expr === "word" ? "mot" : expr) : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "fr" ? (expr === "word" ? "mot" : expr) : expr
);

await mount(SomeComponent, fixture, { translateFn });
expect(fixture.innerHTML).toBe("<div>word</div><div>mot</div>");
expect(translateFn).toHaveBeenCalledWith("word", null);
expect(translateFn).toHaveBeenCalledWith("word", "");
expect(translateFn).toHaveBeenCalledWith("word", "fr");
});
test("translation of attributes in context", async () => {
Expand All @@ -197,8 +197,8 @@ describe("translation context", () => {
`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "fr" ? (expr === "title" ? "titre" : expr) : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "fr" ? (expr === "title" ? "titre" : expr) : expr
);

await mount(SomeComponent, fixture, { translateFn });
Expand All @@ -213,8 +213,8 @@ describe("translation context", () => {
<t t-esc="label"/>`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "fr" ? "traduit" : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "fr" ? "traduit" : expr
);

await mount(SomeComponent, fixture, { translateFn });
Expand All @@ -233,8 +233,8 @@ describe("translation context", () => {
<ChildComponent text.translate="game" t-translation-context-text.translate="fr" />`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "fr" ? "jeu" : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "fr" ? "jeu" : expr
);

await mount(SomeComponent, fixture, { translateFn });
Expand All @@ -258,8 +258,8 @@ describe("translation context", () => {
`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "fr" ? "jeu" : ctx === "pt" ? "título" : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "fr" ? "jeu" : translationCtx === "pt" ? "título" : expr
);

await mount(SomeComponent, fixture, { translateFn });
Expand All @@ -283,8 +283,8 @@ describe("translation context", () => {
</div>`;
}

const translateFn = jest.fn((expr: string, ctx: string | null) =>
ctx === "pt" ? "título" : expr
const translateFn = jest.fn((expr: string, translationCtx: string) =>
translationCtx === "pt" ? "título" : expr
);

await mount(SomeComponent, fixture, { translateFn });
Expand Down

0 comments on commit b4691c2

Please sign in to comment.