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:
<?php $t = new Zend_Translate( 'gettext', //the adapter APPLICATION_PATH.'/translation', //where the lang files will be stored 'auto', //set to auto include .mo files array('scan' => Zend_Translate::LOCALE_FILENAME) //set to scan lang files ); echo $t->_("This is my first translation")."<br/>"; ?>
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:
Go to File->Catalog Manager
Click on the “Create new translations project” icon
Give your project a name
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
Go to File->New Catalog
Give it a project Name and/or version and be sure to feel in the appropriate information. Be sure to select Charset ”utf-8″
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)
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”);
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:
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”
<?php $t = new Zend_Translate('gettext', APPLICATION_PATH.'/translation', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); echo $t->_("This is my first translation")."<br/>"; //change locale to spanish $t->setLocale('es'); echo $t->_("This is my Second translation")."<br/>"; ?>