Version 48.1 by David LeBer on 2010/06/29 16:12

Show last authors
1 == Unicode ==
2
3 See also: [[UTF-8 Encoding Tips>>UTF-8 Encoding Tips]]
4
5 To Enable Unicode for your WO app, add the following to your application constructor:
6
7 {{code}}
8
9 WOMessage.setDefaultEncoding("UTF8");
10
11 {{/code}}
12
13 This tells all WOResponse and WORequest to use UTF8 (Unicode).
14
15 Then you just need to tell the browser. Make all your .wo pages include this meta tag in their HTML:
16
17 {{code value="xml"}}
18
19 <html>
20 <head>
21 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
22 <!-- etc... -->
23
24 {{/code}}
25
26 === Jesse Barnum ===
27
28 Great tip - here is a simple method call you can stick in your Application object to automatically achieve the results outlined above:
29
30 {{code}}
31
32 private boolean enableUTFEncoding = false;
33
34 public void enableUTFEncoding() {
35 enableUTFEncoding = true;
36 WOMessage.setDefaultEncoding(_NSUtilities.UTF8StringEncoding);
37 }
38
39 public WOResponse dispatchRequest(WORequest theRequest) {
40 WOResponse result = super.dispatchRequest(theRequest);
41 if( enableUTFEncoding && "text/html".equals(result.headerForKey("content-type")) ) {
42 result.setHeader("text/html; charset=UTF-8; encoding=UTF-8", "content-type");
43 }
44 return result;
45 }
46
47 {{/code}}
48
49 === Helmut Schottmüller ===
50
51 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.
52
53 To make sure that UTF-8 is supported in multipart forms as well, you have to add the following code to your Application object:
54
55 {{code}}
56
57 public WORequest createRequest(String aMethod, String aURL, String anHTTPVersion,
58 NSDictionary someHeaders, NSData aContent, NSDictionary someInfo) {
59 WORequest newRequest = super.createRequest(aMethod, aURL, anHTTPVersion,
60 someHeaders, aContent, someInfo);
61 newRequest.setDefaultFormValueEncoding(_NSUtilities.UTF8StringEncoding);
62 return newRequest;
63 }
64
65 {{/code}}
66
67 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.
68
69 With Jesse's code and this extension, you will be able to handle UTF-8 character data correctly in your WO application.
70
71 If you use localized strings in your UTF-8 application you may also check out Project Wonder's [[ERXLocalizer>>WOnder-ERXLocalizer]] class.
72
73 === Project Localization tips [[~~ramsey]] ===
74
75 The following are some tips and suggestions for localizing a project in WOLips using Project Wonder.
76
77 ===== Eclipse Default Encoding =====
78
79 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.
80
81 ===== Properties file =====
82
83 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:
84
85 {{noformat}}
86
87 # Localization
88 er.extensions.ERXLocalizer.defaultLanguage=English
89 er.extensions.ERXLocalizer.fileNamesToWatch=("Localizable.strings","ValidationTemplate.strings")
90 er.extensions.ERXLocalizer.availableLanguages=(English,Japanese)
91 er.extensions.ERXLocalizer.frameworkSearchPath=(app,ERDirectToWeb,ERExtensions)
92
93 # Project Encoding
94 er.extensions.ERXApplication.DefaultEncoding=UTF-8
95
96 {{/noformat}}
97
98 Note that if you need to customize the locale for a language, such as Canadian French, you can do so with this property:
99
100 {{noformat}}
101
102 er.extensions.ERXLocalizer.French_CA.locale = fr_ca
103
104 {{/noformat}}
105
106 The other changes are then in the er.extensions.ERXLocalizer.availableLanguages and ERXLanguages:
107 In the above case after adding canadian french these would change in:
108
109 {{noformat}}
110
111 er.extensions.ERXLocalizer.availableLanguages=(English,Japanese,French_CA)
112
113 {{/noformat}}
114
115 ===== Localized strings and components =====
116
117 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:
118
119 Project->Resources->English.lproj->Localizable.strings
120 Project->Resources->Japanese.lproj->Localizable.strings
121 Project->Resources->French//CA.lproj->Localizable.strings//
122
123 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.
124
125 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
126
127 > Project->Components->Main WO
128 >\\
129 >>You would right-click->Refactor->Move it to
130
131 > Project->Resources->English.lproj->Main WO
132 >\\
133 >>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.
134
135 Your layout would end up something like this:
136
137 [[image:ERXLocalizerEclipseLayout.png]]
138
139 ===== Localized EOAttributes =====
140
141 In Wonder, it is also possible to localize attributes. There are two requisites:
142
143 First add ERXLanguages to your Properties file:
144
145 {{noformat}}
146
147 ERXLanguages = (en,jp,fr_ca)
148
149 {{/noformat}}
150
151 Then, for the attribute you'd like to localize, add the ERXLanguages key to the UserInfo for that specific attribute:
152
153 [[image:ERXLocalizerUserInfo_correct.png||border="1"]]
154
155 ===== Direct Actions =====
156
157 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:
158
159 {{code}}
160
161 @Override
162 public WOActionResults performActionNamed(String actionName) {
163 if(!context().hasSession()) {
164 ERXLocalizer localizer =
165 ERXLocalizer.localizerForLanguages(context().request().browserLanguages());
166 ERXLocalizer.setCurrentLocalizer(localizer);
167 }
168 return super.performActionNamed(actionName);
169 }
170
171
172 {{/code}}
173
174 That should give the user their browser's default language setting instead of your server's default language setting until a session is created.
175
176 [[^LocalizerTest.zip]] is an example application demonstrating the sessionless use of the localizer with localized strings and localized components, storing the language state in a cookie.
177
178 ===== Database setup =====
179
180 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:
181
182 jdbc:mysql:~/~/localhost/mydatabase?capitalizeTypenames=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8
183
184 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.