| 27 | | Hints |
|---|
| | 25 | |
|---|
| | 26 | Hints on unicode migration |
|---|
| | 27 | ========================== |
|---|
| | 28 | |
|---|
| | 29 | The problem is that everything will seem to work until you include |
|---|
| | 30 | characters >127 (and even better >255) in your test data. |
|---|
| | 31 | |
|---|
| | 32 | |
|---|
| | 33 | About Unicode*Error |
|---|
| | 34 | ------------------- |
|---|
| | 35 | |
|---|
| | 36 | An exception is raised when mixing unicode and non ascii str, or using str() |
|---|
| | 37 | on unicode in an expression: |
|---|
| | 38 | |
|---|
| | 39 | u"été" + "été" -> UnicodeDecodeError |
|---|
| | 40 | u"été" == "été" -> UnicodeDecodeError |
|---|
| | 41 | str(u"été") -> UnicodeEncodeError |
|---|
| | 42 | |
|---|
| | 43 | Adding unicode to ascii str (ord above 127) is fine: |
|---|
| | 44 | u"été" + "ete" -> ok |
|---|
| | 45 | |
|---|
| | 46 | |
|---|
| | 47 | Rules for python code |
|---|
| | 48 | --------------------- |
|---|
| | 49 | |
|---|
| | 50 | * Zope should only work with unicode strings internally or ascii str |
|---|
| | 51 | (ord<127) for exemple the Localizer and zpt called by python return |
|---|
| | 52 | unicode |
|---|
| | 53 | |
|---|
| | 54 | * only the zope publisher encode the returned output page into utf-8. |
|---|
| | 55 | |
|---|
| | 56 | * check and try to remove any str() call |
|---|
| | 57 | |
|---|
| | 58 | * use isinstance(text, basestring) instead of isinstance(text, str) |
|---|
| | 59 | |
|---|
| | 60 | * remove .encode("ISO-8859-15", ...) |
|---|
| | 61 | |
|---|
| | 62 | * Take care that some attributes expect ascii str and don't support unicode: |
|---|
| | 63 | |
|---|
| | 64 | - identifier |
|---|
| | 65 | |
|---|
| | 66 | - path index |
|---|
| | 67 | |
|---|
| | 68 | |
|---|
| | 69 | |
|---|
| | 70 | Rules for zpt |
|---|
| | 71 | ------------- |
|---|
| | 72 | |
|---|
| | 73 | * the content charset must be utf-8 :: |
|---|
| | 74 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
|---|
| | 75 | |
|---|
| | 76 | This value is taken from the zope.conf if not present check that |
|---|
| | 77 | ``default-zpublisher-encoding`` is set to utf-8. |
|---|
| | 78 | |
|---|
| | 79 | * Add accept-charset="utf-8" in all forms:: |
|---|
| | 80 | <form ... accept-charset="utf-8"> |
|---|
| | 81 | |
|---|
| | 82 | * All text input must be cast with the ':utf8:utype', so zope will pass |
|---|
| | 83 | you unicode value in the request.form:: |
|---|
| | 84 | <input ... name="field:utf8:ustring" ...> |
|---|
| | 85 | |
|---|
| | 86 | There is also utokens, utext, and ulines. |
|---|
| | 87 | |
|---|
| | 88 | |
|---|
| | 89 | XXX |
|---|
| | 90 | |
|---|
| | 91 | Links |
|---|