Version 14.1 by Thomas Mortagne on 2017/03/24

Hide last authors
Thomas Mortagne 7.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Vincent Massol 1.1 4
Vincent Massol 9.1 5 = Tutorials =
6
7 * [[Tutorial for Internationalizing a Form>>DevGuide.TranslationsTutorial]]
8
Silvia Macovei 6.2 9 = Using properties located in XWiki pages =
Vincent Massol 1.1 10
Manuel Smeria 11.3 11 * Create a page that will host your key/value pairs
Thomas Mortagne 7.1 12 * Enter all the key/value pairs in that page ({{warning}}use the wiki editor{{/warning}}). For example:(((
Vincent Massol 6.3 13 {{code language="none"}}
Vincent Massol 1.2 14 greeting=hello
15 welcome=Welcome
Vincent Massol 1.12 16 withparams=I can {0} pass {1} {2} params too using the Java MessageFormat syntax
Silvia Macovei 6.1 17 {{/code}}
Vincent Massol 6.3 18 )))
Vincent Massol 6.4 19 * Do the same for all the translations you want.(((
Silvia Macovei 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}}
Vincent Massol 6.4 23 )))
Vincent Massol 12.1 24 * Tell XWiki that your page is a Translation page by adding an Object of type ##TranslationDocumentClass## to your page.
25 * On the page where you want to use use either the ##~{{translation~}}## macro or the Localization Script Service (check the document for the [[Localization module>>extensions:Extension.Localization Module]]). Quick examples:(((
26 From wiki syntax:
27
Silvia Macovei 6.1 28 {{code}}
Vincent Massol 12.1 29 {{translation key="key"/}}
30 {{/code}}
31
32 From Script (Velocity in this example):
33
34 {{code}}
Clemens Robbenhaar 11.5 35 $services.localization.render("key")
36 $services.localization.render("key", ["param1", "param2", ...])
Silvia Macovei 6.1 37 {{/code}}
Vincent Massol 1.2 38
Silvia Macovei 6.1 39 where ##key## is the key for the message to retrieve. Parameters can also be passed as is shown in the second syntax above.
Vincent Massol 6.3 40 )))
Vincent Massol 1.16 41
Vincent Massol 12.1 42 == Old way ==
43
44 Prior to XWiki 4.3, the way to register a Document Resource Bundle was by going to the Administration page, selecting the "Localization" section and entering it in the "Internationalization Document Bundles" field. You could specify several pages, separated by commas.
45
46 {{info}}
47 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
48 {{/info}}
49
50 Alternatively you could also specify the list of Internationalization Document Bundles in ##xwiki.cfg## under the key ##xwiki.documentBundles##.
51
Silvia Macovei 6.2 52 = I18n of XWiki Objects =
Artem Melentyev 4.3 53
Thomas Mortagne 14.1 54 This is currently not implemented (see our logged issue: [[XWIKI-69>>https://jira.xwiki.org/browse/XWIKI-69]]). There are 2 workarounds you can use:
Silvia Macovei 6.1 55
Sergiu Dumitriu 8.1 56 * 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):(((
57 {{code language="none"}}
58 $doc.getObject('Your.Class', 'lang', $context.language')
59 ## Will select the object whose 'lang' property is set to the current language
60 {{/code}}
61 )))
Vincent Massol 6.3 62 * Use Velocity scripting to do an IF in your object. For example, you could have:(((
Silvia Macovei 6.1 63 {{code language="none"}}
Vincent Massol 5.1 64 #if ($context.language == "fr")
65 French texts
66 #else
67 Default texts
68 #end
Silvia Macovei 6.1 69 {{/code}}
Vincent Massol 6.3 70 )))
Vincent Massol 10.1 71
72 = Using the static Resource Bundles =
73
74 * Stop your XWiki instance
Manuel Smeria 11.3 75 * 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
76 * Edit this file and add/modify the key/value pairs you need for your application
Clemens Robbenhaar 11.5 77 * In your document, use the following to get the value associated with a key: {{code language="none"}}$services.localization.render("key"){{/code}}
Vincent Massol 10.1 78
79 This will get the value from the Resource Bundle corresponding to the current language you are using in XWiki.
Vincent Massol 13.1 80
81 If you're writing an extension JAR and wish to have static translation resources, you can put them in a ##ApplicationResources*.properties## file at the root of your extension JAR and they'll get picked up by the Localization module. See the [[Localization module>>extensions:Extension.Localization Module||anchor="HProvidetranslationbundleinajar"]] for more details.

Get Connected