Searching for translations
- Posted by thomasw at 00:24:59 // //
- Zend Framework, I18N, Translate
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
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Working with multiple translation sources
- Posted by thomasw at 00:46:24 // //
- Zend Framework, I18N, Translate
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
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Translation resources
Hy fellows,
even if it’s not official for now I want to point you to a new feature which will be available within the next few weeks.
Zend_Validate offers more than 100 different error messages which can easily be translated. The problem with “easily” is that you need to create the translation source eighter by parsing ZF’s code or by copying them from the documentation.
This is not a real fast way and is not really good for usability.
So I created translation resources for Zend_Validate’s error messages.
For now you can find them within the Incubator. Look into /incubator/resources/translate/en/Zend_Validate.php.
Use the Array Adapter to load them.
You can already find pre-translated messages for english (of course) and german.
As this feature is for now not officially accepted I would ask to wait before adding new translations until it’s available within core. When it’s time I will add you some example code for those not familiar with Zend_Translate.
Have fun with this new feature when you can’t wait :-)
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
