zendframework / zend-validator

Validator component from Zend Framework
BSD 3-Clause "New" or "Revised" License
181 stars 136 forks source link

Zend\Mvc\I18n\Translator no longer exists #142

Closed prevostc closed 7 years ago

prevostc commented 7 years ago

I'm not sure when this has been introduced but Zend\Mvc\I18n\Translator no longer exists (cf. zend-validator translation doc).

Modules used:

Following the doc blindly I have an issue:

use Zend\I18n\Translator\Resources;
use Zend\Mvc\I18n\Translator;
use Zend\Validator\AbstractValidator;
...
$translator = new Zend\Mvc\I18n\Translator();
AbstractValidator::setDefaultTranslator($translator); 
// Error: Class 'Application\Zend\Mvc\I18n\Translator' not found

Using the i18n module, I have another issue:

use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
...
$translator = Translator::factory([
    'locale' => 'fr_FR',
]);
AbstractValidator::setDefaultTranslator($translator);
//TypeError: Argument 1 passed to Zend\Validator\AbstractValidator::setDefaultTranslator() must be an instance of Zend\Validator\Translator\TranslatorInterface, instance of Zend\I18n\Translator\Translator given

I plan to wrap the I18n Translator to make it pass the AbstractValidator type checking but I thought you guys should know about this issue. Please note that I could be doing something wrong here.

EDIT: the translator wrapper

namespace Application\Util;

use Zend\I18n\Translator\Translator as ZendI18nTranslator;
use Zend\Validator\Translator\TranslatorInterface;

/**
 * Class Translator
 *
 * The Zend validator expects a translator that implements a specific interface
 * The zend i18n translator does not implement this interface
 * the zend-mvc translator did but it's being refactored
 * Related issued
 *   https://github.com/zendframework/zend-validator/issues/142
 *   https://github.com/zendframework/zend-validator/issues/95
 * @package Application\Util
 */
class Translator extends ZendI18nTranslator implements TranslatorInterface
{
    // nothing to do here as both modules share the same translator api
}

EDIT 2: In the end, to enable translation on default zend validators, you have to include another package: zend-i18n-resources:

use Application\Util\Translator;
use Zend\Validator\AbstractValidator;
use Zend\I18n\Translator\Resources;
....
$translator = Translator::factory([ 'locale' => 'fr', ]);
$translator->addTranslationFilePattern(
    'phparray', // WARNING, NO UPPERCASE
    Resources::getBasePath(),
    Resources::getPatternForValidator()
);
AbstractValidator::setDefaultTranslator($translator);

I dit it, but that was not the smoothest development process I had to experience.

weierophinney commented 7 years ago

The MVC <-> i18n integration was removed to its own package for 3.0; read more about it in the [migration documentation] (https://docs.zendframework.com/zend-mvc/migration/to-v3-0/#i18n-integration).