Zend Framework: Internalization and Translation

This week I set myself to lay the ground work of translation on the application we are doing at work. In our application we are using Zend Framework, so I decided to take a look into the Zend_Translate component to see if it was easy and something that could work in our current environment.

After reading the reference guide on Zend_Translate I decided that we were going to use “gettext” as our translation adapter. PHP has support for gettext right our of the box, and with the Zend_Translate it’s easy to change adapters, if you decide that you want to use a different adapter.

Assuming that you already have the latest version of Zend Framework (1.10.*) installed in your server, I’m going to explain and guide you guys on how I got it all working.

First you need to get a tool that we will use to parse your project files to create the translation dictionaries. I’m using PoEdit which is free and supports multiple platforms.

Now let’s put some code down in your index.phtml file of you application. This one should be located in /yourapp/application/views/scripts/index/
If it’s not there, then just create one, but make sure you have to corresponding controller and action.
Inside of this file we are going to put the following:

 Zend_Translate::LOCALE_FILENAME) //set to scan lang files

echo $t->_("This is my first translation")."
"; ?>

Before this piece of code works, we need to generate our translation files, else it will throw an error stating that the application can’t find the corresponding language files.

We need to create a new folder in your application folder. I’m calling it lang, but you can call it anything you want. This folder will be used to store your language files. So you should have something like “/yourapp/application/lang”.

After this, you need to open up PoEdit:

  1. Go to File->Catalog Manager

  2. Click on the “Create new translations project” icon

  3. Give your project a name

  4. Select the directory to store your translation files (“/yourapp/application/lang”) and hit OK

With a newly crated project, we can now generate our language file.
To do this,  close the Catalog Manager window

  1. Go to File->New Catalog

  2. Give it a project Name and/or version and be sure to feel in the appropriate information. Be sure to select Charset  “utf-8”

  3. Now, click on the paths tab. Here you will have to add all the dir paths that point to the files that you want to parse for translation. (/yourapp/application/views/scripts/index/index.phtml)

  4. Finally, click on the Keywords tab, and make sure you include the keyword the parser will use to identify the text that needs translation. In our example we will put an underscore “_”  since in our code we have echo $t->_(“This is my first translation”);

  5. Hit OK and make sure that you save the file with a name such as mylang_en.po. The _en is crucial, since Zend_Translate will scan the appropriate language file and use this naming convention to load the appropriate file

Since by default the PoEdit PHP parser handles files with .php extension. We need to modify the preferences so that it allows other types, such as .phtml files.
To do this, go to Edit->Preferences->Parsers  When you get to parsers select PHP and then click on the EDIT button. In the parser setup window we need to add *.phtml extension to the “List of extensions” field in the Language section, separated by semicolons. The field should be something like this: *.php;*.phtml
Then, in the Invocation section, you need to add  -L php to the parser command. This should look like this  xgettext --force-po -o %o %C %K %F -L php

Once you have completed this, a pop up screen will show the progress of the catalog update and then list you all the parsed strings. Hit OK and then you will be in the translation view. Just select the string you would like to translate save and you are good to go.

Once you’ve completed the following steps just copy the .po file and create additional one’s for the languages you want to do translation. Remember the naming convention discussed on the steps above.

In your code to switch languages, you need to put the following line $t->setLocale('es'); This for example will switch the locale to Spanish and will try to load a file with “_es”


echo $t->_("This is my first translation")."
"; //change locale to spanish $t->setLocale('es'); echo $t->_("This is my Second translation")."
"; ?>

9 Comments to “Zend Framework: Internalization and Translation”

  • Nice. It is actually the first time that I see the real way of doing that with the Zend Framework.

    Another way that you can do this is by using XML files with array of elements. Each XML would represent an controller / action set with entries of strings that would be replaced on the view once the locale is identified.

    You would need a view helper to actually replace the strings on the view for the translated ones.

    Example: On the xml

    variableString: home
    description: Text label for the home link
    en: Home
    pt: Inicio

    On the controller you would need to have a call to the helper where it would load all of the translation strings from that controller / action and pass it to a view variable. On the view you would need to echo the string:

    echo $this->translationStrings->home

    and make sure that the view helper would correctly replace the above string for the translated one based on the current locale.

    A quick note about Internationalization: There are 2 types of locales that you must be careful with – the ones with double-byte characters and the RTL ones. Double-byte chars are already painful in many ways and internationalizing the site for those languages is more than a simple matter of simply replacing strings. I have 2 example for those: China and Japan. China you should have versions for traditional Chinese and Hong Kong Chinese. Japan you need to consider the culture on your site. They don't accept any flaws what so ever.

    Internationalizing for RTL languages is more an exercise of CSS. Put your site in a front of a mirror and convert it for that way.

    Hope this helps and great article by the way.

    • Cris,
      Thanks for the additional tips and for extending on my article. I believe this is a good starting point for a beginner. There are a couple of other things that will have to be taken into consideration, such as caching and setting up the bootstrap to handle the local based on the browser or a user defined setting.

      Maybe I'll do a part 2 to this article.

  • […] by mcloide on August 7, 2010 I have just spent the last half-hour writing an comment on the Smooka blog (that is from a good friend of mine) and since the comment become a little bit more of a […]

  • Great web site you have got here.. It’s difficult to find excellent writing like yours nowadays. I seriously appreciate people like you! Take care!!

  • Thanks for sharing your thoughts on gettext. Regards

  • I don’t even know the way I ended up here, but I

    assumed this submit was once good. I don’t know who you’re however certainly you’re going to a famous blogger in case you are not already.


  • 神迹私服一条龙www.2pk1.com天龙私服一条龙开sf服务QQ1285574370

  • Very excellent post and very informative post thanks for sharing me

Leave a comment