Writing Internationalized XWiki Applications

Invalid macro parameters used for the [toc] macro. Cause: [Failed to validate bean: [must be greater than or equal to 1]]. Click on this message for details.

Using the static Resource Bundles

  • Stop your XWiki instance
  • Unjar the WEB-INF/lib/xwiki-core-*.jar file (it was named xwiki.jar in old versions) in WEB-INF/classes and remove it from WEB-INF/lib
  • Edit the WEB-INF/classes/ApplicationResources*.properties Resource Bundle files by adding the key/value pairs you need for your application.
  • In your document, use the following to get the value associated with a key:
$msg.get("key")

This will get the value from the Resource Bundle corresponding to the current language you are using in XWiki.

Using properties located in XWiki pages

The features described below are only available in XWiki 1.0 Beta 4 and above. The ability to parametrize the resource string is available only in XWiki 1.0 Beta 5 and above.

  • Create a page that will host your key/value pairs.
  • Enter all the key/value pairs in that page (use the wiki editor). For example:
greeting=hello
welcome=Welcome
withparams=I can {0} pass {1} {2} params too using the Java MessageFormat syntax
  • Do the same for all the translations you want.

To enable multiple languages you'll need to go to the Administration page and set MultiLingual to true and list the different languages you wish to use in the Languages field.

  • Tell XWiki that your page is a Document Bundle by going to the Administration page, selecting the "Programming" icon and entering it in the "Internationalization Document Bundles" field. You can specify several pages, separated by commas.

When creating "Internationalization Document Bundles", you should avoid naming the pages with names that include spaces. For example, a document name of Main.My Messages could cause issues, instead, use Main.MyMessages

  • Alternatively you can also specify the list of Internationalization Document Bundles in xwiki.cfg under the key xwiki.documentBundles.
  • On the page where you want to use the internationalized message, use:
$msg.get("key")
$msg.get("key", ["param1", "param2", ...])

where key is the key for the message to retrieve. Parameters can also be passed as is shown in the second syntax above.

I18n of XWiki objects

This is impossible to have different objects for different page translations for now. However, you can emulate it.

A possible solution is to have one object per language and add a language property in the objects. So you can filter objects by language in a page.

Tags:
   

Get Connected