Localization example

Hi. Can you provide localization examples for 5.2? Cannot find any working example in the documentation.

Hi Mathis,

Here is a link to the documentation: API: http://xp.readthedocs.org/en/5.3/reference/script-commands/i18n/localize.html

You need a folder in the resources/cms/ folder named i18n at the same level as the parts and pages folders. Put your phrases.properties files in there. These .properties files will have your translations. For example, phrases.properties might have a line “menu = menu” and the phrases_nb.properties would have a line “menu = meny”. Then in a page or part controller.js file you would use:

 var menu_placeholder = execute('i18n.localize', {
      key: 'menu',

And you can also do this directly in your Thymeleaf view like so:

<span data-th-text="${portal.localize({'_key=menu'})}"></span>

This will check your current site locale and find the “menu” keyword in the correct phrases-file.

Make sure your locale matches the settings in admin. “en” is the shorter English locale, while “en_us” is for American English. However Enonic will “waterfall” down to find the correct phrases file and always use the default one as an last resort. If no match is found anywhere, “NOT TRANSLATED” (or similar) will be printed.

1 Like