<?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; php</title>
	<atom:link href="http://www.tornstrand.com/tag/php/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>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>
		<item>
		<title>Garbeled data using PDO and MySQL</title>
		<link>http://www.tornstrand.com/2008/01/20/garbeled-data-using-pdo-and-mysql/</link>
		<comments>http://www.tornstrand.com/2008/01/20/garbeled-data-using-pdo-and-mysql/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 15:00:43 +0000</pubDate>
		<dc:creator>peter</dc:creator>
				<category><![CDATA[databases]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pdo]]></category>

		<guid isPermaLink="false">http://www.tornstrand.com/index.php/2008/01/20/garbeled-data-using-pdo-and-mysql/</guid>
		<description><![CDATA[Ran into some trouble the other day while emigrating a PHP solution from our development server to the production server. The application was working fine on the development server but once moved to the production server it started to behave strangely. Data returned from the datebase via stored procedures was contaning lots of garbeled data [...]]]></description>
			<content:encoded><![CDATA[<p>Ran into some trouble the other day while emigrating a PHP solution from our development server to the production server. The application was working fine on the development server but once moved to the production server it started to behave strangely.<span id="more-4"></span></p>
<p>Data returned from the datebase via stored procedures was contaning lots of garbeled data and results from previous select statments. So i turned to Google to find the answer to this strange behavior. Turns out this problem appears when executing a stored procedure contaning a <a href="http://dev.mysql.com/doc/refman/5.0/en/sqlps.html">PREPARE</a> statement. Why would one do this you might ask, well it has to do with MySQL inability to handle variables in the LIMIT clause. The only way to do paging with a stored procedure in MySQL is to use a PREPARE statement inside the procedure.</p>
<pre>DELIMITER $$
CREATE PROCEDURE paging (IN pageSize INT, IN position INT)
BEGIN
	PREPARE stmt FROM CONCAT("SELECT * FROM table LIMIT ", position ,", ", pageSize);
	EXECUTE stmt;
END $$
DELIMITER ;</pre>
<p>Since this problem only occured on the production server the solution was pretty clear, simply update MySQL and PHP to the latest stable releases and you&#8217;re fine.</p>
<p>Related links: <a href="http://bugs.mysql.com/bug.php?id=19008">Bug report on php.net</a>, <a href="http://bugs.mysql.com/bug.php?id=19008">Bug report on Mysql.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tornstrand.com/2008/01/20/garbeled-data-using-pdo-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
