Override translations in plugins

Hi, I am researching the following use case:

A plugin uses the t() helper for some frontend strings; these are translated into (currently) two languages in languages/*.php files.

Is there a way to “inject” my own translations for these strings via the config.php on my site?

This could be for two motives:

  • either because I need to override the default hardcoded into the plugin for my language (in this specific case: the plugin uses the German informal “Du” but my site uses the formal “Sie”), or
  • because my site is running in a locale the plugin does not (yet) provide a translation for.

I looked through the docs and the code of I18n, but couldn’t figure it out myself. I’d greatly appreciate for any pointers! The only thing I dug up was this older thread about overriding Kirby’s default translations (not plugins); I could not find anything related in the Ideas repo either.

Hi Sebastian,

You can use a dedicated plugin to override language strings. Just make sure its folder name starts with an “x” or something so it will be the last plugin that will be loaded (kind hacky, I know, but it works).

Like this:

<?php

Kirby::plugin('bvdputte/translations-override', [
    'translations' => [
        'en' => [
            "someLangString" => "Sie haben die Ăśbersetzung angepasst",
        ]
    ]
]);

Maybe, there’s a less hacky way using the system.loadPlugins:after hook and use a function that overrides language strings after all plugins are loaded? But I have not looked into that.

Hi Bart,

Thank you for those ideas. That sounds hacky indeed (and there does not seem to be documentation on how to work with system.loadPlugin:after, so I could not figure it out), but it indeed works! So it has to be a plugin, since config.php does not offer a translations option, and the plugin has to be loaded after the plugin I want to override. Makes sense.

I also tried adding the string from the plugin to my site/languages/*.php files, but that does not work either (which surprises me, as I’d consider that a place to set language strings for everywhere over my site, including plugins).

Now, if I am writing a plugin myself where users should be able to override the built-in translations (e.g. because not everybody might like the informal “Du”), I would have to provide some means for that myself? What appears to work is using option() helpers within my languages/*.php files - that enables developers to adjust the strings as they see fit.

Maybe I should create an Ideas issue for a more generic override feature?

1 Like

AFAIK That’s because plugins can override everything. That’s also why this trick, with storing the translations in a plugin and make sure it is the last one to load, works.

Now, if I am writing a plugin myself where users should be able to override the built-in translations

Maybe you could use Hristiyan’s variables plugin for this? It enables editors to edit the custom language variables.

Thanks for the tip - will keep this in mind for future needs!

Here’s what I went with for my current project (where editing the translation strings in the panel is not required), using the options() helper in the languages/en.php of the plugin:

<?php
$translations = [
    'headline.title' => 'Title',
    'headline.field' => 'Field',
];
foreach ($translations as $key => $value) {
    $return['myplugin.' . $key] = option('sgkirby.myplugin.t.en.' . $key, $value);
}
return $return;

That way, developers can override the strings in their config.php using the 'sgkirby.myplugin.t.en.headline.title' option (and - on multi-language sites - for other languages with the respective language acronmy as well).

Still a bit hacky, but it serves the purpose :slight_smile:

1 Like