Edgewall Software

Changes between Version 1 and Version 2 of GenshiRecipes/Localization


Ignore:
Timestamp:
Nov 9, 2006, 2:34:59 PM (17 years ago)
Author:
David Fraser <davidf@…>
Comment:

added more descriptions about how it works, headings

Legend:

Unmodified
Added
Removed
Modified
  • GenshiRecipes/Localization

    v1 v2  
     1= Localization =
     2
    13This is code to aid in localization of Genshi templates, without altering the underlying templates.
    24It was originally written by Matt Good, then updated and fixed up by David Fraser.
    35
    4 Firstly here is a module that can be used to extract text from Genshi template streams.
     6== How it works ==
     7First 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
     9In 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
     14The 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 ==
     17Here is a module that can be used to extract text from Genshi template streams into POT files, for translation into different languages
    518{{{
    619#!python
     
    130143    for kind, data, pos in stream:
    131144        linenum = pos[1]
    132         print kind, linenum
    133145        if skip_level:
    134146            if kind is genshi.core.START:
     
    173185}}}
    174186
    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 ==
     188The following function can then be used to localize the template stream (see below for details on use)
     189The 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
    176191{{{
    177192#!python