Wiki source code of Programming__WebObjects-Web Applications-Development-Localization and Internationalization
Version 2.1 by smmccraw on 2007/07/08 09:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | == Unicode == | ||
2 | |||
3 | To Enable Unicode for your WO app, add the following to your application constructor: | ||
4 | |||
5 | {{panel}} | ||
6 | |||
7 | WOMessage.setDefaultEncoding("UTF8"); | ||
8 | |||
9 | {{/panel}} | ||
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 | |||
15 | {{panel}} | ||
16 | |||
17 | <html> | ||
18 | <head> | ||
19 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
20 | <!-- etc... --> | ||
21 | |||
22 | {{/panel}} | ||
23 | |||
24 | === Jesse Barnum === | ||
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 | |||
28 | {{panel}} | ||
29 | |||
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")) ) { | ||
40 | result.setHeader("text/html; charset=UTF-8; encoding=UTF-8", "content-type"); | ||
41 | } | ||
42 | return result; | ||
43 | } | ||
44 | |||
45 | {{/panel}} | ||
46 | |||
47 | === Helmut Schottmüller === | ||
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 | |||
53 | {{panel}} | ||
54 | |||
55 | public WORequest createRequest(String aMethod, String aURL, String anHTTPVersion, NSDictionary someHeaders, NSData aContent, NSDictionary someInfo) | ||
56 | { | ||
57 | WORequest newRequest = super.createRequest(aMethod, aURL, anHTTPVersion, someHeaders, aContent, someInfo); | ||
58 | newRequest.setDefaultFormValueEncoding(_NSUtilities.UTF8StringEncoding); | ||
59 | return newRequest; | ||
60 | } | ||
61 | |||
62 | {{/panel}} | ||
63 | |||
64 | 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. | ||
65 | |||
66 | With Jesse's code and this extension, you will be able to handle UTF-8 character data correctly in your WO application. | ||
67 | |||
68 | If you use localized strings in your UTF-8 application you may also check out Project Wonder's [[ERXLocalizer>>Programming__WebObjects-WOnder-ERXLocalizer]] class. | ||
69 | |||
70 | Category:WebObjects |