Wiki source code of Writing Internationalized XWiki Applications
Version 6.4 by Vincent Massol on 2010/03/04
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
6.2 | 1 | {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}} |
![]() |
1.1 | 2 | |
![]() |
6.2 | 3 | = Using the static Resource Bundles = |
![]() |
1.1 | 4 | |
![]() |
1.2 | 5 | * Stop your XWiki instance |
![]() |
6.1 | 6 | * 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## |
7 | * Edit the ##WEB-INF/classes/ApplicationResources*.properties## Resource Bundle files by adding the key/value pairs you need for your application. | ||
![]() |
6.3 | 8 | * In your document, use the following to get the value associated with a key:((( |
9 | {{code language="none"}} | ||
![]() |
1.11 | 10 | $msg.get("key") |
![]() |
6.1 | 11 | {{/code}} |
![]() |
1.2 | 12 | |
13 | This will get the value from the Resource Bundle corresponding to the current language you are using in XWiki. | ||
![]() |
6.3 | 14 | ))) |
![]() |
1.2 | 15 | |
![]() |
6.2 | 16 | = Using properties located in XWiki pages = |
![]() |
1.1 | 17 | |
![]() |
6.1 | 18 | {{warning}} |
19 | 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. | ||
20 | {{/warning}} | ||
![]() |
1.1 | 21 | |
![]() |
1.2 | 22 | * Create a page that will host your key/value pairs. |
![]() |
6.3 | 23 | * Enter all the key/value pairs in that page (use the wiki editor). For example:((( |
24 | {{code language="none"}} | ||
![]() |
1.2 | 25 | greeting=hello |
26 | welcome=Welcome | ||
![]() |
1.12 | 27 | withparams=I can {0} pass {1} {2} params too using the Java MessageFormat syntax |
![]() |
6.1 | 28 | {{/code}} |
![]() |
6.3 | 29 | ))) |
![]() |
6.4 | 30 | * Do the same for all the translations you want.((( |
![]() |
6.1 | 31 | {{info}} |
32 | 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. | ||
33 | {{/info}} | ||
![]() |
6.4 | 34 | ))) |
35 | * 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.((( | ||
![]() |
6.1 | 36 | {{info}} |
![]() |
6.2 | 37 | 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 |
![]() |
6.1 | 38 | {{/info}} |
![]() |
6.4 | 39 | ))) |
![]() |
6.1 | 40 | * Alternatively you can also specify the list of Internationalization Document Bundles in ##xwiki.cfg## under the key ##xwiki.documentBundles##. |
![]() |
6.3 | 41 | * On the page where you want to use the internationalized message, use:((( |
![]() |
6.1 | 42 | {{code}} |
![]() |
1.12 | 43 | $msg.get("key") |
![]() |
1.16 | 44 | $msg.get("key", ["param1", "param2", ...]) |
![]() |
6.1 | 45 | {{/code}} |
![]() |
1.2 | 46 | |
![]() |
6.1 | 47 | where ##key## is the key for the message to retrieve. Parameters can also be passed as is shown in the second syntax above. |
![]() |
6.3 | 48 | ))) |
![]() |
1.16 | 49 | |
![]() |
6.2 | 50 | = I18n of XWiki Objects = |
![]() |
4.3 | 51 | |
![]() |
6.1 | 52 | This is currently not implemented (see our logged issue: [[XWIKI-69>>http://jira.xwiki.org/jira/browse/XWIKI-69]]). There are 2 workarounds you can use: |
53 | |||
![]() |
5.1 | 54 | * Have several objects, one for each language, with a language field, and then decide which object to use (for example in a Class Sheet, based on the current language). |
![]() |
6.3 | 55 | * Use Velocity scripting to do an IF in your object. For example, you could have:((( |
![]() |
6.1 | 56 | {{code language="none"}} |
![]() |
5.1 | 57 | #if ($context.language == "fr") |
58 | French texts | ||
59 | #else | ||
60 | Default texts | ||
61 | #end | ||
![]() |
6.1 | 62 | {{/code}} |
![]() |
6.3 | 63 | ))) |