Share this Page URL
Help

Chapter 18. Internationalizing Packages > Step 2: Internationalizing Page Templ... - Pg. 108

Internationalizing Packages 22 23 24 25 26 27 28 29 30 31 32 33 34 35 >>> 'msgs: %(messages)s, atts: %(attachments)s' %str.mapping 'msgs: 1, atts: 1' >>> size._message['msg2'] = Message() >>> str = size.sizeForDisplay() >>> str u'${messages} replies, 1 attachment' >>> 'msgs: %(messages)s, atts: %(attachments)s' %str.mapping 'msgs: 2, atts: 1' >>> size._message['att2'] = object() >>> str = size.sizeForDisplay() >>> str u'${messages} replies, ${attachments} attachments' >>> 'msgs: %(messages)s, atts: %(attachments)s' %str.mapping 'msgs: 2, atts: 2' 108 In the preceding code block, note the following: · Lines 7­11: The sizeForDisplay() method returns a message ID object. The message ID uses its text part for representation. In the following lines, you check that the mapping exists and that it contains the correct values. · Line 12­35: You repeat the test as before, using different numbers of replies and attachments. One last location where you have to internationalize some Python code output is in browser/ message.py. The string 'unknown' must be wrapped in a message ID factory call. Also, the string returned by the modified() method of the MessageDetails view class must be adapted to use the user's locale information because it returns a formatted date/time string. Because Message- Details is a view class, you have the user's locale available, so you can easily change the old version: 01 return date.strftime('%d/%m/%Y %H:%M:%S') to the internationalized version: 01 formatter = self.request.locale.dates.getFormatter('dateTime', 'short') 02 return formatter.format(date) Every BrowserRequest instance has a locale object, which represents the user's regional set- tings. The getFormatter() method returns a formatter instance that can format a datetime object to a string, based on the locale. (You can refer to the API reference at http://localhost:8080/+ +apidoc++ or to Chapter 10 to see all of the locale's functionality.) You have already done everything that needs to be done in the Python code. You should feel free to check the other Python modules for translatable strings; you will not find any. As promised, Python code contains only a human-readable strings, and that is a good thing. Step 2: Internationalizing Page Templates Internationalizing page templates is more interesting than Python code in many ways. You do not have to worry only about finding the correct tags to internationalize; because you can also have heavy nesting, the complexity can become overwhelming. It's a good idea to keep the content of translatable tags as flat as possible; that is, you should try to have translatable text that does not contain much HTML and TAL code. To achieve internationalization support in Zope 3's page templates, the Zope 3 developers designed a new i18n namespace, which is well documented at http://dev.zope.org/Zope3/ZPTInternationa- lizationSupport. The three most common attributes are i18n:domain, i18n:translate, and i18n:attributes. Note that the i18n namespace has been back-ported to Zope 2 as well, so you might be familiar with it already.