If properly setup, Owl can translate all rendered templates. To do so, it needs a translate function, which takes
For example:
const translations = {
fr: {
hello: "bonjour",
yes: "oui",
no: "non",
},
pt: {
hello: "bom dia",
yes: "sim",
no: "não",
},
};
const translateFn = (str, ctx) => translations[ctx]?.[str] || str;
const app = new App(Root, { templates, tranaslateFn });
// ...
See the app configuration page for more info on how to configure an Owl application.
Once setup, all rendered templates will be translated using translateFn:
title,
placeholder, label and alt,t-translation,
if its value is off.t-translation-context. If a specific node
attribute x needs another context, that context can be specified with a
special directive t-translation-context-x.So, with the above translateFn, the following templates:
<div>hello</div>
<div t-translation="off">hello</div>
<div>Are you sure?</div>
<input placeholder="hello" other="yes"/>
will be rendered as:
<div>bonjour</div>
<div>hello</div>
<div>Are you sure?</div>
<input placeholder="bonjour" other="yes"/>
and the following template:
<div t-translation-context="fr" title="hello">hello</div>
<div>Are you sure?</div>
<input t-translation-context-placeholder="pt" placeholder="hello" other="yes"/>
will be rendered as:
<div title="bonjour">bonjour</div>
<div>Are you sure?</div>
<input placeholder="bom dia" other="yes"/>
Note that the translation is done during the compilation of the template, not when it is rendered.
In some case, it is useful to be able to extend the list of translatable attributes.
For example, one may want to also translate data-title attributes. To do that,
we can define additional attributes with the translatableAttributes option:
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 -:
const app = new App(Root, {
templates,
tranaslateFn,
translatableAttributes: ["data-title", "-title"],
});
// data-title attribute will be translated, but not title attribute...