Changes between Version 1 and Version 2 of GenshiRecipes/Localization
- Timestamp:
- Nov 9, 2006, 2:34:59 PM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
GenshiRecipes/Localization
v1 v2 1 = Localization = 2 1 3 This is code to aid in localization of Genshi templates, without altering the underlying templates. 2 4 It was originally written by Matt Good, then updated and fixed up by David Fraser. 3 5 4 Firstly here is a module that can be used to extract text from Genshi template streams. 6 == How it works == 7 First a word on streams. This operates on the Template streams which are lists of events fairly similar to normal Genshi XML streams, but contain other special things (like EXPRessions and SUBstreams). The reason it needs to operate here is that it should take advantage of the template parsing, but it needs to operate before the contents are merged with the template. 8 9 In order to do this, we need three parts: 10 * Extraction of the localization text from templates 11 * Construction of the localized template stream using the translations and the original template stream 12 * Use of the localized template stream to generate the resulting page 13 14 The code on this page uses gettext - it generates POT files (although they don't currently contain the required header) and uses the ugettext function to translate the template pages (which will use MO files compiled from PO files containing the translations). 15 16 == Extraction of Localized Text == 17 Here is a module that can be used to extract text from Genshi template streams into POT files, for translation into different languages 5 18 {{{ 6 19 #!python … … 130 143 for kind, data, pos in stream: 131 144 linenum = pos[1] 132 print kind, linenum133 145 if skip_level: 134 146 if kind is genshi.core.START: … … 173 185 }}} 174 186 175 The following function can then be used to localize the template stream (see below for details on use): 187 == Localization of the Template Stream at Run Time == 188 The following function can then be used to localize the template stream (see below for details on use) 189 The reason that the ugettext is passed in as a function, is that language selection etc needs to happen depending on the language of the user submitting the request, not the machine serving the pages. You can thus pass in a specialized ugettext function that uses the appropriate language for the current user. 190 176 191 {{{ 177 192 #!python