Featured Post

A review of the book: Zend Framework 2 Cookbook from Packt Publishing

Review of the book: Zend Framework 2 Cookbook written by Josephus Callaars, issued by Packt Publishing. There is many cookery books although I haven’t seen yet any about Zend Framework 2. No matter how you name the book, title is just a title and every book have to stand out of the competitions....

Read More

Localizing application in Zend Framework 2

Posted by Tomasz Kuter | Posted in Programming, Web development | Posted on 29-03-2013

Tags: , , , , , , , , , , ,


Today I would like to focus on translating application in Zend Framework 2.


By default, tutorial application has included Zend\I18n\Translator\Translator configuration in /module/Application/config/module.config.php as follow:

It has also attached translation files in gettext format under the /module/Application/language directory.

There is over a dozen files like en_US.mo and en_US.po. You can edit .PO files (gettext source) with Poedit – a free, open source and cross-platform gettext catalog editor.

.MO file is automatically created (compiled), when you save the .PO file.

Program has very intuitive interface and looks like that:

Poedit - main window

Poedit – main window

There was missing Polish translation, so I created it on my own.

Poedit - Sources paths

Poedit – Sources paths

To be always up to date with the translations we need to add paths to our modules in the Poedit settings: Catalogue » Properties, Source paths tab, and also add keywords on the third tab: Source keywords: translate.

Poedit - Sources keywords

Poedit – Sources keywords

Source keyword: translate is taken from view helper used inside templates:

or from controllers:

With that settings we just need to choose the position Update from sources from Catalogue menu, and check newly added strings in our application source code.

Plural forms in English and Polish

First of all I renamed all translations files from naming format en_US to en-US (I replaced “_” with “-“). I made it to be consistent in whole application and use only that format.

I updated Application module config file (/module/Application/config/module.config.php) with:

As you can see locale has changed from en_US to en-US and also I added another localization resource as phparray for Album module. New files are stored under /module/Album/language, e.g. in file en-US.php – it is PHP file with an array of the localizations strings. I use them only for translating plural forms.

English plural form is quite simple, and the same as Deutsch and Spanish, it can be described as: nplurals=2; plural=(n != 1);.
You need to add that rule into the language file (/module/Album/language/en-US.php):

Polish language is a bit more complicated and has following pattern for plural form:
nplurals=3; plural=n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;.
I included it into Polish language file (/module/Album/language/pl-PL.php):

Customizing translation support in the application

In global.php I added list of supported languages, specifying also which locale is default:

I updated Application Bootstrap script (/module/Application/Module.php) to maintain locales:

Now our tutorial application will be working as follow:

  • / – there will be possibility to choose localization,
  • /en-US – homepage in English,
  • /pl-PL – homepage in Polish,
  • /en-US/album – the same as /en-US,
  • /en-US/album/edit/2 – edit album with id=2.

We need to modify routes configuration of our application to be able to work as it was mentioned above.

Update the Application config at first (/module/Application/config/module.config.php):

And configuration file in Album module (/module/Album/config/module.config.php):

There are two new actions in Application module, so we need to update the Index controller body (/module/Application/src/Application/Controller/IndexController.php):

And create .phtml template files.

In script /module/Application/view/application/index/index.phtml we define the page, where we can choose our localization:

Old index.phtml content has been moved to zf2.phtml

I also made some modifications to layout.phtml, which is default layout template in our application. Now it looks like follow:

There was small modification in style.css. I added new styles:

At the end we have to add locale to all urls in Album controllers. I show only an example action, how it looks now:

You also need to remember to update links in .phtml templates, for example in add.phtml:

Complete code is available at GitHub:


Above code depends on Loculus library in version 0.2.x,
which will be updated automatically with the Composer. All should work with version of that library.

You can check its code at:

Comments (1)

Great tutorial, thanks

Write a comment