开发者

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:

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. 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. I did this with Zend_Translate providing an array adapter.

return array(
  // Zend_Validate_Alnum
  'notAlnum'     => "'%value%' darf nur Buchstaben und Zahlen enthalten",
  'stringEmpty'  => "'%value%' Diese开发者_开发技巧r 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. I get an empty message if 'captcha' is present within the translation array. 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). 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.

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
        );
    }

}


I-m 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.

Thanks for all your efforts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜