Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using own translations #8160

Closed
christophlehmann opened this issue Jun 20, 2015 · 7 comments
Closed

Using own translations #8160

christophlehmann opened this issue Jun 20, 2015 · 7 comments
Labels
answered For when a question was asked and we referred to forum or answered it. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change.
Milestone

Comments

@christophlehmann
Copy link

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
Copy link
Member

mattab commented Jul 15, 2015

@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
Copy link
Member

sgiehl commented Jul 15, 2015

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.

@mattab mattab added this to the Mid term milestone Jul 15, 2015
@mattab mattab added the Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change. label Jul 15, 2015
@christophlehmann
Copy link
Author

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
Copy link
Author

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
Copy link
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
Copy link
Member

mattab commented Jul 22, 2015

@christophlehmann you got the answer. Thanks @diosmosis

@mattab mattab closed this as completed Jul 22, 2015
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label Jul 22, 2015
@Findus23
Copy link
Member

Thanks to @diosmosis' nice trick, I wrote a small plugin that allows overwriting translations:
https://plugins.matomo.org/CustomiseTranslations

It is really basic (only allows one language for now), but should be useful if one wants to edit one string in Matomo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change.
Projects
None yet
Development

No branches or pull requests

5 participants