简体   繁体   English

如何在Zend Framework中翻译自定义验证器的消息?

[英]How to translate messages of a custom validator in Zend Framework?

I created a custom validator by extending Zend_Validate_Abstract to validate a CAPTCHA input regarding Zend_Captcha : 我通过扩展Zend_Validate_Abstract来验证关于Zend_Captcha的CAPTCHA输入, Zend_Captcha创建了一个自定义验证器:

class My_Validate_Captcha extends Zend_Validate_Abstract {
  const CAPTCHA = 'captcha';

  protected $_messageTemplates = array(
    self::CAPTCHA => "'%value%' isn't the right solution"
  );

  protected $_id;

  public function __construct($captchaId) {
    $this->setId($captchaId);
  }

  public function setId($id) {
    $this->_id = $id;
    return $this;
  }

  public function getId() {
    return $this->_id;
  }

  public function isValid($value) {
    $this->_setValue($value);

    $captcha = new Zend_Captcha_Image();
    if(!$captcha->isValid(array('input' => $value, 'id' => $this->getId()))) {
      $this->_error(self::CAPTCHA);
      return false;
    }

    return true;
  }
}

It works fine with Zend_Filter_Input . 它适用于Zend_Filter_Input As you can see, I defined an error message for the case the input value isn't valid. 如您所见,我为输入值无效的情况定义了一条错误消息。

Now I tried to translate this message into German in the same way I translated the other messages coming from Zend_Validate_* classes. 现在我尝试将此消息翻译成德语,就像翻译来自Zend_Validate_*类的其他消息一样。 I did this with Zend_Translate providing an array adapter. 我使用Zend_Translate提供了一个数组适配器。

return array(
  // Zend_Validate_Alnum
  'notAlnum'     => "'%value%' darf nur Buchstaben und Zahlen enthalten",
  'stringEmpty'  => "'%value%' Dieser Wert darf nicht leer sein",
  // ...
  // My_Validate_Captcha
  'captcha'      => "'%value%' ist nicht die richtige Lösung"
)

My problem is that the messages from Zend_Validate_* are translated as defined here, but the message from My_Validate_Captcha isn't translated. 我的问题是来自Zend_Validate_*的消息按照此处的定义进行翻译,但是来自My_Validate_Captcha的消息未被翻译。 I get an empty message if 'captcha' is present within the translation array. 如果翻译数组中存在'captcha'我会收到一条空信息。 If it isn't present, I get the english message defined in the validator class. 如果它不存在,我会在验证器类中定义英语消息。

How can I achieve that the message from the custom validator is also translated using the same mechanism? 如何实现自定义验证器的消息也使用相同的机制进行转换?

Adding this because of Google search, but I was using a different translation adapter (Poedit). 由于谷歌搜索添加了这个,但我使用了不同的翻译适配器(Poedit)。 Another way of handling the translation of the custom validator is by setting the response messages in the constructor. 处理自定义验证器转换的另一种方法是在构造函数中设置响应消息。 This way the translate function of Zend_Translate can be called and catched by Poedit. 这样, Zend_Translate的translate函数可以被Poedit调用和捕获。

class Form_Validator_Promocode extends Zend_Validate_Db_Abstract
{
    const ERROR_CODE_EXPIRED = 'codeExpired';
    const ERROR_CODE_INVALID = 'codeInvalid';

    protected $_messageTemplates = array(
        self::ERROR_CODE_EXPIRED => "",
        self::ERROR_CODE_INVALID => "",
    );

    public function __construct($options)
    {
        parent::__construct($options);

        $tr = Zend_Registry::get('Zend_Translate');

        $this->setMessage(
                $tr->translate("This code has expired"),
                Form_Validator_Promocode::ERROR_CODE_EXPIRED
        );
        $this->setMessage(
                $tr->translate("No code matching '%value%' was found"),
                Form_Validator_Promocode::ERROR_CODE_INVALID
        );
    }

} }

Im not sure that i understood well your question, but i have this code 我不确定我理解你的问题,但我有这个代码

class Gestionale_Validator_UniqueCustomMsg extends Zend_Validate_Abstract
{

    const PIVA_NON_UNICA = 'partita iva già assegnata';

    protected $_messageTemplates = array(
        self::PIVA_NON_UNICA => 'piva occupata'//verra tradotta in modo automatico
            );

then in my translation i have 然后在我的翻译中我有

piva occupata="Questa partita iva è già assengata a \"%value%\""

then i just add the error like this 然后我只是添加这样的错误

$this->_error(self::PIVA_NON_UNICA, $data['ragsoc']);

and it works, also in my bootstrap i have 它的工作原理,也在我的引导程序中

...
Zend_Form :: setDefaultTranslator ( $translate );
...

My problem was the encoding of the file which contains the translation array. 我的问题是包含翻译数组的文件的编码。 German Umlauts were not encoded properly. 德语变音符号编码不正确。 I'm using UTF-8 now and everything works. 我现在正在使用UTF-8,一切正常。

Thanks for all your efforts. 感谢您的所有努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM