Build Multi-lingual application with the Zend Framework

We can not imagine a modern web application without a multi language support. I will show you how easy it is to setup usage of multiple languages in Zend Framework and how to setup some basic language routes (handy for SEO stuff).

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

Why did I choose GetText ?

There are many reasons I choose GetText. First, there is a handy program for editing GetText files called PoEdit. So, when you have a handy program, it is easy to send it to translators, who do not have to mess with some arrays or XML files.

Second, Apache web server is caching the compiled .mo translation files, which is making them really fast.

Let’s start.

First we need an example application, it doesn’t need to be more complicated than a clean installation of ZF with an IndexController and a matching view. So for this article, I am going to use the Zend framework skeleton application.

Note : When you install the Zend framework skeleton application, make sure to install the i18n support (Would you like to install i18n support?, say Yes).

To translate the web pages, we need to use translate function (ZF helper function). But in the Zend framework skeleton application  already the view files are translated.

Example : Look at the module/Application/view/application/index/index.phtml, In line number 2 : $this->translate(‘Welcome to %sZend Framework 2%s’)

So we don’t need to modify the view files, we just need to set the locale to the zend translator. It can be done many different ways, but here I am going to show you only two methods.

  1. Url Structure gTLDs
  2. Url Structure ccTLDs (country-coded top-level domains)

Method 1 – Url Structure gTLDs (generic top-level domains with sub-directories)

In this method, we are going to pass the language code to our application via url.

Example :

  • Step 1 : Add Url Routing

First we need to modify the url routing and add language code to it. To do that, open the module config file Application/config/module.config.php and change the home page route as follows.

  • Step 2 : Set Locale in Module onBootstrap()

Now, we need to read the language code from url and set Locale to the zend translator, based on the language code. To do that, open the  module/Application/Module.php file and add the following code to the begging of the onBootstrap function.   

Here is the complete source code of the onBootstrap() function.

Thats it!!! – Here the pages will be translated in French. – Here the pages will be translated in English.

Method 2 – Url Structure ccTLDs (country-coded top-level domains)

  • Step 1 : Add domains to your virtual host

  • Step 2 : Modify your virtual host configuration and add domain alias

  • Step 3 : Add server environment variable in the virtual host file to set language for each domains

Here is the complete configuration of the virtual host.

  • Step 4 : Modify your application module bootstrap function as follows to read the language variable and set the locale for your application.

Here is the complete source code of the onBootstrap() function.

Thats it!!! – Here the pages will be translated in French. – Here the pages will be translated in Dutch. – Here the pages will be translated in English.

Now, let’s go in our view and add some translations. Open up your index view and translate some strings that you find there:

Simply by using translate method in our view, all the GetText magic is going to happen later.

Now, go to PoEdit website and download PoEdit program and install it. Launch PoEdit and add translation for these texts in module/Application/language/fr_FR.po and module/Application/language/de_DE.po and refresh the page. You should be able to see the changes on your page.

I hope you find this useful! 😀