Version 39.1 by Johan Henselmans on 2009/11/10 06:49

Hide last authors
Pascal Robert 4.1 1 == Unicode ==
smmccraw 1.1 2
3 To Enable Unicode for your WO app, add the following to your application constructor:
4
Quinton Dolan 3.1 5 {{code}}
smmccraw 1.1 6
Quinton Dolan 3.1 7 WOMessage.setDefaultEncoding("UTF8");
smmccraw 1.1 8
Quinton Dolan 3.1 9 {{/code}}
smmccraw 1.1 10
11 This tells all WOResponse and WORequest to use UTF8 (Unicode).
12
13 Then you just need to tell the browser. Make all your .wo pages include this meta tag in their HTML:
14
Quinton Dolan 3.1 15 {{code value="xml"}}
smmccraw 1.1 16
Quinton Dolan 3.1 17 <html>
18 <head>
19 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
20 <!-- etc... -->
smmccraw 1.1 21
Quinton Dolan 3.1 22 {{/code}}
smmccraw 1.1 23
Pascal Robert 4.1 24 === Jesse Barnum ===
smmccraw 1.1 25
26 Great tip - here is a simple method call you can stick in your Application object to automatically achieve the results outlined above:
27
Quinton Dolan 3.1 28 {{code}}
smmccraw 1.1 29
Quinton Dolan 3.1 30 private boolean enableUTFEncoding = false;
31
32 public void enableUTFEncoding() {
33 enableUTFEncoding = true;
34 WOMessage.setDefaultEncoding(_NSUtilities.UTF8StringEncoding);
35 }
36
37 public WOResponse dispatchRequest(WORequest theRequest) {
38 WOResponse result = super.dispatchRequest(theRequest);
39 if( enableUTFEncoding && "text/html".equals(result.headerForKey("content-type")) ) {
Pascal Robert 4.1 40 result.setHeader("text/html; charset=UTF-8; encoding=UTF-8", "content-type");
smmccraw 1.1 41 }
Quinton Dolan 3.1 42 return result;
43 }
smmccraw 1.1 44
Quinton Dolan 3.1 45 {{/code}}
smmccraw 1.1 46
Pascal Robert 4.1 47 === Helmut Schottmüller ===
smmccraw 1.1 48
49 Unfortunately it's not so easy if you want to use file upload fields and UTF-8 encoding in the same form. Adding a file upload component means that you have to set the form's enctype to "multipart/form-data". To force a multipart form to use UTF-8 encoding usually needs an enctype of "multipart/form-data; charset=UTF-8" but WO is not able to identify such a form as multipart form. You will get a "java.lang.IllegalArgumentException: This form is missing a 'enctype=multipart/form-data' attribute. It is required for WOFileUpload to work." error when you open the form in the browser.
50
51 To make sure that UTF-8 is supported in multipart forms as well, you have to add the following code to your Application object:
52
Quinton Dolan 3.1 53 {{code}}
smmccraw 1.1 54
Pascal Robert 4.1 55 public WORequest createRequest(String aMethod, String aURL, String anHTTPVersion,
Quinton Dolan 3.1 56 NSDictionary someHeaders, NSData aContent, NSDictionary someInfo) {
Pascal Robert 4.1 57 WORequest newRequest = super.createRequest(aMethod, aURL, anHTTPVersion,
Quinton Dolan 3.1 58 someHeaders, aContent, someInfo);
59 newRequest.setDefaultFormValueEncoding(_NSUtilities.UTF8StringEncoding);
60 return newRequest;
61 }
smmccraw 1.1 62
Quinton Dolan 3.1 63 {{/code}}
smmccraw 1.1 64
Ramsey Gurley 25.1 65 To make WOFileUpload components working I also had to add the launch parameter WOUseLegacyMultipartParser true to my application. This launch parameter forces the parsing of all form values, the first time WORequest.formValues is called. See the [[apple developer documentation>>http://developer.apple.com/documentation/WebObjects/Reference/api/com/webobjects/appserver/WORequest.html]] for additional information. Without WOUseLegacyMultipartParser true I had serious problems in my applications using a WOFileUpload component because the bindings //data// and //filePath// have been emptied after a form POST.
smmccraw 1.1 66
67 With Jesse's code and this extension, you will be able to handle UTF-8 character data correctly in your WO application.
68
Pascal Robert 21.1 69 If you use localized strings in your UTF-8 application you may also check out Project Wonder's [[ERXLocalizer>>WOnder-ERXLocalizer]] class.
70
David Holt 29.1 71 === Project Localization tips [[~~ramsey]] ===
Pascal Robert 21.1 72
73 The following are some tips and suggestions for localizing a project in WOLips using Project Wonder.
74
75 ===== Eclipse Default Encoding =====
76
77 I prefer to keep my entire project in UTF-8 format for consistency. You can set that in your Eclipse General->Workspace preferences. The exception, of course will be your Localizable.strings files. Those have to be in UTF-16 format. I generally get warnings about the WOO file for my initial Main component whenever I create a new project, but if you right-click your Main.wo you'll see "Properties" at the very bottom of the contextual menu. Open that and flip your encoding between project default and UTF-8, save it, then open it back up and return it to the project default and the problem should go away. This is also how you set your Localized.strings file to UTF-16 even if the rest of your project is not UTF-16.
78
79 ===== Properties file =====
80
81 Let's say your project will be available in English and Japanese. You'll want to include the following in your Project->Resources->Properties file:
82
83 {{noformat}}
84
85 # Localization
86 er.extensions.ERXLocalizer.defaultLanguage=English
87 er.extensions.ERXLocalizer.fileNamesToWatch=("Localizable.strings","ValidationTemplate.strings")
88 er.extensions.ERXLocalizer.availableLanguages=(English,Japanese)
89 er.extensions.ERXLocalizer.frameworkSearchPath=(app,ERDirectToWeb,ERExtensions)
90
91 # Project Encoding
92 er.extensions.ERXApplication.DefaultEncoding=UTF-8
93
94 {{/noformat}}
95
Johan Henselmans 33.1 96 Note that if you need to customize the locale for a language, such as Canadian French, you can do so with this property:
97
98 {{noformat}}
99
100 er.extensions.ERXLocalizer.French_CA.locale = fr_ca
101
102 {{/noformat}}
103
Johan Henselmans 39.1 104 The other changes are then in the er.extensions.ERXLocalizer.availableLanguages and ERXLanguages:
105 In the above case after adding canadian french these would change in:
Johan Henselmans 33.1 106
Johan Henselmans 39.1 107 {{noformat}}
108
109 er.extensions.ERXLocalizer.availableLanguages=(English,Japanese,French_CA)
110
111 {{/noformat}}
112
Pascal Robert 21.1 113 ===== Localized strings and components =====
114
David Holt 29.1 115 For each language available, you will need a corresponding Localizable.strings file. This file should be located in Projects->Resources->"Lang".lproj directory. In these directories, you'll store localized resources such as Localizable.strings files and localized components. So, continuing with the above example, you should create two new Localizable.strings files in the following places in your project directory:
Pascal Robert 21.1 116
117 Project->Resources->English.lproj->Localizable.strings
118 Project->Resources->Japanese.lproj->Localizable.strings
Johan Henselmans 39.1 119 Project->Resources->French//CA.lproj->Localizable.strings//
Pascal Robert 21.1 120
121 As mentioned earlier, it's recommended that these be in UTF-16 format. You can do that by right clicking on the file in WOLips and selecting "Properties." In the resources panel, change from the project default encoding to UTF-16.
122
David Holt 29.1 123 If you have any components that need localizing, then you should relocate that component from your Project->Components folder into the appropriate Lang.lproj folder. Then make a copy of the component into the remaining lproj directories and you can begin the process of localizing the component. You do not need more than one copy of the associated API or java file. You only need duplicates of the WO. So, as an example, if you wanted to localize
Pascal Robert 21.1 124
125 > Project->Components->Main WO
126
David Holt 29.1 127 You would right-click->Refactor->Move it to
Pascal Robert 21.1 128
David Holt 29.1 129 > Project->Resources->English.lproj->Main WO
130
131 and then right-click->Copy it from English.lproj and right-click->Paste it into Japanese.lproj. At this point, when you open the component in WOLips, there will be a tab at the bottom of the component editor view that allows you to switch back and forth between different localized versions of that component.
132
Johan Henselmans 39.1 133 Your layout would end up something like this:
134
135 [[image:ERXLocalizerEclipseLayout.png]]
136
137 ===== Localized EOAttributes =====
138
139 In Wonder, it is also possible to localize attributes. There are two requisites:
140
141 First add ERXLanguages to your Properties file:
142
143 {{noformat}}
144
145 ERXLanguages = (en,jp,fr_ca)
146
147 {{/noformat}}
148
149 Then, for the attribute you'd like to localize, add the ERXLanguages key to the UserInfo for that specific attribute:
150
151 [[image:ERXLocalizerUserInfo.png]]
152
Pascal Robert 21.1 153 ===== Direct Actions =====
154
155 If you are defaulting to direct actions, you may not have a session. If you do not have a session, the server will return the default language specified in the Properties mentioned above. If you're using direct actions and you don't like that behavior, you can stick this in your direct action class:
156
157 {{code}}
158
David Avendasora 23.1 159 @Override
Pascal Robert 21.1 160 public WOActionResults performActionNamed(String actionName) {
161 if(!context().hasSession()) {
162 ERXLocalizer localizer =
163 ERXLocalizer.localizerForLanguages(context().request().browserLanguages());
164 ERXLocalizer.setCurrentLocalizer(localizer);
165 }
166 return super.performActionNamed(actionName);
167 }
168
David Avendasora 23.1 169
Pascal Robert 21.1 170 {{/code}}
171
172 That should give the user their browser's default language setting instead of your server's default language setting until a session is created.
173
174 ===== Database setup =====
175
176 Outside of this, if you are using a database, you'll need to make sure that is encoded properly as well. I'm using MySQL, so I have in my EOModel:
177
178 jdbc:mysql:~/~/localhost/mydatabase?capitalizeTypenames=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8
179
180 The database itself is set to default to "UTF8" encoding. (No hyphen in UTF8 for MySQL) You can set that in the "Options" pane of MySQL Administrator.app under the "Advanced" popup menu item in the "Def. char set" field. Of course, you'll need to use the correct database types too, meaning don't use a blob for text storage. Use varchar and longtext (varcharLarge is the name of the Wonder prototype) instead.