2022-01-13 10:26:14 +01:00
|
|
|
# 🦉 Translations 🦉
|
|
|
|
|
|
2022-01-18 15:22:15 +01:00
|
|
|
If properly setup, Owl can translate all rendered templates. To do
|
2022-01-13 10:26:14 +01:00
|
|
|
so, it needs a translate function, which takes a string and returns a string.
|
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const translations = {
|
|
|
|
|
hello: "bonjour",
|
|
|
|
|
yes: "oui",
|
|
|
|
|
no: "non",
|
|
|
|
|
};
|
|
|
|
|
const translateFn = (str) => translations[str] || str;
|
|
|
|
|
|
2022-01-18 15:22:15 +01:00
|
|
|
const app = new App(Root, { templates, tranaslateFn });
|
|
|
|
|
// ...
|
2022-01-13 10:26:14 +01:00
|
|
|
```
|
|
|
|
|
|
2022-01-18 15:22:15 +01:00
|
|
|
See the [app configuration page](app.md#configuration) for more info on how to
|
|
|
|
|
configure an Owl application.
|
|
|
|
|
|
2022-01-13 10:26:14 +01:00
|
|
|
Once setup, all rendered templates will be translated using `translateFn`:
|
|
|
|
|
|
|
|
|
|
- each text node will be replaced with its translation,
|
|
|
|
|
- each of the following attribute values will be translated as well: `title`,
|
|
|
|
|
`placeholder`, `label` and `alt`,
|
|
|
|
|
- translating text nodes can be disabled with the special attribute `t-translation`,
|
|
|
|
|
if its value is `off`.
|
|
|
|
|
|
|
|
|
|
So, with the above `translateFn`, the following templates:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<div>hello</div>
|
|
|
|
|
<div t-translation="off">hello</div>
|
|
|
|
|
<div>Are you sure?</div>
|
|
|
|
|
<input placeholder="hello" other="yes"/>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
will be rendered as:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<div>bonjour</div>
|
|
|
|
|
<div>hello</div>
|
|
|
|
|
<div>Are you sure?</div>
|
|
|
|
|
<input placeholder="bonjour" other="yes"/>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that the translation is done during the compilation of the template, not
|
|
|
|
|
when it is rendered.
|
2022-01-18 15:22:15 +01:00
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```js
|
2022-02-03 13:31:30 +01:00
|
|
|
const app = new App(Root, { templates, tranaslateFn, translatableAttributes: ["data-title"] });
|
2022-01-18 15:22:15 +01:00
|
|
|
// ...
|
|
|
|
|
```
|
2022-02-03 13:31:30 +01:00
|
|
|
|
|
|
|
|
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...
|
|
|
|
|
```
|