本文转载自WordPress啦!
虽然英语是网络上最有代表性的语言,不过制作主题时多为其他语言用户考虑给他们提供翻译化的主题,当然更好。在这里就一步步教大家如何让WordPress主题可翻译成任何语言。
1 添加必要的函数
从最基本的入手,首先在functions.php 里添加下面的代码:
load_theme_textdomain( ‘Cats Who Code’, TEMPLATEPATH.’/languages’ );
$locale = get_locale();
$locale_file = TEMPLATEPATH.”/languages/$locale.php”;
if ( is_readable($locale_file) )
require_once($locale_file);
看第一行有load_theme_textdomain() 函数,这个函数的作用是允许你加载文本域名。你可以选择任何独一无二的域名。最好选择你的主题名称。
2 国际化主题
翻译WordPress主题,要使用到php gettext 函数。 GetText有两个函数: _e 和 __ (两条下划线)。 “_e”是用来显示 “单纯”文本, __ 函数用于显示已经使用PHP标签的文本。 例如:
<?php _e(“The page you’re looking for doesn’t exist”, “Cats Who Code”); ?>
<?php the_content(__(‘Read more…’, “Cats Who Code”)); ?>
注意上面的文本域名(Cats Who Code)必须与functions.php里的文本域名保持一致。
比较枯燥的部分工作是用这些函数代替每一个字符串。这就得看你的主题里有多少字符串了,可能要花点时间。据说利用GNU 工具可以很容易实现从文件从提取字符串,不过我没有试过也不知道如何。有兴趣的朋友可以用谷歌搜索xgettext。
3 创建.po文件
完成上面两个步骤,你的WordPress主题就可以翻译为任何语言。不过要用其他语言显示出来,需要添加.po文件。
.po 文件是一个可变对象,指的是那些包含字符串及字符串另一种语言的译本的文件。例如,如果你下载法语版的WordPress主题,主题包里就会有fr_FR.po 文件。这个文件就包含了所有让你的主题用法语显示的翻译。你的主题就会用法语“Bienvenue ”显示而不是用英语“Welcome”。
好在这次你无需在的主题文件里面查找所有需要被翻译的字符串。一个免费的在线工具icanlocalize.com可以帮你扫描PHP文件并创建.po文件。ICanLocalize 会自动提取所有包含__(“txt”, “domain”) 和 _e(“txt”, “domain”) 的字符串, 包括所有双引号和单引号中的字符串以及任何字符编码。
还可以通过专门的免费软件PoEdit来编辑Po文件。
你得翻译每一个文本字符串,翻译完后保存.po文件。PoEdit会生产一个.mo文件(.po文件的编译版)。
4 实施
到此为止,你已经完成了最难的部分,剩下的就是定义WordPress主题地域。
首先是要获得你的语言和国家代码,例如,你的语言是中文,你居住在中国。你的代码为zh_CN。你可以通过GNU gettext手册的页面找到你的国家代码和语言代码。
获得你的代码后,打开wp-config.php文件查找WPLANG常数,如果存在直接用你的代码取代当前的代码,如果不存在,在wp-config.php添加下列代码(用你的需要的语言代码)即可:
define (‘WPLANG’, ‘zh_CN’)。
附原文:
How to make a translatable WordPress theme
Althought English is the most represented language over the Internet, it is a good thing to think about people who speak other languages and offer them trabslated WordPress theme. In this step-by-step tutorial, you’ll learn how to take a WordPress theme and make it translatable for any language.
1 – Add the needed functions
Let’s start by the basics: Paste the following lines of codes on your functions.php file.
load_theme_textdomain( 'Cats Who Code', TEMPLATEPATH.'/languages' ); $locale = get_locale(); $locale_file = TEMPLATEPATH."/languages/$locale.php"; if ( is_readable($locale_file) ) require_once($locale_file);
On line 1, you see the load_theme_textdomain() function. This function allow you to load a Text Domain. You can pick up any name, but keep in mind that it have to be unique. So the best practice should be to use your theme name.
2 – Internationalize your theme
For translating our WordPress theme, we’re going to use the php gettext functions.
GetText has two functions: _e and __ (two underscores).
The “_e” function is used to print “simple” text, and the __ function is used when the text to be displayed is already wrapped in php tags.
Examples:
<?php _e("The page you're looking for doesn't exist", "Cats Who Code"); ?>
<?php the_content(__('Read more...', "Cats Who Code")); ?>
Notice again the text domain name (Cats Who Code) above, remember that it should be the same as in the functions.php file.
The boring part is that you have to replace each single string by the required function. Depending on how many strings your theme have, this can take a lot of time. I’ve heard of some GNU tools to easily extract strings from files, but as I never tried it I can’t say anything about it. For those interested, google xgettext.
3 – Create your .po file
Now, your WordPress theme can easily be translated to any languages. But to display text in a foreign language, you have to add a .po file.
.po files stands for Portable Object. basically, theses files contains a string, and its translation in another language. For example, if you download the French version of WordPress, you’ll have a fr_FR.po file in the archive. This file contains all the translations needed for your theme to speak French, so your theme will say Bienvenue instead of Welcome.
Good news, you don’t have to search throught your theme files for all the string to be translated. A free online tool named icanlocalize.com can scan PHP files and create .po files for you ICanLocalize will extract all strings wrapped in __(“txt”, “domain”) and _e(“txt”, “domain”) calls. Strings can be enclosed in either double quotes (“) or single quotes(‘) and with any character encoding.

Po files can be edited with PoEdit, a free software especially dedicated to that task:

As you probably guessed, you have to translate each text string. Once you translated it all, save the .po file. PoEdit will also generate a .mo file, which is basically a compiled version of the .po file.
4 – Implementation
Right now, you have done the most “difficult” part of the job. The only thing you have to do is to define your WordPress locale.
To do so, the first thing to do is to get your language and country code. For example, if your language is French and France is your country of residence, your code will be fr_FR. The GNU gettext manual contains pages to help you find both your country and language codes.
Once you have your codes, open your wp-config.php file and look for the WPLANG constant. If it exists, simply replace the existing code by yours. If it doesn’t exists, simply paste the following line (with your own code, of course)
define ('WPLANG', 'fr_FR');
Sources
The following articles has been very useful to me for writing this post. Thanks to the authors.

