Override WordPress Plugin Translation File at Startup

I use WordPress in French with the Event Calendar plugin.

This plugin comes complete with a French translation, but it has some errors. I want to fix them, but replacing the source file is a bad idea, as it will be replaced by the next update. I contacted the developer to submit the fix, but this may take some time.

In the meantime, I would like to download a duplicate that I made from the templates directory. I have already tried several things like:

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

Or using

 add_filter('override_load_textdomain', …) 

in my functions.php, but it doesn't seem to work. The only thing I managed to do was disable the download of the source translation file.

Is there a way to replace the plugin translation file at boot time? I also use WPML, but in the "Translate with .mo files" mode is not in the "Translate to WPML" mode, so I can’t change the translation plugin on the fly. Maybe WPML can download my own event calendar translation?

+4
source share
2 answers

You can add this a few lines to the 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 will need to replace the two file variables with the actual language file.

But we will assume that the French language file is located in the folder with the languages ​​of the plugins, and your language override file is located in the folder with the language themes.

The idea is as follows:

  • Do not load a language that has already been downloaded automatically by WP
  • Download the override file first. It is important to download first, because already defined translations will be skipped when you upload another language file for this text domain (see WP core ).
  • Download the source translation file, which will actually download all the untranslated lines of the override file.

This only works with the compiled mo file.

You can add several lines to your override file that you want to override.

+3
source

I am the author of the Transposh plugin,

Your answer is actually 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 a function and priority instead of .....)

These functions will receive strings and domain, and you can use them to perform functions such as:

 function gettext_filter($translation, $orig, $domain) { if ($domain == 'plugin_domain') { if ($orig == 'some text') { return "some translation"; } } return $translation; } 
+2
source

Source: https://habr.com/ru/post/1502399/


All Articles