Translator Dependency Injection

Hello, I hope that this is the right place to ask. I want to be able to override the translations within my plugin. During my research I already found this GitHub issue here (Using own translations · Issue #8160 · matomo-org/matomo · GitHub), but it seems like the solution described there is already outdated. I looked around in the existing code-base and found an existing dependency injection (within config/environment/dev) which looks like following:

return array(

    'Piwik\Cache\Backend' => DI\object('Piwik\Cache\Backend\ArrayCache'),

    'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\LoaderCache')
    'Piwik\Translation\Loader\DevelopmentLoader' => DI\object()


I’ve following code within my plugins config.php

return array(

    'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\LoaderCache')->constructor(DI\get('Piwik\Plugins\MyPlugin\CustomTranslationLoader')),
    'Piwik\Plugins\MyPlugin\CustomTranslationLoader' => DI\object()->constructor(DI\get('Piwik\Translation\Loader\JsonFileLoader')),


CustomTranslationLoader looks like that:

namespace Piwik\Plugins\MyPlugin;

class CustomTranslationLoader implements \Piwik\Translation\Loader\LoaderInterface
    * Basically I copied the DevelopmentLoader

Unfortunately it seems like this won’t work out. Is there anything else I’ve to consider?

Thank you!

No idea why I figured out my mistake right after posting here (unfortunately I can’t delete this by myself).
So the solution from linked GitHub issue worked, I had just to extend the JsonFileLoader correctly (Instead of extends JsonFileLoader it has to be \Piwik\Translation\Loader\JsonFileLoader ).

1 Like