Hi devs,
In the context of https://jira.xwiki.org/browse/XWIKI-18491 we’d like to introduce a REST endpoint to resolve translation keys.
The main goal is to allow JavaScript code to use localized messages without having to rely on Velocity code. The JavaScript module that needs to use some translation keys would fetch them using an URL like this:
/xwiki/rest/wikis/{wikiName}/localization/translations?locale=en&prefix=livedata.&key=one&key=two&...
This is endpoint should be called with the GET
method, and the following parameters:
- locale: Optional, defined the locale of the request translation values, otherwise the local of the context will be used.
- prefix: Optional, a common prefix used for all the provided keys
- key: (one or many), once for each requested translation key
The response is an object, where the keys as the requested translation keys and the values the raw translation values (i.e., where the parameters are not resolved), for instance:
{
"this.is.key1": "raw translation {0} 1",
"key2": "translation 2"
}
This means the client side will be responsible for applying (interpolating) translation parameters.
Letting client side resolving the raw translations values has several advantages:
- Responses can be cached
- Translations keys can be pre-fetched before knowning the values of the translations parameters
- When values change client side, the translations can be updated without having to call the server
WDYT?