通过截获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.

小人书!

小摊上看到,挑了一些古代人物和故事的。看上去挺新,但是印刷质量一般,疑心是盗印的。不过重点是内容不错,尤其是文字自然平实,简练流畅。给小朋友看再好不过。贴一下照片:

唐伯虎:上海人民美术出版社
文:夏振亚
图:王义秋

Tang Bohu
cover image of story book “Tang Bohu”.

满江红:人民美术出版社
文:范钧宏 吕瑞明 任梅
图:墨浪

Man Jiang Hong
Cover page of storybook “Man Jiang Hong”

八仙过海:中国旅游出版社
文:李源 任宝贤
图:韩亚洲 范世评

Ba Xie Guo Hai
Cover page of story book “Ba Xie Guo Hai”

海瑞罢官:上海人民美术出版社
文:吴晗 李大发
图:黄全昌

20150723_173425

陆游:上海人民美术出版社

继续阅读小人书!

中国古诗文

正在考虑建立一个中国古诗文网站,目的是给孩子看,类似于《唐诗三百首》和《古文观止》,内容也多从其中来。特色在于:

  • 在内容上,以原文为本。不要译文,基本没有注释。
  • 在布局上,以内容为主,去除一切干扰因素。要求干净,大方。
  • 各古诗文名家作者就是blog作者,既直观有趣,又方便组织。

需要做的工作:

  1. 开发批量插入作者的功能——已完成;
  2. 开发批量插入帖子的功能——已完成;
  3. 建立分类目录:需要专业人员帮助。
  4. 开发新的数据结构以存储传统计时方式:朝代,年号,年号纪年,公元纪年,季节,时令,时辰。
  5. 扩展帖子属性,其中帖子发表时间采用新的数据结构。
  6. 扩展作者属性,其中作者生卒时间采用新的数据结构。
  7. 修改界面,使帖子能够显示传统计时(年号,天干地支)。

当前状态见这里:http://gushiwen.genglinxiao.com/

Blog move done

Finally completely moved all my blog posts from the original blogspot site to my own server.

All posts are here now and the original site:

http://retidigca.blogspot.com/

is deprecated now.

Still a lot of topics were started but haven’t been completed. I will try to put some existing draft online recently.

building self-aware device – part 2

After about 15 months, here’s part 2. You can see part1 here: building self-aware device – part 1

So now we define self-awareness as:

  1. Knows ones own properties and boundary;
  2. Able to learn ones own identity from self-initiated training;

Can we build device that is self-aware? After some thought, we have to say, achieving the most general sense of item 1 is way beyond our reach. Animals learn its own properties and boundary (again) through learning. The learning process correlates the visual signal from eyes,  touch signal from sensors covering the whole body, signals from motor neurons and maybe more. We might be able to build an artificial eye, but currently there’s no technology that come close to a distributed sensing system like the skin and the fur, or a distributed control system like the muscle.

But, if we limit our device to have a rigid body, then it becomes something we can handle, at least to a certain extent.

If the 3D model (the shape, the size) of the rigid body is known. Then with a GPS receiver installed in a fixed point within the rigid body, and a gyroscope to tell the orientation of the device, we basically have a device that knows it’s own properties and boundary. (In our simplified case, both the properties and the boundary are static. Properties are whatever inside the rigid body, boundary is the boundary of the rigid body.)

Following this approach, we might be able to add some moving parts into this rigid body gradually.

<Here further expansion is needed>

Then let’s move to the next step. Let’s put it in front of a mirror. The device has to start some random movement, and then correlate the movement with the movement it sees in the mirror.

For that we need a neural network, the input would be, on the one hand, the instructions for the random movement and on the other hand, the actual movement it sees in the mirror. So this becomes a supervised learning problem.

So we see that building self-aware device is still a long way to go. However, I believe we should be able to experiment it in controlled scenarios, like fully automated driving, and try to push the limit to see how far can we go.

Computer, another origin

When talking about computer origin, people tend to think about chips, CPU, world war II and ENIAC, and in some occasions, date back to slide rule and Suanpan. This is also how it is taught in schools and universities. The logic behind this story line is, computer originated from the need for fast computing, especially arithmetic computing.

However, there’s another origin that is at least as important, has a longer history, and at least to some people, more fascinating. That is how the architecture of modern computer came into being. Computer nowadays is so powerful that sometimes it seems inconceivable to link it to its ancient ancestors. On the other hand, you only have to look into some of the remarkable masterpieces of the past to know the linkage is simply undeniable.

A recent BBC program I watched, “Mechanical Marvels – Clockwork Dreams” showed one of such remarkable masterpieces: A mechanical machine boy that is able to write up to 40 letters of text, depending on the configuration, built back in 1770s.

What makes it remarkable is, the fact that the text is customizable. That means, the mechanical boy is programmable.

Of course, it’s still an Finite State Machine, but being programmable, that means adding scratch memory to it and then it will be a complete Turing Machine!

It’s actually from this origin where techniques were developed for modern computer scientists to deal with abstract topics like computational complexity, formal language.