@christophlehmann opened this Issue on June 20th 2015

Is there a way to use own translations overruling piwiks translations or is there a way to extend the translate function like

class OverrideTranslations extends \Piwik\Translation {
    public function translate($translationId, $args = array(), $language = null) {
        return $myTranslation;
    }
}
@mattab commented on July 15th 2015 Owner

@sgiehl do you maybe know if it's already possible for a plugin to overwrite translation of another plugin? if not, we don't need to add this feature quite yet, but i was just wondering if it's already possible...

@sgiehl commented on July 15th 2015 Member

Good question. IIRC it was not possible in the first version. But I'm not sure if that is still the same after Translation system was refactored.

@christophlehmann commented on July 15th 2015

It would be nice to be able to.

Background:
There is the iframe i can add on a website triggering the DNT in piwik. Iframes are not a good option in responsive sites, but i dont see another good way for that, so the idea was just to show the checkbox without the text...

What about my example code? Is there a way to overwrite the usage of other classes?

@christophlehmann commented on July 15th 2015

Another way to overwrite especially this text can be another iframe url parameter like the cssfile parameter. This way every site can decide which text to use. What do you think of that?

@diosmosis commented on July 16th 2015 Member

You could use dependency injection to override translators like this:

  1. Create a new class in your plugin that derives from JsonFileLoader and overrides translations in load(), eg:
class MyCustomLoader extends JsonFileLoader
{
    public function load($language, array $directories)
    {
        $translations = parent::load($language, $directories);

        $translations['...'] = 'my custom text';

        return $translations;
    }
}
  1. Then in your plugin, create a file /path/to/piwik/plugins/YourPlugin/config/config.php with the contents:
<?php

return array(

    'Piwik\Translation\Loader\JsonFileLoader' => DI\object('Piwik\Plugins\YourPlugin\MyCustomLoader'),

);

This way you can hardcode overrides. Note, though that this is not part of the public API. So we might break it in the future w/o warning.

@mattab commented on July 22nd 2015 Owner

@christophlehmann you got the answer. Thanks @diosmosis

This Issue was closed on July 22nd 2015
Powered by GitHub Issue Mirror