Wiki source code of WOnder-ERXLocalizer

Last modified by Pascal Robert on 2012/02/11 08:22

Hide last authors
David Holt 9.1 1 == Overview ==
David Holt 5.1 2
Pascal Robert 16.1 3 ERXLocalizer provides easy KVC (Key-Value Coding) access to localization. For a short description and a full list of all available methods please see the api ([[http:~~/~~/wocommunity.org/documents/javadoc/wonder/latest/er/extensions/localization/ERXLocalizer.html>>url:http://wocommunity.org/documents/javadoc/wonder/latest/er/extensions/localization/ERXLocalizer.html||shape="rect"]]).
David Holt 5.1 4
David Holt 9.1 5 === How to use ===
David Holt 5.1 6
Pascal Robert 16.1 7 The easiest way to use localization in you WebObjects applications is to use Project Wonder's built-in localization support using the the localizer() method in the [ ERXSession|WO:Programming~_~_WebObjects-WOnder-ERXSession] class. The localizer() method allows you to bind your localized language strings directly to you components. Lets say you want to localize your WebObjects application in english and german. The only thing you have to do is to create the required language resources in you application's resource directory. If you use Eclipse to develop your WebObjects applications, this is the ./Resources directory in you application root directory.
8 For english and german localization you need to create the following files and directories:
David Holt 5.1 9
David Holt 9.1 10 {{code}}
David Holt 5.1 11
David Holt 9.1 12 Resources
13 |
14 +-English.lproj
15 | |
16 | +-Localizable.strings
17 |
18 +-German.lproj
19 | |
20 | +-Localizable.strings
David Holt 5.1 21
David Holt 9.1 22 {{/code}}
David Holt 5.1 23
24 The Localizable.strings files contain the key-value pairs with you your localization. The key is always the //placeholder// for the translation and it is used in your WebObjects application to address the localized value. If your application is a simple //hello world// application, your localized files may look like
25
David Holt 9.1 26 {{code}}
David Holt 5.1 27
David Holt 9.1 28 {
29 "hello" = "Hello";
30 "world" = "World";
31 }
David Holt 5.1 32
David Holt 9.1 33 {{/code}}
David Holt 5.1 34
35 for the Localizable.strings file in the English.lproj directory and
36
David Holt 9.1 37 {{code}}
David Holt 5.1 38
David Holt 9.1 39 {
40 "hello" = "Hallo";
41 "world" = "Welt";
42 }
David Holt 5.1 43
David Holt 9.1 44 {{/code}}
David Holt 5.1 45
46 for the Localizable.strings file in the German.lproj directory.
47
Pascal Robert 16.1 48 To use the localized strings just bind the keys to an appropriate WOComponent (assuming you inherited your Session object from [ ERXSession|WO:Programming~_~_WebObjects-WOnder-ERXSession] in your application).
David Holt 5.1 49
David Holt 9.1 50 {{code}}
David Holt 5.1 51
David Holt 9.1 52 <p>The application says: <webobject name="langHello"/> <webobject name="langWorld"/></p>
David Holt 5.1 53
David Holt 9.1 54 {{/code}}
David Holt 5.1 55
56 (HTML file)
57
David Holt 9.1 58 {{code}}
59 langHello : WOString {
60 value = session.localizer.hello;
61 }
Pascal Robert 16.1 62 {{/code}}
David Holt 5.1 63
David Holt 9.1 64 {{code}}
David Holt 5.1 65
David Holt 9.1 66 langWorld : WOString {
67 value = session.localizer.world;
68 }
David Holt 5.1 69
David Holt 9.1 70 {{/code}}
David Holt 5.1 71
72 (WOD file)
73
David Holt 9.1 74 ERXLocalizer magically detects your system locale and puts out the the following HTML code if you use an english locale:
David Holt 5.1 75
David Holt 9.1 76 {{code}}
David Holt 5.1 77
David Holt 9.1 78 <p>The application says: Hello World</p>
David Holt 5.1 79
David Holt 9.1 80 {{/code}}
David Holt 5.1 81
Pascal Robert 15.1 82 === Useful application settings ===
David Holt 5.1 83
84 These are the default settings for ERXLocalizer:
85
David Holt 9.1 86 {{code}}
David Holt 5.1 87
Pascal Robert 15.1 88 er.extensions.ERXLocalizer.defaultLanguage=English
David Holt 9.1 89 er.extensions.ERXLocalizer.fileNamesToWatch=("Localizable.strings","ValidationTemplate.strings")
90 er.extensions.ERXLocalizer.availableLanguages=(English,German)
91 er.extensions.ERXLocalizer.frameworkSearchPath=(app,ERDirectToWeb,ERExtensions)
David Holt 5.1 92
David Holt 9.1 93 {{/code}}
David Holt 5.1 94
95 You can provide your own defaults in your application properties file or set the defaults somewhere in the initialization code of your application, e.g.
96
David Holt 9.1 97 {{code}}
David Holt 5.1 98
Pascal Robert 15.1 99 public Application() {
David Holt 9.1 100 NSLog.out.appendln("Welcome to " + this.name() + " !");
101 /* ** put your initialization code in here ** */
102 ERXLocalizer.setDefaultLanguage("English");
103 }
David Holt 5.1 104
David Holt 9.1 105 {{/code}}
David Holt 5.1 106
David Holt 9.1 107 === Unicode support ===
David Holt 5.1 108
Pascal Robert 15.1 109 By setting this property, your Wonder application will be correctly encoded in UTF-8 throughout.
110
111 er.extensions.ERXApplication.DefaultEncoding=UTF-8
112
113 The following paragraph may no longer apply:
114
Pascal Robert 16.1 115 If you are creating UTF-8 applications, please be aware that UTF-8 support for your Localizable.strings files seems not to work. Instead of being desperate, simply convert your Localizable.strings files to UTF-16. The localized strings are shown in the correct way in your UTF-8 application. For further information see [[Localizing Property Labels>>url:http://developer.apple.com/documentation/WebObjects/DesktopApplications/index.html?http://developer.apple.com/documentation/WebObjects/DesktopApplications/T9LocalizingDComponents/chapter_22_section_2.html||shape="rect"]] in the Apple Developer Connection.
Pascal Robert 15.1 116
Pascal Robert 16.1 117 (% style="color: rgb(0,0,0);" %)**ERJavaMail support**
Pascal Robert 15.1 118
Pascal Robert 16.1 119 (% style="color: rgb(51,51,51);" %)By setting this property, email sent via ERJavaMail will be correctly encoded in UTF-8.
Pascal Robert 15.1 120
121 er.javamail.defaultEncoding = UTF-8