diff --git a/doc/reference/translations.md b/doc/reference/translations.md index 9af9cccd..52fd1e09 100644 --- a/doc/reference/translations.md +++ b/doc/reference/translations.md @@ -54,6 +54,17 @@ For example, one may want to also translate `data-title` attributes. To do that, we can define additional attributes with the `translatableAttributes` option: ```js -const app = new App(Root, { templates, tranaslateFn, translatalbeAttributes: ["data-title"] }); +const app = new App(Root, { templates, tranaslateFn, translatableAttributes: ["data-title"] }); // ... ``` + +It is also possible to remove an attribute from the default list by prefixing it with `-`: + +```js +const app = new App(Root, { + templates, + tranaslateFn, + translatableAttributes: ["data-title", "-title"], +}); +// data-title attribute will be translated, but not title attribute... +``` diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index ac772bbe..e5dba03e 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -214,14 +214,24 @@ export class CodeGenerator { templateName?: string; dev: boolean; translateFn: (s: string) => string; - translatableAttributes: string[]; + translatableAttributes: string[] = TRANSLATABLE_ATTRS; ast: AST; staticCalls: { id: string; template: string }[] = []; helpers: Set = new Set(); constructor(ast: AST, options: CodeGenOptions) { this.translateFn = options.translateFn || ((s: string) => s); - this.translatableAttributes = options.translatableAttributes || TRANSLATABLE_ATTRS; + if (options.translatableAttributes) { + const attrs = new Set(TRANSLATABLE_ATTRS); + for (let attr of options.translatableAttributes) { + if (attr.startsWith("-")) { + attrs.delete(attr.slice(1)); + } else { + attrs.add(attr); + } + } + this.translatableAttributes = [...attrs]; + } this.hasSafeContext = options.hasSafeContext || false; this.dev = options.dev || false; this.ast = ast; diff --git a/tests/compiler/__snapshots__/translation.test.ts.snap b/tests/compiler/__snapshots__/translation.test.ts.snap index a5e990b8..2de6eb0e 100644 --- a/tests/compiler/__snapshots__/translation.test.ts.snap +++ b/tests/compiler/__snapshots__/translation.test.ts.snap @@ -1,11 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`translation support can set translatable attributes 1`] = ` +exports[`translation support can set and remove translatable attributes 1`] = ` "function anonymous(bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; - let block1 = createBlock(\`
text
\`); + let block1 = createBlock(\`
text
\`); return function template(ctx, node, key = \\"\\") { return block1(); diff --git a/tests/compiler/translation.test.ts b/tests/compiler/translation.test.ts index 55d7550d..676a8d23 100644 --- a/tests/compiler/translation.test.ts +++ b/tests/compiler/translation.test.ts @@ -59,18 +59,20 @@ describe("translation support", () => { ); }); - test("can set translatable attributes", async () => { + test("can set and remove translatable attributes", async () => { class SomeComponent extends Component { static template = xml` -
text
+
text
`; } await mount(SomeComponent, fixture, { translateFn: (expr: string) => (expr === "word" ? "mot" : expr), - translatableAttributes: ["potato"], + translatableAttributes: ["potato", "-label"], }); - expect(fixture.innerHTML).toBe('
text
'); + expect(fixture.innerHTML).toBe( + '
text
' + ); }); test("translation is done on the trimmed text, with extra spaces readded after", async () => {