Saturday
27
March
2010

Zend_Translate goes configuration

Hy interested ones,

This is my third entry for Zend_Translate this week. Maybe I should call it “Zend_Translate“-week. :-)

Ok… so what have I done this time.
I reworked Zend_Translate a little bit. Now it supports Zend_Config and additionally all options can be given as array.

The change seems small but makes coding a little bit easier and more comfortable.

new Zend_Translate('csv', '\path\to\myfile.csv', null, array('delimiter' => ':'));


Now you can do:

new Zend_Translate(
array(
'content' => '\path\to\myfile.csv',
'delimiter' => ':'
)
);


By using the array syntax the code is much more readable and seems more logical.
Of course you can also use a Zend_Config object instead of an array.

This syntax is also supported by using addTranslation(), but as you call addTranslation() on the adapter, you can’t change the adapter within this method so it will be ignored in that case.

$translate->addTranslation( array( 'content' => '\path\to\translations\', 'locale' => 'de', 'scan' => Zend_Translate::LOCALE_DIRECTORY ) );  Note that this new feature is available as with ZF 1.10.3. Of course the old syntax is still supported. So your old code works without that it has to be reworked (when you don’t like to do that). When you find this feature useful, feel free to use it yourself. :-) Greetings Thomas Weidner I18N Team Leader, Zend Framework Zend Framework Advisory Board Member Zend Certified Engineer for Zend Framework Back to top Saturday 27 March 2010 Searching for translations When you want to add translations then you have probably already used the scan option from Zend_Translate. It allows you to search complete directory structures for translations. The scan option has two ways how it can be used. LOCALE_FILENAME and LOCALE_DIRECTORY. They define how the language will be detected out of the directory and filename. LOCALE_FILENAME means that the locale/language this translation file will be added to, will be detected by looking within the filename. And LOCALE_DIRECTORY does the same for the directory where this file has been detected. Some example: \dir\en\translation.xxx <= locale as directory "en" \dir_en\translation.xxx <= locale as directory "en" \dir\en_translation.xxx <= locale as filename "en" \dir\translation.en.xxx <= locale as filename "en"  Simple as is and for most of you nothing new. But sometimes you may need to exclude some of the files and directories within the structure you are searching. Therefor you can use the ignore option. As with 1.10.3 this option accepts 3 syntax to be used. You can use a single string… per default it is set to “.” which means that all files and directories beginning with “.” are ignored. For example all SVN directories begin with “.” and are per default ignored. All files within this path will not be added. array('ignore' => '.'); <= default for ignoring all SVN directories array('ignore' => 'test'); <= ignores all directories and files beginning with 'test'  The second line would ignore “\dir\test\en.mo” or “\dir\testme.mo” but it would accept “\dir\mytest.mo” as the later doe s not begin with “test”. You may want to change this option. But be aware that, when you overwrite the “.”, all SVN directories will be searched. So don’t do this when you are working on or with SVN. As second syntax you can use an array. This allows you to search for several syntax. But also using an array means, like using strings, that the directory or file has to begin with this pattern. array('ignore' => array('.', 'test'); <= ignores SVN directories AND also those beginning with 'test'  Still there could be a problem… the directory or filename could have a syntax where it does not begin with a string. Here you have to use the third syntax. It allows you to use regular expressions to search for files and directories which have to be ignored. array('ignore' => array('regex' => '/test/u', 'regex_2' => '/delete$/u');


The above example would ignore all files and directories which contain test anywhere within their name, and also all files and directories which end with delete.

This should give you enough flexibility to add only those translations which you need and ignore all others.
I hope you find this feature useful. More to come soon…

Greetings
Thomas Weidner

Zend Certified Engineer for Zend Framework

Thursday
25
March
2010

Working with multiple translation sources

Sometimes, when working with translation, you can come into a situation where you need to use multiple adapters.
Especially when you want to use the new resources, which are pre-translated validation messages and are available since 1.10.

There is a simple way how this can be archived by joining two translations. How does this work?
First, you need to have two translation sources:

$translation = new Zend_Translate( 'gettext', '\path\to\translations\', 'en', array('scan' => Zend_Translate::LOCALE_DIRECTORY) );$translation_addme = new Zend_Translate(
'array',
'\resources\languages\',
'en',
array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);


In our example we used array resources and a gettext adapter.
Now we combine them to one translation, simply by:

$translation->addTranslation($translation_addme);


That’s it… now our first instance holds all translations from the second instance.
You can combine as much instances as you want.

The only limitation is, that you should not try to combine same keys by using different translations for these keys. This can (of course) lead to unexpected behaviour when you define two different translations for the same key.

To save resources you can also combine only one language from the second adapter.

$translation->addTranslation($translation_addme, 'en');


Note that this feature is available as with 1.10.3. There was no reason for me to wait for 2.0 for this feature. ;-)

I hope you find this feature useful for your daily work.
When not, you should probably take the I18n webinar about translation. ;-)
I will give a repetition in a few weeks.

Greetings
Thomas Weidner