Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 8 and Version 9 of Internationalization


Ignore:
Timestamp:
11/17/05 17:06:01 (9 years ago)
Author:
michele
Comment:

better formatting

Legend:

Unmodified
Added
Removed
Modified
  • Internationalization

    v8 v9  
    1 === To internationalize your application, you have to make sure of the following steps: === 
     1= To internationalize your application, you have to make sure of the following steps: = 
    22 
    3 1 That you mark your text strings using the _() function. 
     3 1. That you mark your text strings using the _() function. 
     4 1. That you have create message catalogs in the right directory structure for all your text strings and supported languages. 
     5 1. That any date or number strings are formatted using the turbogears.i18n formatting functions. 
    46 
    5 2 That you have create message catalogs in the right directory structure for all your text strings and supported languages. 
    6  
    7 3 That any date or number strings are formatted using the turbogears.i18n formatting functions. 
    8  
    9 '''1. Mark your text strings with _()''' 
     7== 1. Mark your text strings with _() == 
    108 
    119All text strings you want translated should be included in the _() function. This function is builtin to turbogears, so you don't need to import it specifically into your module, if you have already called "import turbogears". 
     
    1311For example: 
    1412 
     13{{{ 
     14#!python 
    1515import turbogears 
    1616 
     
    2323    def index(self): 
    2424        return dict(message=_("Welcome")) 
     25}}} 
    2526 
    2627If you want to explicitly pass in the locale in the _() call, you can do this: 
    2728 
     29{{{ 
     30#!python 
    2831print _("Welcome", "de") 
     32}}} 
    2933 
    30 Handling text strings in Kid templates is somewhat easier. If you set the cherrypy configuration setting "i18n.runTemplateFilter" to True, all text inside your Kid templates will be passed through _() automatically. The user locale (see below) can be overriden in your template by using the "lang" attribute, for example: 
     34Handling text strings in Kid templates is somewhat easier. If you set the cherrypy configuration setting '''i18n.runTemplateFilter''' to True, all text inside your Kid templates will be passed through _() automatically. The user locale (see below) can be overriden in your template by using the "lang" attribute, for example: 
    3135 
     36{{{ 
     37#!text/html 
    3238<div> 
    3339  <p>Welcome</p> 
    3440  <p lang="de">Welcome</p> 
    3541</div> 
     42}}} 
    3643 
    3744The first "Welcome" will be translated using the user locale setting, the second using the German locale. Assuming your user locale is English, you would see: 
    3845 
     46{{{ 
     47#!text/html 
    3948<div> 
    4049  <p>Welcome</p> 
    4150  <p lang="de">Willkommen</p> 
    4251</div> 
     52}}} 
    4353 
    4454However, attribute values are not translated and so have to be handled explicitly by using the _() call: 
    4555 
     56{{{ 
     57#!text/html 
    4658<img py:attrs="src=_('flag_icon_gb.bmp')" /> 
    47  
     59}}} 
    4860 
    4961Note that text inside widget templates, etc. will also be translated. 
    5062 
    51 '''2. Create your message catalogs''' 
     63== 2. Create your message catalogs == 
    5264 
    53 Use the Python scripts in tools/i18n, pygettext.py and msgfmt.py, to create your .po file and .mo files. For details on using this tool and creating your message catalogs, see[http://docs.python.org/lib/module-gettext.html]. Basically you should have a directory structure like this: 
     65Use the Python scripts in tools/i18n, pygettext.py and msgfmt.py, to create your .po file and .mo files. For details on using this tool and creating your message catalogs, see [http://docs.python.org/lib/module-gettext.html gettext module]. Basically you should have a directory structure like this: 
    5466 
     67{{{ 
    5568myapp/ 
    5669    locales/ 
     
    6679 
    6780                messages.mo 
     81}}} 
    6882 
    6983Eventually we should have tools in tg-admin to handle text extraction and compilation.  
    7084 
    71 By default, _() looks for subdirectory "locales" and domain "messages" in your project directory. You can override these settings in your config file by setting "i18n.localeDir" and "i18n.domain" respectively. 
     85By default, _() looks for subdirectory "locales" and domain "messages" in your project directory. You can override these settings in your config file by setting '''i18n.localeDir''' and '''i18n.domain''' respectively. 
    7286 
    7387If a language file(.mo) is not found, the _() call will return the plain text string. 
    7488 
    75 '''3. Localize your dates and numbers''' 
     89== 3. Localize your dates and numbers == 
    7690 
    7791The i18n package has a number of useful functions for handling date, location and number formats. Data for these formats are located in LDML (Locale Data Markup Language) files under turbogears/i18n/data; if you wish to use your own files, set the config setting i18n.dataDir. When searching for a format, the full locale name (e.g. en_CA for Canadian English) is used; if no matching format is found then the fallback locale(e.g. en for English) is used. These functions are found in i18n/formats.py. They include: 
    7892 
     93{{{ 
     94#!python 
    7995format_date: returns a localized date string 
    8096 
     
    8298 
    8399format_currency: returns a formatted currency value (e.g. in German 56.89 > 56,89) 
     100}}} 
    84101 
    85 '''Finding the user locale''' 
     102=== Finding the user locale === 
    86103 
    87104The default locale function, _get_locale, can be overriden by your own locale using the config setting "i18n.getLocale". This default function finds the locale setting in the following order: 
    88105 
    89 1 By looking for a session value. By default this is "locale", but can be changed in the config setting "i18n.sessionKey". 
     106 1. By looking for a session value. By default this is "locale", but can be changed in the config setting "i18n.sessionKey". 
     107 1. By looking in the HTTP Accept-Language header for the most preferred language. 
     108 1. By using the default locale (config setting "i18n.defaultLocale", by default "en"). 
    90109 
    91 2 By looking in the HTTP Accept-Language header for the most preferred language. 
    92  
    93 3 By using the default locale (config setting "i18n.defaultLocale", by default "en"). 
    94  
    95 '''Encoding''' 
     110=== Encoding === 
    96111 
    97112The _() and all formatting functions return unicode strings. 
    98  
    99  
    100