Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a WordPress plugin translation file on load

Tags:

wordpress

wpml

I'm using WordPress in french with the plugin The Events Calendar.

This plugin comes with a bundled french translation but it has some mistakes. I want to fix them but replacing the original file is a bad idea since it's gonna be replaced with the next update. I contacted the developer to submit a fix but it may take some time.

In the meantime, I would like to load a duplicate I did from my template directory. I already tried multiple things like:

load_plugin_textdomain( 'tribe-events-calendar', get_template_directory() . '/languages' );

Or with

add_filter('override_load_textdomain', …)

in my functions.php but it doesn't seem to work. The only thing I was able to do is disabling the load of the original translation file.

Is there any way to replace a plugin translation file on load? I use WPML too but in "Translate with .mo files" mode not in "Translate with WPML" so I can't change plugin translation on the fly. Maybe WPML can load my own translation of The Events Calendar?

like image 800
LeBen Avatar asked Sep 16 '13 11:09

LeBen


2 Answers

You can add this few lines in your functions.php theme file

$text_domain = 'the-events-calendar';
$original_language_file = ABSPATH . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'the-events-calendar' . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'the-events-calendar-fr_FR.mo';
$override_language_file = ABSPATH . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'your-own-theme' . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'the-events-calendar-fr_FR.override.mo'; 

// Unload the translation for the text domain of the plugin
unload_textdomain($text_domain);
// Load first the override file
load_textdomain($text_domain, $override_language_file );
// Then load the original translation file
load_textdomain($text_domain, $original_language_file );

You'll have to replace the two file variables with the actual language file.

But we'll suppose that the french language file is in the plugin language folder and your override language file is in the language theme folder.

The idea is to :

  • Un load the language that has already been loaded automatically by WP
  • Load your override file first. This is important to load it first, because already defined translations will be skipped when you'll load another language file for this text domain (see WP core).
  • Load the original translation file, which will in fact load all the untranslated strings of the override file.

This works only with compiled mo file.

You can only add to your override file the few strings you want to override.

like image 130
Jiwoks Avatar answered Nov 15 '22 23:11

Jiwoks


I am the author of the Transposh plugin,

Your answer actually is in the following four filters:

   add_filter('gettext', ......, 3);
   add_filter('gettext_with_context', ......, 3);
   add_filter('ngettext', ......, 4);
   add_filter('ngettext_with_context', ....., 4);

(Naturally, you need to add the function and the priority instead of the .....)

Those functions will get the strings and the domain, and you can use those to do functions like:

function gettext_filter($translation, $orig, $domain) {
    if ($domain == 'plugin_domain') {
        if ($orig == 'some text') {
            return "some translation";
        }
    }
    return $translation;
}
like image 20
oferwald Avatar answered Nov 15 '22 23:11

oferwald