Sunday
17
January
2010

Translating validation messages

Hy interested ones,

many of you are using validators within Zend Framework.

As you know the returned error messages can be changed. This is very important for sites which are not delivered in english.
But they must translate all messages into their own language. This is a very tendious task.

Therefor I added today a new feature to Zend Framework. In fact it’s not really a new issue but more a long outstanding work :-)

Zend Framework comes now with translation resources.
These are pre-translated validation messages which are shipped as with release 1.10.

To use them you can simply do the following in your bootstrap:

$translator = new Zend_Translate(
    'array',
    '/resources/languages',
    $language,
    array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);
Zend_Validate_Abstract::setDefaultTranslator($translator);

When $language would be “de” all validation message are returned in german.
This should save you a lot of work for your project.

Note that the resources are outside the ‘/library’ path.
And you can also change the existing resources to do your own translations.
This is much simpler and faster than creating a new translation file manually.

For more informations take a look into Zend Frameworks manual.

I hope you find this feature useful and
have fun with Zend Framework,

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
  1. Diego Henrique Oliveira

    Sunday, January 17, 2010 - 23:09:59

    Hi Thomas,

    Me and some colleagues translated the messages from Zend_Validade for brazilian portuguese and made available initially on my blog: http://www.diegoholiveira.com/blog/zend-validate-messages-pt-br/

    It is a small contribution from me and my friends to the community. :)

    hugs

  2. Frieder

    Saturday, February 6, 2010 - 17:44:50

    Hi, I can not find the pretranslated ressources in the official release! The Folder “resoruces” is just empty in the ZendFramework-1.10.0.tar.gz file.

    And your Code gives me:
    Fatal error: Undefined class constant ‘LOCALE_DIRECTORY’ in /var/www/intern/application/Bootstrap.php on line 62

    I can not find a constant named ‘LOCALE_DIRECTORY’ in library/Zend/Locale.php

    Who is wrong?

  3. thomasw

    Saturday, February 6, 2010 - 18:13:25

    It is Zend_Translate::LOCALE_DIRECTORY and not Zend_Locale… false class ;-)

    And the resources have missed the official release for about 2 days. They are in 1.10.1 which comes within the next few days.

  4. casebone

    Thursday, February 11, 2010 - 18:01:46

    Hi Thomas,

    The translation resources are really great! Saved me a lot of time, thanks!

    I have a question regarding adapters and I can’t seem to find the answer. Maybe you have a tip.

    Is there a way to mix different zend_translate_adapters in a single zend_translate? I’ve got my regular translations in an ini file, but the validation translations are in array form. I’ve tried to convert the array to ini, but I get errors on puntuations in keys. The other way around (from ini to array) would be a solution but I want to keep the translation files ’simple’ for the people who have to translate them.

    So I’m looking for something like this:
    $translator->addAdaptor(Zend_Translate_Adapter $adapter)

    Do you know of a way to accomplish this?

    Thanks in advance,
    Kees Boon

  5. thomasw

    Thursday, February 11, 2010 - 19:32:09

    Actually there is no way to have multiple adapters set. The problem would be that Zend_Translate has no way to know which adapter it should use for reading translations.

    But you could simply load files with the first adapter and then set the second adapter and add files for it. When you use a cache (which should be default) then the content from the first adapter should persist (as long as the cache persists). As the cache itself is static this should work without problems.

  6. casebone

    Thursday, February 11, 2010 - 23:45:11

    Thanks for the tip. I’m still learning Zend and haven’t come to caching yet. Maybe I should check it out.

    My solution is to use a csv adapter for both. Really easy since everybody has excel :)

    The more I see of Zend, the more I’m impressed. It’s a bit though getting into but worth the trouble!

  7. Alexander Mahomet

    Friday, February 12, 2010 - 17:00:10

    Thomas, wrote you a message via your contact form, about russian translation of Zend_Validate messages

  8. Paul

    Wednesday, March 3, 2010 - 08:43:41

    Hi,

    Does this work for Zend_Filter_Input too?

    We have implemented the above code for the default Zend_Validate(_Abstract) translator (call this “translatorA”).

    We have another instance of Zend_Translate set as the registry ‘Zend_Translate’ instance that translates all our static page text (call this “translatorB”).

    All our input data is validated using instances of Zend_Filter_Input.

    Can you please let me know which version of the translator is used in Zend_Filter_Input to translate notEmpty and missing messages … translatorA or translatorB? To me it looks like the “B” instance (line 606 of Zend_Filter_Input.php), where we would need the “A” instance.

    Any help would be greatly appreciated.

    Cheers,
    Paul

  9. thomasw

    Wednesday, March 3, 2010 - 23:00:00

    In my understanding Zend_Filter is not the same component as Zend_Validate.

    The resource files are only available for Validators, because validators return an error (and true or false) but Filters throw normally an exception.

    Exceptions themself are actually not translated. The related proposal is for now not accepted and will be rejected as I understood for now.

    I don’t see how the resource files of Zend_Validate are related to your problem with Zend_Filter. I would request that you ask within the mailing list as my private blog is for informational purposes and not for bug fixing/tracking. Thnx…

    PS: Why don’t you simply try.. writing your entry needs much more time than writing those 3 lines of code to test the behaviour yourself.

  10. Bingo auf deutsch

    Monday, July 19, 2010 - 11:58:50

    The files with the first adapter and then set the second adapter and add files for it. When you use a cache (which should be default) then the content from the first adapter should persist as long as the cache persists.

  11. thomasw

    Monday, July 19, 2010 - 18:25:26

    This works only with 1.10.6 and did not work in March.
    See the newer entry within my blog which is related to Zend_Translate.

  12. Kal

    Wednesday, July 28, 2010 - 14:05:41

    Hi,

    here is the Zend_Application configuration I use :

    ; locale to be used for the whole application
    resources.locale.default = “fr_FR”
    resources.locale.force = true

    ; add the validate translations
    resources.translate.adapter = array
    resources.translate.options.scan = “directory”
    resources.translate.data = APPLICATION_PATH “/../resources/languages”

    I wrote a specific translate resource for Zend_Application to add multiple translate sources and use cache.

    Thanks

  13. Benny Neugebauer

    Friday, August 20, 2010 - 18:10:03

    Hi, I’m using “ZendFramework 1.10.2” and with your code I get the same error as Diego Henrique Oliveira:
    Fatal error: Undefined class constant ‘LOCALE_DIRECTORY’ in C:xampphtdocszendapplicationBootstrap.php :(

  14. max4ever

    Friday, January 28, 2011 - 18:30:35

    hello, i’ve been trying to do the same thing, but it won’t work for me, if you have time, could you give me a hand
    i’ve put the question here http://stackoverflow.com/questions/4829873/zend-translate-join-multiple-file-languages

    also made some publicity for your blog :D

  15. saudia arabia Markets of the city

    Thursday, September 20, 2012 - 05:18:31

    Howdy! I realize this is somewhat off-topic however I had to ask.
    Does managing a well-established blog such as yours require a massive amount work?
    I am brand new to writing a blog however I do write in my diary every day.

    I’d like to start a blog so I can easily share my own experience and thoughts online. Please let me know if you have any kind of ideas or tips for brand new aspiring bloggers. Appreciate it!