Edgewall Software

Changes between Version 9 and Version 10 of CookBook/PluginL10N


Ignore:
Timestamp:
May 9, 2010, 4:01:04 PM (14 years ago)
Author:
Christian Boos
Comment:

reviewed first half of the page

Legend:

Unmodified
Added
Removed
Modified
  • CookBook/PluginL10N

    v9 v10  
    1010
    1111== i18n, l10n, ... help! ==
    12 In short '''i18n''' stands for '''`i`'''`nternationalizatio`'''`n`''' (count 18 more chars between i and n) and is defined as software design for programs with translation support. '''`l`'''`ocalisatio`'''`n`''' that is abbreviated as '''l10n''' could be seen as a follow-up process providing data for one or more locales. It is taking care of feature differences between the original/default (that is English is most cases including Trac) and a given locale as well. Such features are i.e. sentence structure including punctuation and formatting of numbers, date/time strings, and currencies. Once you did some ground work at the source (i18n), most remaining is proper translation work (l10n) putting more or less effort in preserving the sense of the original while looking as native locale as possible.^1^
    13 
    14 '''NLS''' (National Language Support or Native Language Support) is meant to be the sum of both. And there are more related terms that we could safely skip for now.^1, 2^
     12In short '''i18n''' stands for '''`i`'''`nternationalizatio`'''`n`''' (count 18 more chars between i and n) and is defined as software design for programs with translation support. '''`l`'''`ocalisatio`'''`n`''' that is abbreviated as '''l10n''' could be seen as a follow-up process providing data for one or more locales. It is taking care of feature differences between the original/default (that is English is most cases including Trac) and a given locale as well. Such features are i.e. sentence structure including punctuation and formatting of numbers, date/time strings, and currencies. Once you did some ground work at the source (i18n), most remaining is proper translation work (l10n) putting more or less effort in preserving the sense of the original while looking as native locale as possible.^[#a1 1]^
     13
     14'''NLS''' (National Language Support or Native Language Support) is meant to be the sum of both. And there are more related terms that we could safely skip for now.^[#a1 1], [#a2 2]^
    1515
    1616== Background and concept of i18n/l10n support for Trac plugins ==
    1717It begun with adding Babel to Trac. Some plugin maintainers created their own translation module inside each plugin separately. Growing amount of code redundancy and possibility of error within imperfect copies and variants of a translation module all that was certainly not a desirable situation. And Trac core maintainers took responsibility with adding functions dedicated to i18n/l10n support for Trac plugins.
    1818
    19 The evolution of this functions has been documented in [comment:11:ticket:7497 ticket 7497]. The final implementation as mentioned there in [comment:12:ticket:7497 comment 12] was introduced to Trac trunk in [changeset: changeset r7705] and finally done with [changeset: changeset r7714].
     19The evolution of this functions has been documented in [comment:11:ticket:7497 ticket 7497]. The final implementation as mentioned there in [comment:12:ticket:7497 comment 12] was introduced to Trac trunk in changeset r7705 and finally done with changeset r7714.
    2020
    2121Now adding the needed i18n/l10n helper functions is done by importing a set of functions from `trac/util/translation.py` and providing proper configuration for an additional translation layer ('domain') inside the plugin code. On plugin initialization the dedicated translation domain is created as well and corresponding catalog files holding translated messages are loaded into it. Whenever a translatable text is encountered during runtime inside plugin's code, i18n/l10n helper functions will try to get the corresponding translation from the message catalog of plugin's domain and fall back silently to Trac's main message catalog, if needed.
    2222
     23//
    2324Both searches take the locale setting as a second request argument. Valid settings are a combination of language and country code, often extended further by the character encoding used, i.e. to read like β€˜de_DE.UTF-8’. The encoding is of special relevance for languages that had an older encoding per default that was not sufficient for all common chars used by native speakers of that language. 'C' is a special locale code since it disables all translations and programs use English texts as required by POSIX standard. Character encoding is highly dependent on the underlying operation system then.^3^
     25//
     26 I'm not sure to what the above refers. Which search? What locale argument?
     27 I don't think the character encoding plays any role here (we deal with unicode internally,
     28 catalogs themselves are always encoded in UTF-8) - cboos
    2429
    2530First matching translation will replace the default text what by gettext convention is the same as the msgid, that is used, if all attempts fail to find an exact matching translation.
     
    3136==== Import i18n/l10n helper programs ====
    3237For a '''fictional plugin 'foo' ''' just add
    33 {{{
     38{{{#!python
    3439from trac.util.translation import domain_functions
    3540
     
    3843}}}
    3944at the beginning of the main python script file.
     45  // TODO  I think I'll change that to: //
     46{{{#!python
     47from trac.util.translation import domain_functions
     48
     49_, tag_, N_, add_domain = domain_functions('foo', ('_', 'tag_', 'N_', 'add_domain'))
     50}}}
     51 //FIXME give the list of available domain functions//
    4052
    4153To bring in the compiled message catalog for actually using plugin's message catalogs you'll have to extend the `__init__` function of plugins main class as well. This could be done like this:
    42 {{{
     54{{{#!python
    4355    def __init__(self):
    44         self._version = None
    45         self.ui = None
    4656        # bind the 'foo' catalog to the specified locale directory
    4757        locale_dir = pkg_resources.resource_filename(__name__, 'locale')
     
    5060assuming that folder `locale` will reside in `./foo/locale/` within the directory structure (as observable inside the Python egg after packaging).
    5161
    52 [FIXME: explain what _version and ui are used for or leave them out, if unnecessary here]
    53 
    5462If you didn't have it in the source by now, add another import statement
    55 {{{
     63{{{#!python
    5664import pkg_resources
    5765}}}
     
    5967
    6068The i18n/l10n helper programs are available inside the plugin now, but if the plugin code contains several python script files and you encounter text for translation in one of them too, you need to import the functions from the main script, say it's name is `api.py`, there:
    61 {{{
     69{{{#!python
    6270from api import _, tag_, N_
     71}}}
     72{{{#!comment
     73resume review here...
    6374}}}
    6475
     
    183194== Related resources ==
    184195
    185 ^1^ http://en.wikipedia.org/wiki/Internationalization_and_localization - Internationalization and localization[[BR]]
    186 ^2^ http://en.wikipedia.org/w/index.php?title=Multilingualism&section=18 - Multilingualism in computing[[BR]]
    187 ^3^ http://www.gnu.org/software/gettext/manual/gettext.html#Locale-Names - GNU 'gettext' utilities: Locale Names[[BR]]
    188 ^4^ http://www.gnu.org/software/gettext/manual/gettext.html#Editing - GNU 'gettext' utilities: Editing PO Files[[BR]]
    189 ^5^ http://techbase.kde.org/Localization/Concepts/PO_Odyssey - PO Odyssey in '!Localization/Concepts' section of KDE !TechBase
     196[=#a1 ^1^] http://en.wikipedia.org/wiki/Internationalization_and_localization - Internationalization and localization[[BR]]
     197[=#a2 ^2^] http://en.wikipedia.org/w/index.php?title=Multilingualism&section=18 - Multilingualism in computing[[BR]]
     198[=#a3 ^3^] http://www.gnu.org/software/gettext/manual/gettext.html#Locale-Names - GNU 'gettext' utilities: Locale Names[[BR]]
     199[=#a4 ^4^] http://www.gnu.org/software/gettext/manual/gettext.html#Editing - GNU 'gettext' utilities: Editing PO Files[[BR]]
     200[=#a5 ^5^] http://techbase.kde.org/Localization/Concepts/PO_Odyssey - PO Odyssey in '!Localization/Concepts' section of KDE !TechBase