This page describes the Magnolia i18n API, which is based on standard Java resource bundles.
The Magnolia i18n API is based on Java resource bundles and related classes. Among the main advantages of this API are:
- a mechanism to automatically load bundles, eliminating the need to handle bundle base names
- into one large bundle
- automatic generation of keys for Magnolia items such as template definitions, dialogs, apps and similar things
The base – Java message bundle
Java provides an API to load resource properties files for the purposes of internationalization.
What is a message bundle?
.propertiesfiles. Each file contains key-value pairs of translated user interface text such as labels and messages. The keys in all files of the same bundle are identical but the values are language specific translations.
A message bundle must contain at least one
.properties file. The files are named after the language (locale):
<bundle-name>_<locale>.properties, for example
app-pages-messages_en.properties. Every Magnolia module should provide its own message bundle. If a module installs several apps, each app should have its own message bundle.
Bundle basename and location
Typically message bundle files are located within the
resources directory of a Java (Maven) project. For example, the files could be in the
Working with the ordinary Java internationalization API you have to know the basename to load a message bundle. The basename for the above bundle would be
com.example.foobar.messages.app-texts. The basename reflects the location within the module by the dot notation. Magnolia allows you to use this "plain" Java internationalization API and provides utility classes to operate with it. However, we recommend using the Magnolia i18n API.
Automatic loading of message bundles from the Magnolia i18n folder
The Magnolia i18n API automatically loads message bundles if they are kept in the appropriate folders:
|Location||In Magnolia Maven module||In Magnolia light module|
Message bundles stored in these directories will be loaded automatically during module startup.
*) If you have modules with message bundles located within the deprecated folder, make sure that the bundles' file names are unique!
Bundle load order and bundle merging
Message bundles are loaded in the same order as modules. The Magnolia i18n API loads bundles automatically during module startup as long as they are stored in the directories specified in the table above. If present in the directories, all bundles from all modules are merged at startup into one large bundle, a virtual file, which is then used by the Magnolia i18n API.
The keys stored in the message files in the
i18n folder, as well as in the deprecated
mgnl-i18n folder, must be unique throughout all files in Magnolia i18n directories. When using one and the same key twice, the one which is loaded second overwrites the one loaded first. Organize your bundles and keys in a way that does not depend on the bundle load order.
The Magnolia i18n API automatically creates and assigns keys for properties of items such as template definition, dialog definition, forms, fields, validators, column definitions, subapp description, app definition and others. Depending on the underlying
Example: Auto-generated keys in a template definition
Let's suppose there is a template named
dilberts-page in a module called
foobar and the task is to provide the translation of the
title property of this template definition.
Here is the yaml template definition file:
- The key for the
titleproperty is created automatically by Magnolia, you do not have to provide it. In fact Magnolia will recognize two keys, one with module name and one without it:
- Line 5: If desired, you can assign a custom key.
Here is the message bundle file for the translation of the
Note that Magnolia provides many so called "generic keys" which are already translated and which may fit to your items. See generic i18n keys and their list for further details.
Magnolia provides different key generator classes for different items. These key generators are responsible for the creation and assignment of the auto-generated keys.
You can also implement your own custom key generator class. Implement
Below is an example of an existing Magnolia definition class using an existing key generator class:
- Line 1: $webResourceManager.requireResource("info.magnolia.sys.confluence.artifact-info-plugin:javadoc-resource-macro-resources") I18nableannotation specifes the key generator class.
- Lines 11, 14: $webResourceManager.requireResource("info.magnolia.sys.confluence.artifact-info-plugin:javadoc-resource-macro-resources") I18nTextannotation marks methods that return text which should be translated.
SimpleTranslator – using i18n in Java classes
If you want to internationalize UI elements or other things which are built directly on Java, use the
SimpleTranslatorbut inject it in the constructor.
Then pass the key in the
messages-app.app.button.sendMessage must be in a message file with a value: