Zend Translate and directory scanning
- Posted by thomasw at 19:57:05 // //
- Filter
Hy framework users…
Most of you are already using Zend Translate. Until now the option for scanning a directory for translation files was only available when creating the instance.
I added this feature now also for the addTranslation() method, which means that you are now able to add new directories afterwards. This feature is handy when you are working with a modul based implementation and have also the translations seperated per module.
For those who did not know this feature for now is here a short description:
Instead of adding several translations manually it’s much more convinient to have them added automatically by only one method call.
Depreciated way:
$translate = new Zend_Translate('gettext', '/my/proj/lang/en/proj.mo', 'en');
$translate->addTranslation('/my/proj/lang/de/proj.mo', 'de');
$translate->addTranslation('/my/proj/lang/fr/proj.mo', 'fr');
As you see we have only added three languages… if you are working with seperated translation you will probably have not only one file per language but more.
So it would be better to have this done by Zend_Translate itself… see the following code:
$translate = new Zend_Translate('gettext', '/my/proj/lang/', 'en',
array('scan' => Zend_Translate::LOCALE_DIRECTORY));
So what is now hapening in the background:
Zend_Translate recognises a directory instead of a file.
With the ’scan’ option we said that the locale/language is written in the directory name.
Zend_Translate will now search all sub-directories within the lang directory and add all found translations.
It uses the directory name as locale if it fits…
So a file ‘/my/proj/lang/de/proj.mo’ would be added within the locale ‘de’ because this directory is recognised as german locale and so on… it doesn’t matter how deep the directory structure is, all subdirectories will be searched.
This works not only with gettext but also with any other adapter like TMX, Array, Csv and so on… the only thing which has to fit is the name of the directory.
But there is also another way to get the name of the locale. It can be written within the filename.
'/my/proj/lang/myfile_de.mo' '/my/file/lang/myfile-de.mo' '/my/file/lang/myfile.de.mo'
All of this three ways of naming files will be recognised translation for the locale ‘de’.
To use this way of naming you will only have to use the ’scan’ option with the Zend_Locale::LOCALE_FILENAME constant.
Have fun with the framework.
Greetings
Thomas
I18N Team Leader of the Zend Framework
Add comment
Fill out the form below to add your own comments

Sunday, March 9, 2008 - 20:34:12
Thanks for this addition :)
Friday, June 19, 2009 - 18:26:48
Hi Thomas,
I’m having problems when I add folders specifying the country like es,es_MX,es_AR. The problem is Zend does not find the es directory just the other 2.
Could you explain a little bit about this?
Thanks
Thursday, June 25, 2009 - 10:54:12
The directory scan function does not work when using locale specific translations like en_GB that fall before regular language folder names.
eg.
en/
en_GB/
ja/
All the ja translations will be added to the en_GB translation object because of line 159 in Zend_Translate_Adapter:
if (strlen($prev) <= strlen($file))
This is because it scans though the directory in an alphabetical order and for some reason compares the length of the previous valid locale name to the new locale.
Thanks
Thursday, June 25, 2009 - 12:46:56
Please use the issue tracker from ZF.
This is my private blog and it’s not the proper media to do bug fixing.