Translation Messages in JavaScript

Last modified by Marius Dumitru Florea on 2021/09/22

We added support for using translation messages in JavaScript without relying on Velocity code. This has the big advantage that the JavaScript code can be cached without the need to invalidate the cache when the current locale changes. Here's how you can do it:

// First you need to define a module that holds the required translation keys.
define('my-translation-keys', {
  prefix: 'livedata.',
  keys: [
   "dropdownMenu.title",
   "selection.infoBar.allSelectedBut"
  ]
});

// Then you require the translation messages using the dedicated loader.
require(['xwiki-l10n!my-translation-keys'], function(l10n) {
 // Translation key without parameters.
 console.log(l10n['dropdownMenu.title']);

 // Translation key with parameters.
 console.log(l10n.get('selection.infoBar.allSelectedBut', 'parameter value'));
});

See the Localization Module documentation for more information on this topic.

Tags:
   

Get Connected