Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

AbstractValidator does not use messageKey when translating messages #260

Open
bkilinc opened this issue Apr 6, 2019 · 5 comments
Open
Assignees
Labels

Comments

@bkilinc
Copy link

bkilinc commented Apr 6, 2019

I would like to translate validation messages using messageKeys as message strings may change. But translateMessage in AbstractValidator does not use $messageKey parameter for translating, even if it is defined as method parameter. It uses message string to translate.

protected function translateMessage($messageKey, $message)
{
    $translator = $this->getTranslator();
    if (! $translator) {
        return $message;
    }
    return $translator->translate($message, $this->getTranslatorTextDomain());
}
@Ocramius
Copy link
Member

Ocramius commented Apr 6, 2019

@bkilinc can you maybe outline a test case for what you'd like the behavior to be?

@bkilinc
Copy link
Author

bkilinc commented Apr 6, 2019

I want to use something like this as a translator object. This is not possible, because AbstractValidator does not pass messageKey to translator object.

class myTranslator implements \Zend\Validator\Translator\TranslatorInterface
{
    protected $translations = [
        'dateInvalid'     => "Invalid type given. String, integer, array or DateTime expected",
        'dateInvalidDate'=> "The input does not appear to be a valid date",
        'dateFalseFormat' => "The input does not fit the date format '%format%'",
    ];

    public function translate($messageKey, $textDomain = 'default', $locale = null)
    {
        return $this->translations[$messageKey];
    }
}

@froschdesign
Copy link
Member

@bkilinc

  1. You can use zendframework/zend-i18n-resources and the messages of all validators are already translated. (you may already have seen this)
  2. The AbstractValidator uses the text of the message, because if no translation exists than the message itself is used as fallback by the translator.
  3. You can create a pull request to contribute your proposal. But keep in mind that every change of the current behaviour means a BC break and the need of a new major version. Because nobody knows if a user already has dateInvalid or dateFalseFormat in his translation files which can then lead to incorrect translations.

@bkilinc
Copy link
Author

bkilinc commented Apr 6, 2019

Thanks, I was creating my custom validator. I would like to use messageKey for looking up translations, because default message may also change. when I saw
"protected function translateMessage($messageKey, $message)"
in "AbstractValidator", I thought I could do this. But now I understand that, it is not possible with current implementation.

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-validator; a new issue has been opened at laminas/laminas-validator#1.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants