0

i have an problem within the translator. I want to use for every module an module specific text domain (NAMESPACE). First i've seen that ZF2 need to inject the Translator into the Validators since Version 2.2 so i've do somethink like this in my Application\Module.php

class Module
{
    public function onBootstrap(\Zend\Mvc\MvcEvent $e)
    {
        $translator = new \Zend\Mvc\I18n\Translator(
            $e->getApplication()->getServiceManager()->get('translator')
        );

        \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
    }
}

This works to inject the default Translator. So now i've need to set the TextDomain for the Validators. At the time i set them via an Validator Factory for every Module Validator like

Class PasswordFactory implements FactoryInterface
{
    public function CreateServcie(ServiceLocatorInterface $sl) {
        $validator = new PasswordValidator();
        $validator->setTranslatorTextDomain('User'); // User = Module namespace
    }
}

This works but its a little bit tedious and bloats the code. So is there an easy way to handle TextDomains for Validators? For example attach them within the Event:Dispatch or via Initializer?

regards

  • Do You call `PasswordFactory` only from module `User` ? If it right you can create `__construct()` in `PasswordFactory` and call `\Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain('User');` – newage Aug 16 '14 at 12:02

1 Answers1

0

You can set default TextDomain in Module

class Module
{
    public function onBootstrap(\Zend\Mvc\MvcEvent $e)
    {
        ...
        \Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain(__NAMESPACE__);

Yes it's not work, because NAMESPACE get local NAMESPACE

For get current __NAMESPACE__ you can use MvcEvent::EVENT_DISPATCH action.

public function onBootstrap(MvcEvent $e)
{
    ...
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'setTextDomain'), 100);
}

public function setTextDomain(MvcEvent $e)
{
    $currentNameSpace = $e->getRouteMatch()->getParam('__NAMESPACE__');
    \Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain(currentNameSpace);
}
newage
  • 899
  • 7
  • 18
  • yes i tried this. But i have problems with this. When i have for example 2 custom Modules and i load them like this in application.config.php return array( 'modules' => array( ...., 'User', 'UI' ), I overwrite the DefaultTextDomain, so when i tried to execute an request which will be get on the User Module the Validator has by default TextDomain the UI - TextDomain, because of the onBootstrap setter. I don't know, perhaps i do anything wrong? – user3549136 Aug 16 '14 at 11:01
  • `MvcEvent::EVENT_DISPATCH` will be call one time only for current `module/controller/action`. Don't need call this event on every `Module`. Call this event in default module for example `Index` – newage Aug 16 '14 at 11:03
  • ok, i've tried this - will be the same problem, because of the arrangment in the application.config.php module loading, i get the textdomain of the last loadet module. What i doesnt understand the \Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain will be an static function, so the store will be global defined, and i think the problem is, that the validator called after the last set of the TextDomain? – user3549136 Aug 16 '14 at 11:20
  • Yes, I understand you! You call another validators from another modules. – newage Aug 16 '14 at 11:27
  • You solution is right if you call another module from module! Need set `TextDomain` with domain name to validators, because you call validators from another module and ZF don't know about new `__NAMESPACE__` – newage Aug 16 '14 at 11:50
  • at this time, yes only from one module, but to let them reusable, i have to extend them by an other module in future, so i call them later from other modules – user3549136 Aug 16 '14 at 12:03
  • How do you call `PasswordFactory` ? – newage Aug 16 '14 at 12:05
  • The PasswordFactory will be called via ValidatorManager, i add the validator into an InputFilter like this $this->add(array( 'name' => 'password', 'validators' => array( array( 'name' => 'User\Validator\Password' ) ) )); at the init function, the factory will be defined via configuration ('validators') – user3549136 Aug 16 '14 at 12:09
  • Maybe better solution set `defaultTextDomain` in the `InputFilter`. It set `defaultTextDomain` for all flters – newage Aug 16 '14 at 12:11
  • Oh yes, this seems like an good solution, but i've looked into the zend code of inputfilters, and doesnt find any solution to set an translator or translatortextdomain, it looks like, that the validator will be the highest way in this case to set the translator? Did you have any ressource/infos about this way? – user3549136 Aug 16 '14 at 13:32