Click to Download the Theme for Free
Recently I’m doing some wordpress theme designing for XMind Ltd. They want a blog for multi-language support. Since it could be useful for many people, I would like to share my experiences.
我最近在给Xmind这家公司做一个多语言的Blog模板,感觉效果还不错,把我的经验共享一下:
The theme is based on a clean blue theme “Cold Blue” which matches the company’s color scheme perfectly and it’s free. I made some changes so that it looks better for me. But the problem is the theme has no support for multilingual. So I have to dig into the theme PHP files and manually change that. So:
我的模板是下载的 “Cold Blue” 这个比较简洁的模板,我很喜欢,但是它并没有多语言的支持,所以我只好手动修改PHP文件,下面介绍一下如何把一个模板加入多语言的支持。
How to add multilingual support for a theme?
The answer is: You have to tell wordpress which is a multilingual string.
Like said in this blog, you first register a name for yourself in function.php.
你要做的就是在模板里把需要翻译的地方标记出来,先要在function.php里面给你的模板注册一个名字,这样才能在里面使用。
load_theme_textdomain('coldblue')
And in other theme files, you can now tell wordpress which are multilingual strings in your file. Basically there are following things you may want to use:
然后在模板里,看到一段字符串就把它们替换成下面的写法吧。注意一下_e('','')是直接输出,适用于一般的文字内容。而__('','')是返回一个字符串,这样可以放在其它函数里面调用。如果对于比较复杂的情况,可以使用printf来输出。不明白可以看我提供的下载里面是怎么用。
<?php _e('Hello world', 'coldblue') ?> <!--Will output the string directly--!> <?php some_function( __('Hello world', 'coldblue') ) ?> <!-- Returns a string for function use --!> <?php printf(__('Sometimes %s and %s and %s'),$a1,$a2,$a3);?> <!-- Complicate things--!>
So you need to change every possible strings in your theme into multilingual notations. And after that you can ask the tool gettext to find those things for you!
一旦标记完成了之后,就可以用gettext工具把所有标记过的东西查找出来,如下:
cd wp-content/themes/coldblue find . -iname "*.php" > files.tmp xgettext --language=PHP --indent --keyword=__ --keyword=_e \ --keyword=__ngettext:1,2 -s -n --from-code=UTF-8 -f files.tmp
Then you will get a file calledmessage.po. You may rename it for your locale setting, like zh_CN.po, and edit the first lines of the file like this:
然后得到一个message.po的文件,然后你可以把它重命名一下,把开头的一段话改成以下的样子:
msgstr "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-08-03 17:35+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=1 != 1;\n"
And then translate items one by one in .pofile. Once you finished, a compile job is needed:
当把.po文件里面的所有行都翻译过了之后,可以把它编译成机器识别的.mo文件
msgfmt -o zh_CN.mo zh_CN.po
There are some tricky things about the date format. My way is to make the date format string multilingual:
对于一些比较麻烦的事情比如日期格式,其实可以把它本身就设成多语言字符串,这样就可以分别独立进行设置了。其实不同语言的格式也是可以随便改的,把css地址也设成多语言字符串,然后你就知道怎么回事了吧。
get_the_time(__('F jS, Y','coldblue'))
msgid "F jS, Y" msgstr "Y年n月j日"
You may need to edit wp-config.php to change define ('WPLANG', 'zh_CN'); for a refresh, or simply wait for the next thing:
如果对于一般应用的话,可以直接改wp-config.php来设置语言,但这并不是真正的多语言。
How to add multilingual contents?
A very nice plugin called WPML. It will:
我找到的一个插件是WPML,简直是为我量身定做。
- Allow you to create translations of posts or pages manually. (I don’t want the “Google Translate” way. 可以为每篇日志创建不同语言的版本而不是用Google翻译的办法。
- Skip contents if no translation is available. So the page will look native. 如果没有相关语言的文章版本,可以设置直接不显示。
- Setup the theme locale for each language. 为不同的语言版本设置不同的模板语言文件,我们刚才做的那些东西就可以用上了。
- Translate widget titles, blog titles, tags and categories. 可以设置翻译各处的小标题,标签名,分类名,保证不留死角。
You may follow the manual of the plugin which is very clear about everything. I just want to show some screenshots: 具体如何设置看它的手册已经写得很清楚了。我只是大概贴几张截图,大体介绍一下。
Setting translation for posts: 设置文章的版本

Theme properties, you know how to use the .mo, .potranslations now. 模板的语言设置

And of course I would like to share the modified theme to you. 就知道有人想坐享其成,欢迎笑纳。
However I’m not yet so interested in making a multilingual site myself, I think writing them together is fine for a normal blogger
不过现在我还不打算用那种方式来多语言,对于我一个平头百姓来说,这样英汉混杂大家不影响理解也就好了。



2 Comments
冰桃:
August 4, 2009哈 不错 留着以后用