Add language route to your Zend Framework project.


Today it’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.

This tutorial requiere Zend Framework 1.8.2 version or higher.

Summary :

Route configuration

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.

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’s reference guide but Abstract parameter make the route to be unusable outside a chain route.

 

1
2
3
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On    
resources.router.routes.defaultmodule.defaults.module = "system"

After we setup the language route with default language if you need it and the regex of what is expected for the language value.

4
5
6
7
resources.router.routes.language.type = Zend_Controller_Router_Route
resources.router.routes.language.route = ":language"
resources.router.routes.language.reqs.language = "^(fr|en)$"
resources.router.routes.language.defaults.language = "en"

Than we setup the default route to overwrite the one create by default by the Front Controller.

8
9
resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "language, defaultmodule"

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.
If you call http://www.exemple.com/fr/articles , language will be french, and module will be articles.

Dont forgot to setup your locale and translate resources!

Multilanguage Plugin

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’s very basic and you will have to adapte to your own use.

Create a new class in your own library componant with routeShutdown method.

1
2
3
4
5
6
7
8
9
10
11
class My_Controller_Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract{
    public function routeStartup (Zend_Controller_Request_Abstract $request)
    {
 
    }
 
    public function routeShutdown (Zend_Controller_Request_Abstract $request)
    {
 
    }
}

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.

1
2
3
4
if (substr($request->getRequestUri(), 0, -1) == $request->getBaseUrl()){
    $request->setRequestUri($request->getRequestUri()."en"."/");
    $request->setParam("language", "en");
}

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 :

1
2
3
4
5
6
$language =  $request->getParam("language", Zend_Registry::getInstance()->Zend_Locale->getLanguage());
$locale = new Zend_Locale($language);
Zend_Registry::getInstance()->Zend_Locale->setLocale($locale);
$translate = Zend_Registry::getInstance()->Zend_Translate;
$translate->getAdapter()->setLocale(Zend_Registry::getInstance()->Zend_Locale);
Zend_Controller_Router_Route::setDefaultTranslator($translate);

Once it done. Url helper will be very easy to use.

Url Helper howto

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’t need the following part. Jump to the next step.

1
2
3
4
5
$front = Zend_Controller_Front::getInstance();
$router = Zend_Controller_Front::getInstance()->getRouter();
$currentRoute = $router->getCurrentRouteName();
$locale = $this->registry->Zend_Locale;
$currentRoute = ($currentRoute == "language")? "default": $currentRoute;

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.
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.

1
2
3
4
<div id="langswitch">
	<?php if ($locale->getLanguage() != "fr"):?><a href="<?=$this->url(array("language" => "fr"), $currentRoute)?>">Fran­­cais</a><?php endif?>
	<?php if ($locale->getLanguage() != "en"):?><a href="<?=$this->url(array("language" => "en"), $currentRoute)?>">English</a><?php endif?>
</div>

An other example that will output http://www.exemple.com/fr/articles/

1
<a href="<?=$this->url(array("language" => "fr", "module" =>"articles", "action"=>"list"), "default")?>">Module Articles</a>

Conclusion

This example is take from my current PhenixApp project. 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.

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.

If you like this tutorial or want to improve it, you can add comment.

Related posts:

  1. Autocomplete field with Zend Framework and Dojo
  2. How to use Flickr with Zend Framework?
  3. Phenix_Wiki, a parser componant for Zend Framework
  4. Joomla : How to show Menu to Guest Only

copy m4d3l-network.com

Advertisements

One comment on “Add language route to your Zend Framework project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s