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

南笙酒味 提交于 2020-01-13 06:07:49

问题


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



来源:https://stackoverflow.com/questions/2626972/how-we-could-create-translate-validate-error-messages-on-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!