简体   繁体   English

在加载时覆盖WordPress插件转换文件

[英]Override a WordPress plugin translation file on load

I'm using WordPress in french with the plugin The Events Calendar . 我正在使用带有插件The Events Calendar的法语WordPress。

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. 在我的functions.php中,但它似乎不起作用。 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. 我也使用WPML,但在“Translate with .mo files”模式中没有“Translate with WPML”,所以我无法动态更改插件翻译。 Maybe WPML can load my own translation of The Events Calendar? 也许WPML可以加载我自己的事件日历翻译?

You can add this few lines in your functions.php theme file 您可以在functions.php主题文件中添加这几行

$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 卸载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 ). 这对于首先加载它很重要,因为当您为该文本域加载另一个语言文件时,将跳过已定义的翻译(请参阅WP核心 )。
  • 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. 这仅适用于已编译的mo文件。

You can only add to your override file the few strings you want to override. 您只能向覆盖文件添加要覆盖的几个字符串。

I am the author of the Transposh plugin, 我是Transposh插件的作者,

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. 此解决方案使用wordpress的自动加载器来覆盖插件,不需要额外的编程逻辑,而是让CMS执行覆盖。

Let's say you want to translate a plugin called my-plugin . 假设你要翻译一个名为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 . 一个正确构建的插件将在目录wp-content/languages/plugins/有两个已翻译的文件,名为my-plugin-fr_FR.pomy-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 . 将上面提到的.po和.mo文件复制到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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM