Featured Post

SubForms example in Zend Framework 1.x

Today I would like to write something about SubForms available in Zend Framework 1.x. Currently we have Zend Framework 2 released, and there is some similar solution called Form Collections with completely new approach. I will take care that new feature from Zend Framework 2 in the close future, but...

Read More

Localizing application in Zend Framework 2

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

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

1

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

Overview

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:

https://github.com/evolic/zf2-tutorial/tree/v0.4.0.1

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

You can check its code at:
https://github.com/evolic/loculus/tree/v0.2.2.0

Comments (1)

Great tutorial, thanks

Write a comment