通过截获gettext调用来修改WordPress的部分翻译

这是上一篇文章的中文版。

WordPress网站的页面底端通常有一句话,英文原文是“Proudly Powered by WordPress”,链接到WordPress网站。这句话当前的官方翻译是,“自豪地采用WordPress”。我个人认为,这个翻译是蹩脚的字面翻译,中国人不大会用这种语言致谢。更好的翻译应该是“低调地使用WordPress”。

但是要修改这一句话还不太容易,比较了多种方法之后,我采用了如下方法:

  1. 创建一个child theme。
  2. 在child theme的functions.php中加入如下代码:

[code language=”php”]
function change_attribute_line( $translated_text, $text, $domain ) {
        switch ( $text ) {
            case ‘Proudly powered by %s’ :
               $translated_text = __( ‘低调地使用%s’);
           break;
}
return $translated_text;
}
add_filter(‘gettext’, ‘change_attribute_line’,20,3);
[/code]
就可以了。

代码基本没有需要解释的地方。我在此处搜索$test而不是$translated_text仅仅是因为我觉得匹配英文字符串可能更快一些。

值得注意的是,此处的字符串中仍然包含有占位符”%s”。这就是说,gettext和它的filter是在占位符”%s”被替换之前调用的。

如果我搜索替换后的字符串“Proudly powered by WordPress”,是搜索不到的。我刚开始不明白(不仅WordPress文档没有指出这一点,网上好像也没有人遇到这个问题)所以在此花了很长时间。希望看到帖子的同学们可以节省一些时间。

Replacing WordPress translation by hooking to gettext

In a typical WordPress website, you see this line in the page footer:

“Proudly powered by WordPress”

This line links to WordPress website. The official Chinese translation of this line is:

“自豪地采用WordPress”

I’m using WordPress in several of my websites and I found it’s annoying: It’s a word to word translation and it’s simply not what we Chinese would say when we want to attribute something to someone. After some thinking, I think the appropriate translation should be:

低调地使用WordPress

“采用”emphasis the choosing of WordPress, “使用”emphasis the fact we are using WordPress right now.

The message is clear and typical Chinese: I’m a humble webmaster. All the glories go to WordPress.

So I set out to change the translation. It turned out to be rather complicated, but here’s how I finally accomplished it:

  1. Create a child theme of your current theme, because you’ll have to rewrite its logic and you don’t want your effort to be overwritten by an upgrade of that theme. The procedure of creating a child theme can be found here.
  2. In your function.php file, add the following lines:

[code language=”php”]
function change_attribute_line( $translated_text, $text, $domain ) {
        switch ( $text ) {
            case ‘Proudly powered by %s’ :
               $translated_text = __( ‘低调地采用%s’);
           break;
}
return $translated_text;
}
add_filter(‘gettext’, ‘change_attribute_line’,20,3);
[/code]

That’s it.

Notice that I search for $text instead of $translated_text. You can also search for $translated_text. That would be:
[code language=”php”]
        switch ( $translated_text ) {
            case ‘自豪地采用%s’ :
               $translated_text = __( ‘低调地使用%s’);
           break;
}
return $translated_text;
[/code]
I search for $text because I assume (I’m not sure) search for non-unicode string will be faster.

It’s worth noting that both strings have placeholders in it. Initially I was trying to search the whole string, “Proudly powered by WordPress” or “自豪地采用WordPress” and failed. The fact that the above code works tells us, gettext() and its filters are called before the placeholders get replaced. I spent a lot of time figuring this out. It’s not mentioned in the document, nor did anyone post this on the web. This is actually the key reason why I write this post. Someone from WordPress should update the document.