With Zend Framework it’s not possible to define multiple translate sources for one Zend_Translate instance that will be stored in the Zend_Registry. Also if you want to add a logger to Zend_Translate, it’s not possible to do this only in your application.ini. The closest you get is to define all translate settings, than create a bootstrap method and “extend” the settings and add a instance of Zend_Log with a writer there.
But we made it possible at the PiKe project to define all this purely in your application.ini.
You only need the file following file, or just add the PiKe library to “library/Pike” in your Zend Framework application.
Add the PiKe namespace and application resource path to your application.ini:
It’s always good to define a default (fallback) locale. You can do that in your application.ini like this:
If you want to add a logger for Zend_Translate, define a log resource as below with for example the name “translate”.
To add the translate log resource to Zend_Translate add the following to your application.ini:
Now we are ready to define the default Zend_Translate instance. In this example we use the gettext translate source, but you are free to pick anything else. The locale “auto” setting, means that the user locale will be detected by the browser language preferences of the user. If no translation source is found for the detected locale Zend Framework will fall back to de setting defined at resources.locale.default as described above.
This directory structure I used for this configuration is:
Make sure you have Zend Framework version 1.11.10 or later! In several versions earlier exists a bug that causes the last locale always to be selected. So in our example the chosen translation would always be “de”, even though the current locale would be “nl”.
If you like to add a translation source that is not only used for the current application, but for example company wide, we can add another translation source. The token %locale% will be replaced with the current locale.
But you can also mix all the available translation source types that Zend_Translate offers! If you download the Zend Framework full version (not the minimal), there is also a folder “resources/languages”.
If the locale you want is available there all Zend_Validate messages that are used in Zend_Form are translated for you. You can copy this file and put it for example in your own languages directory. Because the translations in this file are defined as an array we must use the “array” translation source adapter.
All the translation source are configured now! In your views you can do:
But how can we actually create translations. For a gettext translation source we can do this easily with the free cross platform tool POEdit. Read more about how to setup the “.po” catalog file so that it scans your Zend Framework application correctly at the article “Configure POEdit for Zend Framework“.