mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] qweb: expose translatable attributes
This commit allows owl users to add any attribute to the list of translatable attributes. closes #903
This commit is contained in:
+5
-1
@@ -1,4 +1,5 @@
|
|||||||
import { QWeb } from "./qweb/index";
|
import { QWeb } from "./qweb/index";
|
||||||
|
import { TRANSLATABLE_ATTRS } from "./qweb/qweb";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This file creates and exports the OWL 'config' object, with keys:
|
* This file creates and exports the OWL 'config' object, with keys:
|
||||||
@@ -9,9 +10,12 @@ import { QWeb } from "./qweb/index";
|
|||||||
interface Config {
|
interface Config {
|
||||||
mode: string;
|
mode: string;
|
||||||
enableTransitions: boolean;
|
enableTransitions: boolean;
|
||||||
|
translatableAttributes: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {} as Config;
|
export const config = {
|
||||||
|
translatableAttributes: TRANSLATABLE_ATTRS,
|
||||||
|
} as Config;
|
||||||
|
|
||||||
Object.defineProperty(config, "mode", {
|
Object.defineProperty(config, "mode", {
|
||||||
get() {
|
get() {
|
||||||
|
|||||||
+11
-3
@@ -66,7 +66,7 @@ interface QWebConfig {
|
|||||||
// Const/global stuff/helpers
|
// Const/global stuff/helpers
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"];
|
export const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"];
|
||||||
|
|
||||||
const lineBreakRE = /[\r\n]/;
|
const lineBreakRE = /[\r\n]/;
|
||||||
const whitespaceRE = /\s+/g;
|
const whitespaceRE = /\s+/g;
|
||||||
@@ -560,7 +560,11 @@ export class QWeb extends EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node.tagName !== "t" && node.hasAttribute("t-call")) {
|
if (node.tagName !== "t" && node.hasAttribute("t-call")) {
|
||||||
const tCallNode = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "t", null).documentElement;
|
const tCallNode = document.implementation.createDocument(
|
||||||
|
"http://www.w3.org/1999/xhtml",
|
||||||
|
"t",
|
||||||
|
null
|
||||||
|
).documentElement;
|
||||||
tCallNode.setAttribute("t-call", node.getAttribute("t-call")!);
|
tCallNode.setAttribute("t-call", node.getAttribute("t-call")!);
|
||||||
node.removeAttribute("t-call");
|
node.removeAttribute("t-call");
|
||||||
node.prepend(tCallNode);
|
node.prepend(tCallNode);
|
||||||
@@ -596,7 +600,11 @@ export class QWeb extends EventBus {
|
|||||||
throw new Error(`Unknown QWeb directive: '${attrName}'`);
|
throw new Error(`Unknown QWeb directive: '${attrName}'`);
|
||||||
}
|
}
|
||||||
if (node.tagName !== "t" && (attrName === "t-esc" || attrName === "t-raw")) {
|
if (node.tagName !== "t" && (attrName === "t-esc" || attrName === "t-raw")) {
|
||||||
const tNode = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "t", null).documentElement;
|
const tNode = document.implementation.createDocument(
|
||||||
|
"http://www.w3.org/1999/xhtml",
|
||||||
|
"t",
|
||||||
|
null
|
||||||
|
).documentElement;
|
||||||
tNode.setAttribute(attrName, node.getAttribute(attrName)!);
|
tNode.setAttribute(attrName, node.getAttribute(attrName)!);
|
||||||
for (let child of Array.from(node.childNodes)) {
|
for (let child of Array.from(node.childNodes)) {
|
||||||
tNode.appendChild(child);
|
tNode.appendChild(child);
|
||||||
|
|||||||
@@ -3943,6 +3943,20 @@ exports[`t-set value priority 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`translation support can add additional attributes to the list of translatable attributes 1`] = `
|
||||||
|
"function anonymous(context, extra
|
||||||
|
) {
|
||||||
|
// Template name: \\"test\\"
|
||||||
|
let h = this.h;
|
||||||
|
let _1 = 'word';
|
||||||
|
let _2 = 'mot';
|
||||||
|
let c3 = [], p3 = {key:3,attrs:{tomato: _1,potato: _2}};
|
||||||
|
let vn3 = h('div', p3, c3);
|
||||||
|
c3.push({text: \`text\`});
|
||||||
|
return vn3;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`translation support can translate node content 1`] = `
|
exports[`translation support can translate node content 1`] = `
|
||||||
"function anonymous(context, extra
|
"function anonymous(context, extra
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { QWeb } from "../../src/qweb/index";
|
import { QWeb } from "../../src/qweb/index";
|
||||||
|
import { config } from "../../src/index";
|
||||||
import { nextTick, normalize, renderToDOM, renderToString, trim } from "../helpers";
|
import { nextTick, normalize, renderToDOM, renderToString, trim } from "../helpers";
|
||||||
import { patch } from "../../src/vdom";
|
import { patch } from "../../src/vdom";
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ let qweb: QWeb;
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
QWeb.TEMPLATES = {};
|
QWeb.TEMPLATES = {};
|
||||||
|
QWeb.nextId = 1;
|
||||||
qweb = new QWeb();
|
qweb = new QWeb();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2211,6 +2213,17 @@ describe("translation support", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("can add additional attributes to the list of translatable attributes", () => {
|
||||||
|
const translations = {
|
||||||
|
word: "mot",
|
||||||
|
};
|
||||||
|
const translateFn = (expr) => translations[expr] || expr;
|
||||||
|
const qweb = new QWeb({ translateFn });
|
||||||
|
config.translatableAttributes.push("potato");
|
||||||
|
qweb.addTemplate("test", `<div tomato="word" potato="word">text</div>`);
|
||||||
|
expect(renderToString(qweb, "test")).toBe('<div tomato="word" potato="mot">text</div>');
|
||||||
|
});
|
||||||
|
|
||||||
test("translation is done on the trimmed text, with extra spaces readded after", () => {
|
test("translation is done on the trimmed text, with extra spaces readded after", () => {
|
||||||
const translations = {
|
const translations = {
|
||||||
word: "mot",
|
word: "mot",
|
||||||
|
|||||||
Reference in New Issue
Block a user