Localization Scripting

Version 11.1 by Marius Dumitru Florea on 2022/02/23 06:24

cogProvide script oriented APIs to manipulate translations
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

The identifier of the Localization Script Service is localization.

A translation corresponding to a key can be obtained by calling:

$services.localization.get('some.key')

The printable translation of a defined property is obtained by calling:

$services.localization.render('some.key')

As with the old MessageTool, parametrized translations can be obtained with:

$services.localization.render('some.key', ['param1', 10])

where parameters is a list of parameter values. The format of the translation is the one accepted by the MessageFormat class.

It's also possible to choose the syntax in which the translation message is going to be rendered using:

$services.localization.render('some.key', 'xhtml/1.0', ['param1', 10])

XWiki 5.1+ $services.localization.render('some.key', 'xhtml/1.0')

the default being plain/1.0.

XWiki 9.0-rc-1+ 

You can also pass the Locale to search for (instead of using the current Locale):

$services.localization.render('some.key', 'fr_FR')

XWiki 10.2+ 

It's possible to pass a list of keys to test one by one until one is found.

$services.localization.render([$someInputKey, "some.${specific}.key", 'some.generic.key'])

It's possible to add on demand translation bundles for the current execution by indicating their type and identifier like in:

$services.localization.use('document', 'wiki:Space.PageWithTranslations')

XWiki 5.0-milestone-1+ 

You can retrieve the current Locale:

$services.localization.currentLocale

XWiki 14.1-rc-1+ 

and the configured default Locale:

$services.localization.defaultLocale

Tags:
    

Get Connected