<?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>M4d3L Blog&#039;s&#187; parameters</title>
	<atom:link href="http://www.m4d3l-network.com/tag/parameters/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.m4d3l-network.com</link>
	<description>Sky is NOT the limit!</description>
	<lastBuildDate>Sat, 30 Jul 2011 19:30:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<div id='fb-root'></div>
					<script type='text/javascript'>
						window.fbAsyncInit = function()
						{
							FB.init({appId: 178184625541198, status: true, cookie: true, xfbml: true});
						};
						(function()
						{
							var e = document.createElement('script'); e.async = true;
							e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
							document.getElementById('fb-root').appendChild(e);
						}());
					</script>	
						<item>
		<title>Add language route to your Zend Framework project.</title>
		<link>http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/</link>
		<comments>http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 20:59:00 +0000</pubDate>
		<dc:creator>M4d3L</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[controller plugin]]></category>
		<category><![CDATA[default controller]]></category>
		<category><![CDATA[default language]]></category>
		<category><![CDATA[default route]]></category>
		<category><![CDATA[demonstration]]></category>
		<category><![CDATA[dispatcher]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[language resources]]></category>
		<category><![CDATA[module resource]]></category>
		<category><![CDATA[module structure]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[reference guide]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[translate]]></category>
		<category><![CDATA[translation]]></category>

		<guid isPermaLink="false">http://www.m4d3l-network.com/?p=189</guid>
		<description><![CDATA[Today it&#8217;s easy to make business with other country and to join the most poeple as possible, our website must be translated in many language. This tutorial will show you how to integrate multi-language into your URL with Zend Framework controller route component and be able to use it with the Url helper in your [...]
Related posts:<ol>
<li><a href='http://www.m4d3l-network.com/developpement/php/autocomplete-field-with-zend-framework-and-dojo/' rel='bookmark' title='Autocomplete field with Zend Framework and Dojo'>Autocomplete field with Zend Framework and Dojo</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/how-to-use-flickr-with-zend-framework/' rel='bookmark' title='How to use Flickr with Zend Framework?'>How to use Flickr with Zend Framework?</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/phenix_wiki/' rel='bookmark' title='Phenix_Wiki, a parser componant for Zend Framework'>Phenix_Wiki, a parser componant for Zend Framework</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/' layout='default' show_faces='true' width='400' action='recommend' colorscheme='light' send='false' /></div><p>Today it&#8217;s easy to make business with other country and to join the most poeple as possible, our website must be translated in many language. This tutorial will show you how to integrate multi-language into your URL with Zend Framework controller route component and be able to use it with the Url helper in your layout.</p>
<p>This tutorial requiere Zend Framework 1.8.2 version or higher.<br />
<span id="more-189"></span><br />
Summary : </p>
<ul>
<li><a href="#Route">Route configuration</a></li>
<li><a href="#Plugin">Multilanguage Plugin</a></li>
<li><a href="#url_helper">Url Helper howto</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ul>
<h4 id="Route">Route configuration</h4>
<p>This tutorial will show you how to setup your route in .ini config. But you can do it with any other configuration type.  The demonstration will also use the Module structure of Zend Framework.</p>
<p>Here we setup the default module route without any parameters because it will automatically use the Dispatcher to setup it. Note the abstract param. This part is not actually documented in the ZF Programmer&#8217;s reference guide but Abstract parameter make the route to be unusable outside a chain route.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ini" style="font-family:monospace;">resources.router.routes.defaultmodule.type <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Zend_Controller_Router_Route_Module</span>
resources.router.routes.defaultmodule.abstract <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> On    </span>
resources.router.routes.defaultmodule.defaults.module <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;system&quot;</span></pre></td></tr></table></div>

<p></code></p>
<p>After we setup the language route with default language if you need it and the regex of what is expected for the language value.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
</pre></td><td class="code"><pre class="ini" style="font-family:monospace;">resources.router.routes.language.type <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Zend_Controller_Router_Route</span>
resources.router.routes.language.route <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;:language&quot;</span>
resources.router.routes.language.reqs.language <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;^(fr|en)$&quot;</span>
resources.router.routes.language.defaults.language <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;en&quot;</span></pre></td></tr></table></div>

<p></code></p>
<p>Than we setup the default route to overwrite the one create by default by the Front Controller.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>8
9
</pre></td><td class="code"><pre class="ini" style="font-family:monospace;">resources.router.routes.default.type <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Zend_Controller_Router_Route_Chain</span>
resources.router.routes.default.chain <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;language, defaultmodule&quot;</span></pre></td></tr></table></div>

<p></code></p>
<p>This configuration mean that if you call url like http://www.exemple.com/, language will be English, module will be default, controller will be index and action will be index.<br />
If you call http://www.exemple.com/fr/articles , language will be french, and module will be articles.</p>
<blockquote><p>Dont forgot to setup your locale and translate resources!</p></blockquote>
<h4 id="Plugin">Multilanguage Plugin</h4>
<p>In this part I will explain how to create your multilanguage plugin for controller. I assume here you know how to setup your Controller plugin into your Front Controller and pass directly to the Plugin class. It&#8217;s very basic and you will have to adapte to your own use.</p>
<p>Create a new class in your own library componant with routeShutdown method.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Controller_Plugin_Multilanguage <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> routeStartup <span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> routeShutdown <span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<p>In the routeStartup method, we add a quick fix to be sure the language is alway in each RequestUri. If the language parameter is not there, we force it.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestUri</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBaseUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequestUri</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestUri</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;language&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<p>In the routeShutdown method you will catch the language and setup the local and translator object already created by the resource. After you setup the translator to the Zend_Controller_Router_Route. This will be something like this :<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$language</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;language&quot;</span><span style="color: #339933;">,</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Zend_Locale</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLanguage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$locale</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Locale<span style="color: #009900;">&#40;</span><span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Zend_Locale</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLocale</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$locale</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Zend_Translate</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$translate</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLocale</span><span style="color: #009900;">&#40;</span>Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Zend_Locale</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Controller_Router_Route<span style="color: #339933;">::</span><span style="color: #004000;">setDefaultTranslator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$translate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<p>Once it done. Url helper will be very easy to use.</p>
<h4 id="url_helper">Url Helper howto</h4>
<p>Ok, here if you have lot of route setup, you will need the $currentRoute parameter in your url helper. If you play only with the default route, you don&#8217;t need the following part. Jump to the next step.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$front</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$router</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRouter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$currentRoute</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrentRouteName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$locale</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Zend_Locale</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$currentRoute</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$currentRoute</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;language&quot;</span><span style="color: #009900;">&#41;</span>? <span style="color: #0000ff;">&quot;default&quot;</span><span style="color: #339933;">:</span> <span style="color: #000088;">$currentRoute</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<p>In the last part, I show you a basic Language Switch I did for my project. It check the current language and dont show the link of the current language.<br />
See the Zend_View_Helper_Url parameter. If I dont put the language param in the array, it will show the current one. This link will switch between the same page but in different language.<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;langswitch&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$locale</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLanguage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;fr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;language&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;fr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currentRoute</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;Fran­­cais&lt;/a&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$locale</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLanguage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;language&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currentRoute</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;English&lt;/a&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></td></tr></table></div>

<p></code></p>
<p>An other example that will output http://www.exemple.com/fr/articles/<br />
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;language&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;fr&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;module&quot;</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;articles&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;action&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;list&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;default&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;Module Articles&lt;/a&gt;</pre></td></tr></table></div>

<p></code></p>
<h4 id="Conclusion">Conclusion</h4>
<p>This example is take from my current <a href="http://www.phenixapp-project.net">PhenixApp project</a>. All my source can be found in the repository. The code on my repository is not the same because I had more option like I take my default language from the database and I have some other route that I have to work with.</p>
<p>With the code in this tutorial, you get a working and easy multilanguage URL. You can integrate it very fast to your existing project because it use chain routing and dont broke any other route already exist.</p>
<p>If you like this tutorial or want to improve it, you can add comment.</p>
<img src="http://www.m4d3l-network.com/?ak_action=api_record_view&id=189&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://www.m4d3l-network.com/developpement/php/autocomplete-field-with-zend-framework-and-dojo/' rel='bookmark' title='Autocomplete field with Zend Framework and Dojo'>Autocomplete field with Zend Framework and Dojo</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/how-to-use-flickr-with-zend-framework/' rel='bookmark' title='How to use Flickr with Zend Framework?'>How to use Flickr with Zend Framework?</a></li>
<li><a href='http://www.m4d3l-network.com/developpement/php/zend-framework/phenix_wiki/' rel='bookmark' title='Phenix_Wiki, a parser componant for Zend Framework'>Phenix_Wiki, a parser componant for Zend Framework</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.m4d3l-network.com/developpement/php/zend-framework/add-language-route-to-your-zend-framework-project/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

