<?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>Peter Törnstrand &#187; localization</title>
	<atom:link href="http://www.tornstrand.com/category/localization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tornstrand.com</link>
	<description>Drupal, PHP and web developing</description>
	<lastBuildDate>Fri, 16 Apr 2010 07:04:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Zend_Form custom/localized error messages</title>
		<link>http://www.tornstrand.com/2008/06/27/zend_form-customlocalized-error-messages/</link>
		<comments>http://www.tornstrand.com/2008/06/27/zend_form-customlocalized-error-messages/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 19:45:25 +0000</pubDate>
		<dc:creator>peter</dc:creator>
				<category><![CDATA[localization]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_form]]></category>

		<guid isPermaLink="false">http://www.tornstrand.com/2008/06/27/zend_form-customlocalized-error-messages/</guid>
		<description><![CDATA[I&#8217;ve been playing around with Zend_Form and Zend Framework 1.5 these last couple of days. Like most people I hade some trouble getting used to the decorator part of the form API. Decorators are used to style form elements for rendering in the browser, adding tags before, after and wrapping the form element itself. After [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with <a href="http://framework.zend.com/manual/en/zend.form.html">Zend_Form</a> and <a href="http://framework.zend.com/">Zend Framework</a> 1.5 these last couple of days. Like most people I hade some trouble getting used to the decorator part of the form API. Decorators are used to style form elements for rendering in the browser, adding tags before, after and wrapping the form element itself. After much swearing I finally found <a href="http://devzone.zend.com/article/3450-Decorators-with-Zend_Form">this article</a> in the Zend Developer forums that did a pretty good job explaining it.</p>
<p>The next problem I ran into was customizing the form error messages, not just customizing the messages but also localizing them. The framework has default error messages defined for all validators you can use with your form elements but they are kind of stiff and I doubt anyone really want&#8217;s to use them. It turns out the solution is really simple.</p>
<p>To attach a validator to a form element you normally write something like this:</p>
<p><code>$textField = new Zend_Form_Element_Text("myField");<br />
$textField-&gt;addValidator(new Zend_Validate_NotEmpty());</code></p>
<p>If you want to customize the message and maybe even localize it you simply init a validator and manuelly set the message via the <em>setMessage</em> method, like this:</p>
<p><code>$notEmpty = new Zend_Validate_NotEmpty();<br />
$notEmpty-&gt;setMessage("You have to enter a value");<br />
$textField = new Zend_Form_Element_Text("myField");<br />
$textField-&gt;addValidator($notEmpty);</code></p>
<p>If you like to localize the error message, replace the setMessage call with:</p>
<p><code>$notEmpty-&gt;setMessage($this-&gt;getView()-&gt;translate("You have to enter a value"));</code></p>
<p>It&#8217;s just that simple <img src='http://www.tornstrand.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.tornstrand.com/2008/06/27/zend_form-customlocalized-error-messages/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>String localization with gettext and Zend Framework</title>
		<link>http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/</link>
		<comments>http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/#comments</comments>
		<pubDate>Sat, 29 Mar 2008 12:57:48 +0000</pubDate>
		<dc:creator>peter</dc:creator>
				<category><![CDATA[localization]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/</guid>
		<description><![CDATA[There&#8217;s a lot more to localizing a web site then just replacing strings with diffrent languages (for an exelent introduction read the ZF documentation on Zend_Locale). But in this post I will talk about string localization, and especially string localization using gettext and .mo-files with Zend Framework. The Zend Framework offers many ways for you [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a lot more to localizing a web site then just replacing strings with diffrent languages (for an exelent introduction read the <a href="http://framework.zend.com/manual/en/zend.locale.html">ZF documentation on Zend_Locale</a>). But in this post I will talk about string localization, and especially string localization using <a href="http://www.gnu.org/software/gettext/">gettext</a> and .mo-files with <a href="http://framework.zend.com/">Zend Framework</a>.<span id="more-12"></span></p>
<p>The Zend Framework offers many ways for you to store your translated strings. It could be an array, a CSV or XML file or you could use gettext which we will be using in the examples below.</p>
<p>First you need an example application, it doesn&#8217;t need to be more complicated than a clean installation of ZF with a IndexController and a matching view, and of course the bootstrap.</p>
<p>Ok, so the first thing we will do is to load the <a href="http://framework.zend.com/manual/en/zend.translate.html">Zend_Translate</a> class in our bootstrap, load the language files, and save the object in the registry using <a href="http://framework.zend.com/manual/en/zend.registry.html">Zend_Registry</a>.</p>
<p><code>Zend_Loader::loadClass('Zend_Translate');<br />
Zend_Loader::loadClass('Zend_Registry');<br />
$translate = new Zend_Translate('gettext', './languages');<br />
$registry = Zend_Registry::getInstance();<br />
$registry-&gt;set('Zend_Translate', $translate);</code></p>
<p>Ok, so what we did here was to first load the classes that we need, the Zend_Translate and Zend_Registry classes. Next we create the Zend_Translate object and pass in the directory containing all of our different languages we will support in our application, in this case we will be supporting swedish and english language. Don&#8217;t worry about the language files, I will explain how you create those later on. Finally we save our Zend_Translate object to the registry so we don&#8217;t need to create our object over and over again in our application.</p>
<p>The next step is to dive into our applications views to identify the strings we want translated. In our example we only have one view, the index view of our IndexController. So let&#8217;s open that up and have a look at it.</p>
<p><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;<br />
&lt;title&gt;Zend_Translate example&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Hello World!&lt;/h1&gt;<br />
&lt;p&gt;This is a Zend_Translate demonstration&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>Here we have two strings that needs to be localized, so we wrap these strings in a call to the translate object.</p>
<p><code>&lt;h1&gt;&lt;?php echo $this-&gt;translate("Hello World!"); ?&gt;&lt;/h1&gt;<br />
&lt;p&gt;&lt;?php echo $this-&gt;translate("This is a Zend_Translate demonstration"); ?&gt;&lt;/p&gt;</code></p>
<p>Zend Framwork 1.5 ships with the Zend_View_Helper_Translate which makes translating strings easy, before you were forced to translate the strings in the controller or write your own translation view helper.</p>
<p>Now we have made all the modifications to our application that needs to be done to support string localization. Now it&#8217;s time to create our translation files, the  .mo-files. First thing you want to do is to download <a href="http://www.poedit.net/">Poedit</a>. Poedit is an application that helps you gather all the strings that should be translated from your application and presents them in a interface for you to translate. The program is open source and exists in versions for all major operating systems.</p>
<p>Lets fire up Poedit and try to import the strings. Click File -&gt; New catalogue and a settings windows should appear.</p>
<p><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_settings.png" title="Poedit - Settings form"></a></p>
<p style="text-align: center"><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_settings.png" title="Poedit - Settings form"><img src="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_settings.png" alt="Poedit - Settings form" /></a></p>
<p>Fill in the settings so it looks something like the above image. Then click the Paths tab.</p>
<p><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_paths.png" title="Poedit - Paths"></a></p>
<p style="text-align: center"><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_paths.png" title="Poedit - Paths"><img src="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_paths.png" alt="Poedit - Paths" /></a></p>
<p>Here you first enter the base path to your application and then you add the directories in your application that contains the files with the strings to be translated (your views). When your done click the Keywords tab.</p>
<p><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_keywords.png" title="Poedit - Keywords"></a></p>
<p style="text-align: center"><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_keywords.png" title="Poedit - Keywords"><img src="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_keywords.png" alt="Poedit - Keywords" /></a></p>
<p>Here you enter the name of the function you are calling to translate a string, in Zend Framework the function name is translate ($this-&gt;<strong>translate</strong>(&#8220;String&#8221;)).</p>
<p>When you&#8217;re done click OK. If your setup is anything like mine you will now encounter a error saying the catalogue update failed.</p>
<p><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_error.png" title="Poedit - Error"></a></p>
<p style="text-align: center"><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_error.png" title="Poedit - Error"><img src="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_error.png" alt="Poedit - Error" /></a></p>
<p>This was expected and happens because Poedit don&#8217;t know to look for strings in .phtml files that are used by Zend Framework. Click File -&gt; Preferences and click the tab Code readers. Select the PHP item and click Edit.</p>
<p><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_php.png" title="Poedit - PHP settings"></a></p>
<p style="text-align: center"><a href="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_php.png" title="Poedit - PHP settings"><img src="http://www.tornstrand.com/wp-content/uploads/2008/03/poedit_php.png" alt="Poedit - PHP settings" /></a></p>
<p>Edit the settings so they reflect the settings in the picture above.</p>
<p>Now when you click update the strings that we wrapped in the translate function call should appear in Poedit in a list with the original strings to the left and the translations to the right. Get to work translating your strings and then save the catalogue. I&#8217;m keeping all my language files in a directory called languages in the web application root next to the library and application directories.</p>
<p>The application will automatically detects locale based on the HTTP_ACCEPT_LANGUAGE header sent by browsers. It will try to match the available languages to the users preffered language and if no match is made it will use the default language (the strings in your view files). You can customize this to use other methods but that&#8217;s out of the scope for this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
