how we could create translate validate error messa

2020-07-24 05:34发布

问题:

how we could create translate validate error messages on zend framework?

someone could give a example ?

thanks

回答1:

From the ZF Manual on Zend_Validate Validation Messages

  $validator = new Zend_Validate_GreaterThan();
  $validator->setMessage('Please enter a lower value', 
                         Zend_Validate_GreaterThan::NOT_GREATER);

And also:

Zend Framework is shipped with more than 45 different validators with more than 200 failure messages. It can be a tendious task to translate all of these messages. But for your convinience Zend Framework comes with already pre-translated validation messages. You can find them within the path /resources/languages in your Zend Framework installation. [...]
So to translate all validation messages to german for example, all you have to do is to attach a translator to Zend_Validate using these resource files.

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

Of course, you can also provide your own translations. All you have to do is load make them available to the translation adapter. Basically you just swap out the part shown above to your custom path.



回答2:

I just want to improve a little bit the answer from Gordon: a working example is

$translator = new Zend_Translate(
    'array',
    'resources/languages',  // you need to copy the resources folder
                            // (from your Zend Framework installation)
                            // in the application folder 

    'it', // 'it' for italian, 'fr' for french, etc. 
          // Just look at the directories

    // Zend_Translate, NOT Zend_Locale
    array(
        'scan' => Zend_Translate::LOCALE_DIRECTORY
    )
);

Zend_Validate_Abstract::setDefaultTranslator($translator);

Cheers! Bruno