<?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>Bill Dawson &#187; i18n</title>
	<atom:link href="http://www.billdawson.com/category/i18n/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.billdawson.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 20:50:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How one becomes lazy (when one is being translated by Django)</title>
		<link>http://www.billdawson.com/how-one-becomes-lazy-when-one-is-being-translated-by-django/</link>
		<comments>http://www.billdawson.com/how-one-becomes-lazy-when-one-is-being-translated-by-django/#comments</comments>
		<pubDate>Sat, 17 May 2008 04:07:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[i18n]]></category>

		<guid isPermaLink="false">http://www.billdawson.com/blog/?p=8</guid>
		<description><![CDATA[I&#8217;ve enjoyed digging even deeper into the lazy text questions that I came up with last time. The &#8220;thingamajig&#8221; returned by ugettext_lazy indeed waits until unicode is called upon it, then it runs through translation. You can see this by the way ugettext_lazy is defined. It&#8217;s in core/utils/translation/__init__.py: core/utils/translation/__init__.py ugettext_lazy = lazy&#40;ugettext, unicode&#41; That call [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;ve enjoyed digging even deeper into the lazy text questions that I came up with <a title="How lazy can text get?" href="http://www.billdawson.com/blog/how-lazy-can-text-get/">last time</a>.  The &#8220;thingamajig&#8221; returned by <code>ugettext_lazy</code> indeed waits until <code>unicode</code> is called upon it, then it runs through translation.</p>
<p>You can see this by the way <code>ugettext_lazy</code> is defined.  It&#8217;s in core/utils/translation/__init__.py:</p>
<div class="geshi no python">
<div class="head">core/utils/translation/__init__.py</div>
<ol>
<li class="li1">
<div class="de1">ugettext_lazy = lazy<span class="br0">&#40;</span>ugettext, <span class="kw2">unicode</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>That call to <code>lazy</code> produces a bit of functional programming magic (see core/utils/functional.py for the <code>def lazy</code>) that results in <code>ugettest_lazy</code> calling <code>ugettext</code> (the standard translation function) whenever its unicode value is requested (i.e., every time it&#8217;s used as a string.)</p>
<p>Fancy stuff, to my little brain anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.billdawson.com/how-one-becomes-lazy-when-one-is-being-translated-by-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How lazy can text get?</title>
		<link>http://www.billdawson.com/how-lazy-can-text-get/</link>
		<comments>http://www.billdawson.com/how-lazy-can-text-get/#comments</comments>
		<pubDate>Wed, 14 May 2008 23:57:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[i18n]]></category>

		<guid isPermaLink="false">http://www.billdawson.com/blog/?p=7</guid>
		<description><![CDATA[Inspired by Malcolm Tredinnick&#8216;s appearance on TWiD 19, I decided to create a small dummy site and work through the steps of translating it to German. Setting up all the translation files was easy enough, but then the first thing that bit me was overlooking this instruction on Django&#8217;s Internationalization documentation page: Always use lazy [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Inspired by <a title="Malcolm's blog" href="http://www.pointy-stick.com/blog/">Malcolm Tredinnick</a>&#8216;s appearance on <a title="This Week in Django 19" href="http://blog.michaeltrier.com/2008/4/22/this-week-in-django-19-2008-04-20">TWiD 19</a>, I decided to create a small dummy site and work through the steps of translating it to German.</p>
<p>Setting up all the translation files was easy enough, but then the first thing that bit me was overlooking this instruction on Django&#8217;s <a title="Django Internationalization" href="http://www.djangoproject.com/documentation/i18n/">Internationalization</a> documentation page:</p>
<blockquote><p>Always use lazy translations in Django models.</p></blockquote>
<p>Such a short, innocent sentence!  Easy to miss.  But the author (Malcolm himself, I believe) means it.  <em>Always</em> use lazy translations in models.</p>
<p>My model contained:</p>
<div class="geshi no python">
<div class="head">models.py</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">from</span> django.<span class="me1">db</span> <span class="kw1">import</span> models</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">from</span> django.<span class="me1">newforms</span> <span class="kw1">import</span> ModelForm</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">from</span> django.<span class="me1">utils</span>.<span class="me1">translation</span> <span class="kw1">import</span> ugettext as _</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">PROJECT_STATUSES = <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="st0">&#39;active&#39;</span>, _<span class="br0">&#40;</span><span class="st0">&#39;active&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="st0">&#39;someday&#39;</span>, _<span class="br0">&#40;</span><span class="st0">&#39;someday&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Project<span class="br0">&#40;</span>models.<span class="me1">Model</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; title = models.<span class="me1">CharField</span><span class="br0">&#40;</span>max_length=<span class="nu0">200</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; description = models.<span class="me1">CharField</span><span class="br0">&#40;</span>max_length=<span class="nu0">1000</span>, blank=<span class="kw2">True</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; status = models.<span class="me1">CharField</span><span class="br0">&#40;</span>max_length=<span class="nu0">10</span>, choices=PROJECT_STATUSES<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#&#8230;..</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> ProjectForm<span class="br0">&#40;</span>ModelForm<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">class</span> Meta:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; model = Project</div>
</li>
</ol>
</div>
<p>I then displayed the model form (ProjectForm) in a template.  The template also contained a language switcher, following the directions concerning the <a title="set_language" href="http://www.djangoproject.com/documentation/i18n/#the-set-language-redirect-view">set_language redirect view.</a> I ran the app and used the <code>set_language</code> redirect to set my language to German (the default language of the app was English.)  I expected the combobox &#8212; the one auto-generated by using the <code>choices</code> parameter when defining the <code>status</code> member of the model &#8212; to show me German options, because I&#8217;d defined those options (when setting the values of the <code>PROJECT_STATUSES</code> tuple) to have translated text values.</p>
<p>Of course they displayed English options instead of German, because I used <code>ugettext</code> instead of <code>ugettext_lazy</code>.  The question is, do I understand why <code>ugettext</code> doesn&#8217;t work?  One of my goals during <a title="Total Django Immersion" href="http://www.billdawson.com/blog/2008/05/15/its-time-for-total-django-immersion/">TDI</a> is to understand how and why things work the way they do in Django.  I haven&#8217;t dug into this yet, but I <em>think</em> it can be explained something like this: at the time the models.py is evaluated and, therefore, the time the <code>PROJECT_STATUSES</code> variable is filled, the thread (or whatever it&#8217;s called in this context) is still running under the site&#8217;s default locale.  Since that&#8217;s English, when <code>ugettext</code> gets called it just returns the original string, since I haven&#8217;t specified any English translations.  Only later (I guess when about to render a response?) is the user&#8217;s (i.e., session&#8217;s) locale evaluated and used.  By that time, <code>PROJECT_STATUSES</code> values have already been sent through the translation engine, so they&#8217;re not sent through again.</p>
<p>So maybe <code>ugettext_lazy</code> returns some sort of thingamajig that doesn&#8217;t call into the translation engine until its <code>__unicode__</code> function is accessed.  I would guess that&#8217;s what the documentation means by&#8230;</p>
<blockquote><p>The translation itself will be done when the string is used in a string context.</p></blockquote>
<p>I look forward to digging deeper by:</p>
<ul>
<li>Searching for and learning about the place in Django code where the switch from the default to the session-specific locale takes place.</li>
<li>Reading through the <code>ugetext_lazy</code> code to see how it works.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.billdawson.com/how-lazy-can-text-get/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

