<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../../assets/xml/rss.xsl" media="all"?><rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Gladko kot po sluzi (gettext)</title><link>https://polz.si/</link><description></description><atom:link href="https://polz.si/sl/categories/gettext.xml" type="application/rss+xml" rel="self"></atom:link><language>sl</language><lastBuildDate>Wed, 28 Sep 2016 14:51:33 GMT</lastBuildDate><generator>https://getnikola.com/</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Turbogears i18n pains</title><link>https://polz.si/sl/posts/Turbogears_i18n_pains/</link><dc:creator>Polž</dc:creator><description>&lt;div&gt;&lt;p&gt;The final problem I have run into with TurboGears is the poor documentation regarding i18n with Genshi templates. The only way I have been able to get gettext to work for this blog was by using sourcecode from &lt;a class="reference external" href="http://fedorahosted.org/transifex/"&gt;Transifex&lt;/a&gt;. To get i18n to work with genshi templates, I added this to config/app.cfg:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
session_filter.on = True
i18n.run_template_filter = True

i18n.domain = "cblog"
i18n.locale_dir = "locales/"
i18n.po_view_dir = "locales/view/"

i18n.locale_dir = "locales/"
&lt;/pre&gt;
&lt;p&gt;and this to controllers.py:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
# i18n support
from turbogears.i18n import gettext
from genshi.filters import Translator
import turbogears.view

def genshi_loader_callback(template):
    template.filters.insert(0, Translator(gettext))

def init_callback():
    turbogears.view.engines['genshi'].loader.callback = genshi_loader_callback

turbogears.startup.call_on_startup.append( init_callback )

config.update({'genshi.loader_callback': genshi_loader_callback})

# i18n support - end
&lt;/pre&gt;
&lt;p&gt;To create the translation files, I followed the instructions from the &lt;a class="reference external" href="http://genshi.edgewall.org/wiki/GenshiTutorial/Internationalization"&gt;Genshi i18n page&lt;/a&gt;. First, I created a mappings.cfg file containing:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
# Extraction from Python source files
[python: **.py]
# Extraction from Genshi HTML and text templates
[genshi: **/templates/**.html]
&lt;/pre&gt;
&lt;p&gt;And then ran:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
pybabel extract -o locales/cblog.pot -F ./mappings.cfg cblog
pybabel init -D cblog -i locales/cblog.pot -d locales/ -l en # for the english language
pybabel compile -D cblog -d locales/ -f --statistics
&lt;/pre&gt;
&lt;p&gt;Unfortunately, in order for the changes to the translated strings to take effect, the application needs to be restarted. The need to restart the server each time a translated string changes seems silly. Also, I spent more than 3 days hunting for documentation on how to actually get the i18n to work. Hopefully, doing the same with Django will be less painful.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://polz.si/sl/posts/Turbogears_i18n_pains/"&gt;Več o tem…&lt;/a&gt; (še 1 min za branje preostanka)&lt;/p&gt;&lt;/div&gt;</description><guid>https://polz.si/sl/posts/Turbogears_i18n_pains/</guid><pubDate>Mon, 19 Jan 2009 11:37:20 GMT</pubDate></item><item><title>Better templates using gettext</title><link>https://polz.si/sl/posts/Better_templates_using_gettext/</link><dc:creator>Polž</dc:creator><description>&lt;div&gt;&lt;p&gt;which is supposed to produce "category" if there is exactly one category and "categories" if there are multiple categories.&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;There is a better way to handle this, which is to use the ngettext function. Like this::&lt;/dt&gt;
&lt;dd&gt;${ngettext("%d category", "%d categories", numtags) % numtags}&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;By using ngettext it becomes possible to translate the blog into languages where counting is done differently than in English. Which is what I intend to do next.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://polz.si/sl/posts/Better_templates_using_gettext/"&gt;Več o tem…&lt;/a&gt; (še 1 min za branje preostanka)&lt;/p&gt;&lt;/div&gt;</description><guid>https://polz.si/sl/posts/Better_templates_using_gettext/</guid><pubDate>Mon, 19 Jan 2009 10:31:54 GMT</pubDate></item></channel></rss>