<?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>Code-It &#187; UnicodeDecodeError</title>
	<atom:link href="http://lois.di-qual.net/blog/tag/unicodedecodeerror/feed/" rel="self" type="application/rss+xml" />
	<link>http://lois.di-qual.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 30 Oct 2012 19:19:29 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Django &#8211; UnicodeDecodeError using i18n</title>
		<link>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/</link>
		<comments>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/#comments</comments>
		<pubDate>Mon, 30 Jul 2012 21:32:11 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[UnicodeDecodeError]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=83</guid>
		<description><![CDATA[If you&#8217;re using i18n in your django website, you may encounter this issue. It took me two hours to find where the problem came from: you have to use ugettext instead of gettext if your translated strings contains non-ascii characters. gettext => ugettext With gettext: 12345from django.utils.translation import gettext as _ define my_view&#40;request&#41;: &#160; &#160; [...]]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;re using i18n in your django website, you may encounter this issue. It took me two hours to find where the problem came from: you have to use <code class="codecolorer text default"><span class="text">ugettext</span></code> instead of <code class="codecolorer text default"><span class="text">gettext</span></code> if your translated strings contains non-ascii characters.</p>
<p><span id="more-83"></span></p>
<h2>gettext => ugettext</h2>
<p>With <code class="codecolorer text default"><span class="text">gettext</span></code>:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">translation</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">gettext</span> <span style="color: #ff7700;font-weight:bold;">as</span> _<br />
<br />
define my_view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span><span style="color: black;">&#41;</span> // Raises a <span style="color: #008000;">UnicodeDecodeError</span> because <span style="color: #483d8b;">&quot;é&quot;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">unicode</span></div></td></tr></tbody></table></div>
<p>With <code class="codecolorer text default"><span class="text">ugettext</span></code>:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">translation</span> <span style="color: #ff7700;font-weight:bold;">import</span> ugettext <span style="color: #ff7700;font-weight:bold;">as</span> _<br />
<br />
define my_view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span></div></td></tr></tbody></table></div>
<h2>ugettext or ugettext_lazy ?</h2>
<p>Two simple rules:</p>
<ul>
<li>Use <code class="codecolorer text default"><span class="text">ugettext_lazy</span></code> in global scopes (model fields, django settings, everything not wrapped by a function or method, &#8230;)</li>
<li>Use <code class="codecolorer text default"><span class="text">ugettext</span></code> everywhere else (in views, methods, functions, &#8230;)</li>
</ul>
<p>That&#8217;s it !</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
