<?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>Python Blog - All about python &#187; translation problems</title>
	<atom:link href="http://www.python-blog.com/tag/translation-problems/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.python-blog.com</link>
	<description>and technologies around</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:58:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>pylons translating custom error messages.</title>
		<link>http://www.python-blog.com/2010/02/08/pylons-translating-custom-error-messages/</link>
		<comments>http://www.python-blog.com/2010/02/08/pylons-translating-custom-error-messages/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:02:53 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[lazy translations]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[pylons translation issues]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[translation problems]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=148</guid>
		<description><![CDATA[I&#8217;d created another simple application in pylons, final step was to translate it from English to Polish.
I used the babel extractor recommended by pylons and that went really smooth.
My application have a language selector available anytime from menu.
I saw that even this switch worked very well for mako templates and string inside controllers,
 it did [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d created another simple application in pylons, final step was to translate it from English to Polish.</p>
<p>I used the babel extractor recommended by pylons and that went really smooth.</p>
<p>My application have a language selector available anytime from menu.<br />
I saw that even this switch worked very well for mako templates and string inside controllers,<br />
 it did not work when displaying my custom messages that i over rid in validators<br />
and also did not work in my own custom validators that inherit from FancyValidator.<br />
So i started to investigate this.</p>
<p>The first problem of not translating custom error messages in the regular formencode validators was easily being fixed by using lazy_ugettex method.</p>
<pre class="brush:python">
def _(s):
    #fix for translation error messages
    return lazy_ugettext(s)
</pre>
<p>This method allowed me to use lazy_ugettext to translate.<br />
So now when in the validators your translating a string using _(&#8216;this string is for translate&#8217;) method it&#8217;s actually wrapper for lazy_ugettext function.<br />
Lazy translation is when you translate a string when it is accessed, not when the _() or other functions are called.</p>
<p>The second problem of translating custom error messages in your own validator was a bigger problem</p>
<p>I was looking for a solution for some time, and found a nice trick. You have to use your own<br />
state_obj with static _() function. As example below for custom validation function.</p>
<pre class="brush:python">
class ValidLoginNames(formencode.validators.FancyValidator):

    messages = {'invalid_name':_('you cannot use %(username)s as login')}

    def validate_python(self, value, state):
        banned_names = ['admin', 'administrator', 'root']
        if value in banned_names:
            raise formencode.Invalid(self.message('invalid_name', state = State_obj, username = value), value, state)

#this is needed to translate the messages using _()
class State_obj(object):
    _ = staticmethod(_)
</pre>
<p>Those two trick now let&#8217;s you translate custom validators without a problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2010/02/08/pylons-translating-custom-error-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
