Wiki source code of Writing Internationalized XWiki Applications
Version 11.5 by Clemens Robbenhaar on 2013/11/07
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
7.1 | 1 | {{box cssClass="floatinginfobox" title="**Contents**"}} |
2 | {{toc/}} | ||
3 | {{/box}} | ||
![]() |
1.1 | 4 | |
![]() |
9.1 | 5 | = Tutorials = |
6 | |||
7 | * [[Tutorial for Internationalizing a Form>>DevGuide.TranslationsTutorial]] | ||
8 | |||
![]() |
6.2 | 9 | = Using properties located in XWiki pages = |
![]() |
1.1 | 10 | |
![]() |
11.3 | 11 | * Create a page that will host your key/value pairs |
![]() |
7.1 | 12 | * Enter all the key/value pairs in that page ({{warning}}use the wiki editor{{/warning}}). For example:((( |
![]() |
6.3 | 13 | {{code language="none"}} |
![]() |
1.2 | 14 | greeting=hello |
15 | welcome=Welcome | ||
![]() |
1.12 | 16 | withparams=I can {0} pass {1} {2} params too using the Java MessageFormat syntax |
![]() |
6.1 | 17 | {{/code}} |
![]() |
6.3 | 18 | ))) |
![]() |
6.4 | 19 | * Do the same for all the translations you want.((( |
![]() |
6.1 | 20 | {{info}} |
21 | 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. | ||
22 | {{/info}} | ||
![]() |
6.4 | 23 | ))) |
![]() |
11.5 | 24 | * Tell XWiki that your page is a Document Bundle by going to the Administration page, selecting the "Localization" section and entering it in the "Internationalization Document Bundles" field. You can specify several pages, separated by commas.((( |
![]() |
6.1 | 25 | {{info}} |
![]() |
6.2 | 26 | 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 | 27 | {{/info}} |
![]() |
6.4 | 28 | ))) |
![]() |
6.1 | 29 | * Alternatively you can also specify the list of Internationalization Document Bundles in ##xwiki.cfg## under the key ##xwiki.documentBundles##. |
![]() |
6.3 | 30 | * On the page where you want to use the internationalized message, use:((( |
![]() |
6.1 | 31 | {{code}} |
![]() |
11.5 | 32 | $services.localization.render("key") |
33 | $services.localization.render("key", ["param1", "param2", ...]) | ||
![]() |
6.1 | 34 | {{/code}} |
![]() |
1.2 | 35 | |
![]() |
6.1 | 36 | 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 | 37 | ))) |
![]() |
1.16 | 38 | |
![]() |
6.2 | 39 | = I18n of XWiki Objects = |
![]() |
4.3 | 40 | |
![]() |
11.3 | 41 | This is currently not implemented (see our logged issue: [[XWIKI-69>>http://jira.xwiki.org/browse/XWIKI-69]]). There are 2 workarounds you can use: |
![]() |
6.1 | 42 | |
![]() |
8.1 | 43 | * 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):((( |
44 | {{code language="none"}} | ||
45 | $doc.getObject('Your.Class', 'lang', $context.language') | ||
46 | ## Will select the object whose 'lang' property is set to the current language | ||
47 | {{/code}} | ||
48 | ))) | ||
![]() |
6.3 | 49 | * Use Velocity scripting to do an IF in your object. For example, you could have:((( |
![]() |
6.1 | 50 | {{code language="none"}} |
![]() |
5.1 | 51 | #if ($context.language == "fr") |
52 | French texts | ||
53 | #else | ||
54 | Default texts | ||
55 | #end | ||
![]() |
6.1 | 56 | {{/code}} |
![]() |
6.3 | 57 | ))) |
![]() |
10.1 | 58 | |
59 | = Using the static Resource Bundles = | ||
60 | |||
61 | * Stop your XWiki instance | ||
![]() |
11.3 | 62 | * Extract the ##ApplicationResources*.properties## file for your language from the ##WEB-INF/lib/xwiki-*oldcore-*.jar## file and put them in your ##WEB-INF/classes## directory |
63 | * Edit this file and add/modify the key/value pairs you need for your application | ||
![]() |
11.5 | 64 | * In your document, use the following to get the value associated with a key: {{code language="none"}}$services.localization.render("key"){{/code}} |
![]() |
10.1 | 65 | |
66 | This will get the value from the Resource Bundle corresponding to the current language you are using in XWiki. | ||
67 |