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?

有帮助吗?

解决方案

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.

其他提示

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;
}

This solution uses wordpress's automatic loaders to override the plugin and doesn't need additional programming logic, instead it lets the CMS do the override.

Let's say you want to translate a plugin called my-plugin. A properly built plugin would have two translated file in the directory wp-content/languages/plugins/ called my-plugin-fr_FR.po and my-plugin-fr_FR.mo.

If your plugin has this structure, the steps below will override the translations, if not you can try and see anyway:

  1. Copy the .po and .mo files mentioned above into the directory wp-content/languages/my-plugin. If the directory doesn't exist, create it.
  2. Edit the translation files as you wish and save them.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top