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

usage documentation for zf3 not meaningful/correct #20

Closed
rogatec opened this issue Sep 20, 2016 · 15 comments
Closed

usage documentation for zf3 not meaningful/correct #20

rogatec opened this issue Sep 20, 2016 · 15 comments

Comments

@rogatec
Copy link

rogatec commented Sep 20, 2016

When using the addTranslationFilePattern the first parameter is given as a string in the documentation phpArray.

In Zend Framework 3 the loadMessagesFromPatterns in the Translator class uses this string to get the plugin from the PluginManager (in line 655).

Unfortunately this throws an exception because the PluginManager can't handle phpArray as a string(?).

If I explicitly use the class reference it is working.

working solution:

use Zend\I18n\Translator\Loader\PhpArray;
// [...]
$translator = new Translator();
$translator->addTranslationFilePattern(
            PhpArray::class,
            Resources::getBasePath(),
            Resources::getPatternForValidator()
);

Additionally I recognized that the config array should also look like this to work properly, only using a string like MvcTranslator didn't work:

    'service_manager' => [
        'delegators' => [
            \Zend\Mvc\I18n\Translator::class => [
                TranslatorDelegator::class,
            ],
        ],
    ],

Maybe to give other users a better understanding this can be adjusted?

I think the problem occurs, when using the complete zend framwork which brings other classes with the same name like those to phpArray

@weierophinney
Copy link
Member

The problem is missing aliases.

In zend-servicemanager v2, we performed normalization that would transform PhpArray, phpArray, PHPArray, etc to phparray. In v3, we no longer do such normalization, which prompted us to write a number of aliases to the new service name.

So, the problem here is that we only define phparray as an alias, and neither of PhpArray or phpArray. Would you be willing to create a pull request adding these two aliases to src/Translator/LoaderPluginManager.php?

@rogatec
Copy link
Author

rogatec commented Sep 21, 2016

@weierophinney hope the PR is correct 🐙

btw. I saw this in the composer.json : "fabpot/php-cs-fixer": "1.7.*", <-- should this also be changed to "friendsofphp/php-cs-fixer":"^1.12", ?

@weierophinney
Copy link
Member

btw. I saw this in the composer.json : "fabpot/php-cs-fixer": "1.7.*", <-- should this also be changed to "friendsofphp/php-cs-fixer":"^1.12", ?

No, it should be changed to "squizlabs/php_codesniffer": "^2.6.2" 😠 (because phpcs at least doesn't introduce new arbitrary, non-related rules that lead to CS "failures" on new versions...)

@rogatec
Copy link
Author

rogatec commented Sep 27, 2016

obviously there is already a pull request by @samsonasik with zendframework/zend-i18n#56

@rogatec rogatec closed this as completed Sep 27, 2016
@bartoszzajac
Copy link

@rogatec I suppose that TranslatorDelegator::class refers to file created here https://github.com/zendframework/zend-i18n-resources/blob/master/doc/book/usage.md#automating-resource-injection. I put this in Application/Delegator/TranslatorDelegator.php file and changed TranslatorDelegator::class to Application\Delegator\TranslatorDelegator::class and my TranslatorDelegator is never called. What should I do?

@rogatec
Copy link
Author

rogatec commented Oct 5, 2016

@bartoszzajac your url behind the link is broken ;)

I have a custom TranslatorDelegator::class which implements DelegatorFactoryInterface.
The __invoke method looks like this:

public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
{
    $config = $container->get('config');

    if (!isset($config['translator']['locale'])) {
        throw new InvalidArgumentException("translator/locale array part missing in global config file");
    }

    $translator = new Translator();
    $translator->addTranslationFilePattern(
        PhpArray::class,
        Resources::getBasePath(),
        Resources::getPatternForValidator()
    );
    $translator->setLocale($config['translator']['locale']);

    return new \Zend\Mvc\I18n\Translator($translator);
}

In the config.module.php of the module I'm implementing into the returned array the following part:

'service_manager' => [
    'factories' => [
        // other module factories
    ],
    'delegators' => [
        \Zend\Mvc\I18n\Translator::class => [
            TranslatorDelegator::class,
        ],
    ],
],

Lastly to get things working I implemented the onBootstrap method in my Module.php class.

public function onBootstrap(MvcEvent $event)
{
    /** @var Translator $translator */
    $translator = $event->getApplication()->getServiceManager()->get(Translator::class);
    AbstractValidator::setDefaultTranslator($translator);
}

I hope this can answer your question. If not please provide a little bit of your code implementation.

@bartoszzajac
Copy link

Broken link is official documentation: https://github.com/zendframework/zend-i18n-resources/blob/master/doc/book/usage.md#automating-resource-injection (I just copied this file). You did this in some different way. I need to analyse that.

@rogatec
Copy link
Author

rogatec commented Oct 5, 2016

I have to admit, that I also tried it for several hours and the working solution from the previous comment is my result :-/ I don't know if this is the correct way.

@bartoszzajac
Copy link

@rogatec can you tell what is your file structure for TranslatorDelegator?

@rogatec
Copy link
Author

rogatec commented Oct 5, 2016

{PROJECT_ROOT}/module/Main/src/Factory/TranslatorDelegator.php

therefore my namespace is namespace Main\Factory in the TranslatorDelegator.php

@bartoszzajac
Copy link

Do you have 'factories' key for TranslatorDelegator in service_manager config?

@rogatec
Copy link
Author

rogatec commented Oct 5, 2016

No, only delegators

'service_manager' => [
    'delegators' => [
        \Zend\Mvc\I18n\Translator::class => [
            TranslatorDelegator::class,
        ],
    ],
],

@bartoszzajac
Copy link

Ok it is working now. I missed onBootstrap part that you have provided. I haven't change TranslatorDelegator from documentation so it is working.

@phgeek
Copy link

phgeek commented Dec 11, 2016

Thanks a lot @rogatec!!! You saved my day.

As said at #106 on zend-validator 'phpArray' is now 'phparray'.

@85jacek
Copy link

85jacek commented May 25, 2017

That's the fastest way without configuring delegators:

public function onBootstrap(MvcEvent $e)
   {
       AbstractValidator::setDefaultTranslator($e->getApplication()->getServiceManager()->get('MvcTranslator'));
   }

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

No branches or pull requests

5 participants