PiKe multi translate with Zend_Translate and application.ini

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:

pluginPaths.Pike_Application_Resource = "Pike/Application/Resource"
autoloaderNamespaces[] = "Pike"

It’s always good to define a default (fallback) locale. You can do that in your application.ini like this:

resources.locale.default = "en"

If you want to add a logger for Zend_Translate, define a log resource as below with for example the name “translate”.

resources.log.translate.writerName = "Stream"
resources.log.translate.writerParams.stream = APPLICATION_PATH "/../data/logs/translate.log"
resources.log.translate.writerParams.permission = 660
resources.log.translate.writerParams.mode = "a"
resources.log.translate.filterName = "Priority"
resources.log.translate.filterParams.priority = 7

To add the translate log resource to Zend_Translate add the following to your application.ini:

resources.multiTranslate.logger = "translate"

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.

resources.multiTranslate.default.adapter = "gettext"
resources.multiTranslate.default.content = APPLICATION_PATH "/../languages"
resources.multiTranslate.default.locale = "auto"
resources.multiTranslate.default.scan = "directory"
resources.multiTranslate.default.logUntranslated = 1
resources.multiTranslate.default.disableNotices = 1

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.

resources.multiTranslate.company.adapter = "gettext"
resources.multiTranslate.company.content = APPLICATION_PATH "/../library/Company/Locale/Data/%locale%/messages.mo"

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.

resources.multiTranslate.zendValidate.adapter = "array"
resources.multiTranslate.zendValidate.content = APPLICATION_PATH "/../languages/%locale%/Zend_Validate.php"

All the translation source are configured now! In your views you can do:

$this->translate('Some string to translate');

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“.

Tags: ,,,,,