<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joys and rants of a Python programmer &#187; i18n</title>
	<atom:link href="http://blog.pow.lt/tag/i18n/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pow.lt</link>
	<description>Pow! Wham, bam, kapow!</description>
	<lastBuildDate>Wed, 02 Dec 2009 16:04:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Testing your translations for bugs</title>
		<link>http://blog.pow.lt/2009/11/29/testing-your-translations-for-bugs/</link>
		<comments>http://blog.pow.lt/2009/11/29/testing-your-translations-for-bugs/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 22:23:02 +0000</pubDate>
		<dc:creator>Ignas Mikalajūnas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[zc.buildout]]></category>

		<guid isPermaLink="false">http://blog.pow.lt/?p=40</guid>
		<description><![CDATA[The problem


We have released the Polish version
of Ututi a week ago, and that taught me
a couple of lessons:



  I18n in Pylons support is lacking
  You must have tests for your translations just as you test your code



There are some flaws in Pylons I18n that got me longing for Zope3 I18n.


  
  [...]]]></description>
			<content:encoded><![CDATA[<h2>The problem</h2>

<p>
We have released the Polish version
of <a href="http://ututi.pl">Ututi</a> a week ago, and that taught me
a couple of lessons:
</p>

<ol>
  <li>I18n in Pylons support is lacking</li>
  <li>You must have tests for your translations just as you test your code</li>
</ol>

<p>
There are some flaws in Pylons I18n that got me longing for Zope3 I18n.

<ul>
  <li>
  There is no &#8216;default&#8217; translation. When I used Zope3 I used to be able to say
  <code>_('ok-button-text', default='OK')</code>. With Pylons I have
  to have an English to English translation, which means that
  translators cannot see the default text, which leads to mistakes
  like <code>'ok-mygtuko-tekstas'</code> instead of <code>'Gerai'</code>
  or <code>'OK'</code>
  </li>
  <li>
  Using Python formatting directives leads to tracebacks if there are
  bugs in the translations. If someone translates <code>'Hi %(fullname)s!'</code>
  into <code>'Labas %(fullname)'</code>, all the pages that try showing this
  message will end up as error pages, because of the missing <code>'s'</code>.
  </li>
  <li>
    Mako templates are very very very translation unfriendly. Simple
    translatable texts look like: <code>${_('Hi!')}</code> and more complex texts
    in our templates end up looking like:
<pre class="brush: plain;">
${_('A new file %(link_to_file)s was uploaded for the subject %(subject_title)s') % dict(
        filename=h.link_to(c.filename, c.file_url),
        subject_title=c.subject_title)}
</pre>

  It looks like perl already, I am not even talking about what happens
  when you have something like an email, with multiple lines of text
  and multiple embedded links, to translate. Though Zope&#8217;ish
<pre class="brush: plain;">
&lt;span i18n:translate=&quot;&quot;&gt;A new file
&lt;a tal:attributes=&quot;href view/file_url&quot; i18n:name=&quot;link_to_file&quot; tal:content=&quot;view/filename&quot; /&gt;
was uploaded for subject
&lt;tal:block i18n:name=&quot;subject_title&quot; tal:content=&quot;view/subject_title&quot; /&gt;
&lt;/span&gt;
</pre>
  is just as ugly for short pieces of text I would surely prefer it
  for something that is more than 2 lines of text. An Emacs macro that
  wraps any selected text in <code>${_('&lt;text here&gt;')}</code>
  helps to reduce the strain, but I&#8217;d prefer dedicated markers for
  translatable text, ones that would be easier on the Shift-pressing
  hand than what we have now.
  </li>
  <li>
  Babel has problems extracting some strings, some times, from Mako
  templates. The workaround -
    <ul>
      <li>run tests (yay almost 100% coverage),</li>
      <li>copy template cache into your &#8217;src&#8217;,</li>
      <li>extract the translations,</li>
      <li>remove the copy.</li>
      <li>And then remove all the fuzzy markers from plural strings
      that are marked as <code>#&nbsp;,python-format</code> by babel, which
      as you can guess is all the plural strings. We don&#8217;t want to
      guess the position of the hours in a sentence like <code>'uploaded
      %(hours)s ago'</code></li>
    </ul>
  I still haven&#8217;t managed to find the time to reduce the problematic
  templates to something that I can fit into a bug report. <em>(Update: Seems like the bug that is causing some (if not all) of the problems was reported 8 months ago in the <a href="http://www.makotemplates.org/trac/ticket/106">mako bug tracker</a>)</em>
  </li>
</ul>
</p>

<h2>The solution</h2>

<p>
Now that we&#8217;re done explaining what&#8217;s wrong, let&#8217;s talk about
something more constructive &#8211; making sure that tracebacks do not
happen, because of typos in translations. First &#8211; we need a nice
translation tester. Candidates:
</p>

<ol>
  <li>potest</li>
  <li>gettext-lint</li>
  <li>pofilter from translate-toolkit</li>
</ol>

<h3>potest</h3>

<p>
Last commit 78 weeks ago. Can&#8217;t parse plural forms. Verdict &mdash; unusable.
</p>

<h3>gettext-lint</h3>

<p>
Seems to be written in Python, but packaging uses autoconf !? which
generates a Makefile that does <strong>nothing</strong>. Seems
cumbersome to use, can&#8217;t check html tags (some translators get this
idea of translating &lt;strong&gt; into the target language), does not
handle %(foo)s syntax.
</p>

<h3>pofilter</h3>

<p>
The tarball has all parts that are needed to package this tool as an
egg, but it is not easy_installable. So what do we do? The same thing
we do every night,
<ul>
  <li><code>extract</code>,</li>
  <li><code>python setup.py sdist</code>,</li>
  <li><code>scp dist/translate-toolkit.tar.gz pow.lt:~/www/eggs/</code></li>
</ul>
</p>

<p>
Now we just make a new virtualenv and
easy_install <em>translate-toolkit</em> in it:
</p>

<pre class="brush: bash;">
virtualenv translations
cd translations
bin/easy_install translate-toolkit --find-links=http://pow.lt/eggs
</pre>

<p>
And test it on one of the projects in my src that has a <strong>lot</strong> of translations &mdash; SchoolTool:
</p>

<pre class="brush: bash;">
bin/pofilter ../trunk/schooltool/src/schooltool/locales \
               -o ./out -t printf -t xmltags -t variables --openoffice
</pre>

<p>
(I pass the <code>--openoffice</code> parameter, so that it would recognize
Zope3 translation markers, like <code>${calendar_title}</code>, as variables)
</p>

<p>
This results in a bunch of PO files in the ./out, each file containing
the errors for the corresponding translation file.
</p>

<pre class="brush: plain;">
# (pofilter) variables: do not translate: ${event_title}
#: /src/schooltool/app/browser/templates/recevent_delete.pt:4
#: /src/schooltool/app/browser/templates/recevent_delete.pt:14
msgid &quot;Deleting a repeating event (${event_title})&quot;
msgstr &quot;Šalinamas pasikartojantis įvykis (${event title}&quot;
</pre>

<p>
Pretty cool, eh? pofilter has most of the functions I need, I just
have to integrate it into my sandbox and extend it a little bit. So
first I add:
</p>

<pre class="brush: plain;">
[test_translations]
find-links = http://pow.lt/eggs/
recipe = zc.recipe.egg
eggs = translate-toolkit
       lxml
entry-points = pofilter=translate.filters.pofilter:main
</pre>

<p>
to my buildout.cfg.
</p>

<p>
 (I added an entry point to the [test_translations] section, because
 all the translate-toolkit scripts seem to be defined as plain scripts
 and not registered as console_script entry points)
</p>

<p>
Customizing pofilter is slightly difficult. I could not find any
defined hooks that would allow me to customize the functionality. And
&#8220;xmltags&#8221; seems to be picking up all the translated &#8220;title&#8221; attributes
on links, which is annoying. So after reporting this as a bug I just
find the &#8220;main&#8221; function in translate.filters.pofilter, copy it and
produce &#8211; this:
</p>

<pre class="brush: python;">
from translate.filters.pofilter import cmdlineparser
from translate.filters.checks import StandardChecker

from translate.filters.checks import CheckerConfig

ututiconfig = CheckerConfig(
    canchangetags = [(&quot;a&quot;, &quot;title&quot;, None)]
    )

class UtutiChecker(StandardChecker):

    def __init__(self, **kwargs):
        checkerconfig = kwargs.get(&quot;checkerconfig&quot;, None)
        if checkerconfig is None:
            checkerconfig = CheckerConfig()
            kwargs[&quot;checkerconfig&quot;] = checkerconfig
        checkerconfig.update(ututiconfig)
        StandardChecker.__init__(self, **kwargs)


def main():
    parser = cmdlineparser()
    parser.add_option(&quot;&quot;, &quot;--ututi&quot;, dest=&quot;filterclass&quot;,
        action=&quot;store_const&quot;, default=None, const=UtutiChecker,
        help=&quot;use the standard checks for Ututi translations&quot;)

    parser.run()
</pre>

<p>
Then registered this new function as an entry point instead of the old
one:
</p>

<pre class="brush: plain;">
[test_translations]
find-links = http://pow.lt/eggs/
recipe = zc.recipe.egg
eggs = ututi
entry-points = pofilter=ututi.tests.translations:main
</pre>

<p>
Now if I will pass &#8220;&#8211;ututi&#8221; to pofilter it will not raise warnings for
title attributes anymore.
</p>

<h2>Icing on the cake</h2>

<p>
  Tests are pretty useless if they are not run, and we want to run our
  tests after every modification to the code, and after every commit
  to our git server. As I am using make as my tool to run everything,
  I just added these two targets to the Makefile.
</p>

<pre class="brush: plain;">
.PHONY: test_translations
test_translations: bin/pofilter
	bin/pofilter --progress=none -t xmltags -t printf --ututi src/ututi/i18n/ -o parts/test_translations/
	diff -r -u src/ututi/tests/expected_i18n_errors/ parts/test_translations/

.PHONY: update_expected_translations
update_expected_translations: bin/pofilter
	bin/pofilter --progress=none -t xmltags -t printf --ututi src/ututi/i18n/ -o parts/test_translations/
	rm -rf src/ututi/tests/expected_i18n_errors/
	mv parts/test_translations/ src/ututi/tests/expected_i18n_errors/
</pre>

<p>
  Even after changes pofilter is still reporting 3-4 false positives,
  that I will have to resolve with our translators, so instead of
  expecting absolutely no output, I am just asking for the output to
  be identical to the old one. If it is a known/accepted failure
  &ndash; we let it be.
</p>

<p>
  And of course &#8211; made our dear Hudson run this after every commit,
  for when I forget to do it myself.
</p>

<h2>Fin!</h2>
<a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F11%2F29%2Ftesting-your-translations-for-bugs%2F&amp;linkname=Testing%20your%20translations%20for%20bugs" title="Digg" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F11%2F29%2Ftesting-your-translations-for-bugs%2F&amp;linkname=Testing%20your%20translations%20for%20bugs" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F11%2F29%2Ftesting-your-translations-for-bugs%2F&amp;linkname=Testing%20your%20translations%20for%20bugs" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F11%2F29%2Ftesting-your-translations-for-bugs%2F&amp;linkname=Testing%20your%20translations%20for%20bugs" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F11%2F29%2Ftesting-your-translations-for-bugs%2F&amp;linkname=Testing%20your%20translations%20for%20bugs"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.pow.lt/2009/11/29/testing-your-translations-for-bugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
