Edgewall Software

Changes between Version 15 and Version 16 of CookBook/PluginL10N


Ignore:
Timestamp:
May 20, 2010, 12:57:11 AM (14 years ago)
Author:
hasienda <hoff.st@…>
Comment:

add information regarding msg text extraction from Genshi templates and Java scripts

Legend:

Unmodified
Added
Removed
Modified
  • CookBook/PluginL10N

    v15 v16  
    133133This is a somewhat time consuming task depending on the size of the plugin's code. If you initially fail to find all desired texts you may notice this by missing them from the message catalog later and come back to this step again. If the plugin maintainer is unaware of your i18n work or unwilling to support it and he adds more message without the translation function call, remember that you have to do the wrapping of these new texts too.
    134134
     135==== Text extraction from Genshi templates ====
    135136Message extraction for Genshi templates should be done auto-magically. However there is the markup `i18n:msg` available to ensure extraction even from less common tags. For a real-world example have a look at [changeset:9542/trunk/trac/ticket/templates/ Trac SVN changeset r9542] for marking previously undetected text in templates. But there are not all issues settled with some special cases for message extraction from Genshi templates, i.e. see [g:ticket:385 Genshi ticket 385]. You should search for similar issues you may encounter while trying to handle plugin templates.
    136137
    137 [FIXME: add more details about msg extraction from other files]
     138But Babel only does extract from Python scripts by default. To extract messages from Genshi templates as well, you'll have to declare the needed extractors in `setup.py`:
     139{{{#!diff
     140diff --git a/setup.py b/setup.py
     141--- a/setup.py
     142+++ b/setup.py
     143@@ -34,6 +35,21 @@
     144
     145 from setuptools import find_packages, setup
     146
     147+extra = {}
     148+try:
     149+    extractors = [
     150+        ('**.py',                'python', None),
     151+        ('**/templates/**.html', 'genshi', None),
     152+        ('**/templates/**.txt',  'genshi', {
     153+            'template_class': 'genshi.template:TextTemplate'
     154+        }),
     155+    ]
     156+    extra['message_extractors'] = {
     157+        'foo': extractors,
     158+    }
     159+except ImportError:
     160+    pass
     161+
     162 setup(
     163     name = 'foo',
     164     version = '0.12',
     165@@ -53,6 +69,7 @@
     166             'templates/*.txt',
     167             'htdocs/*.*',
     168             'htdocs/css/*.*',
     169+            'locale/*/LC_MESSAGES/*.mo',
     170         ]
     171     },
     172     install_requires = [
     173@@ -96,4 +113,5 @@
     174         ]
     175     },
     176     test_suite = '<path>.tests',
     177+    **extra
     178 )
     179}}}
     180
     181==== i18n for Java scripts ====
     182{{{#!comment
     183This is taken largely from studying code in http://trac-hacks.org/wiki/TracTicketTemplatePlugin. Some conclusions may be plain wrong, since I had not time to carefully investigate and verify this.
     184}}}
     185Note: This section has some preliminary content. Please handle with care and contribute your/any better knowledge on the subject.
     186
     187You'll have to start with marking text to get it handled by translation helper programs, so a line that was
     188{{{
     189    var answer = confirm("Submit ticket?");
     190}}}
     191before, will read like
     192{{{
     193    var answer = confirm("${_('Submit ticket?')}");
     194}}}
     195afterwards.
     196
     197Wrap whole code containing marked text with a text replacement function like
     198{{{
     199$.fn.extend({
     200        foo: function() {
     201        function _(message){ return message; }
     202        messages = [
     203            _('Submit ticket?'),
     204        ]
     205
     206        ...
     207
     208        function onFormSubmit(evt) {
     209            if (evt.type == "submit"){
     210                // confirm to save
     211                if ($("#ticket_status").length == 0 || $.trim($("#ticket_status$
     212                    var answer = confirm("${_('Submit ticket?')}");
     213                    if (!answer) {
     214                        return false;
     215                    }
     216                }
     217            }
     218        }
     219
     220        ...
     221
     222});
     223
     224}}}
     225
     226To extract messages from Java scripts, you'll have to extend  `setup.py` like shown in previous chapter for Genshi templates, but declare yet another extractor by listing it within the `extractors` list:
     227{{{
     228        ('**/templates/**.js',   'javascript', None),
     229}}}
    138230
    139231==== Register message catalog files for packaging ====
    140 To include the translated messages into the packaged plugin you need to add the path to the catalog files to `package_data` in `setup.py`:
     232To include the translated messages into the packaged plugin you need to add the path to the catalog files to `package_data` in the call for function `setup()` in `setup.py`:
    141233{{{#!diff
    142234diff -u a/setup.py b/setup.py