TaoTao的网络记事本
In: wordpress
12 Jan 2010woothemes是非常知名的wordpress主题制作公司,制作了大量高质量的主题。虽然大多数是收费主题,但也有一部分免费的,而且在我看来免费中也有精品,比如meta-morphosis。
最近做了一个站用到了meta-morphosis,需要将该主题界面汉化。woothemes出品的主题确实是比较规范的,主题文件夹里带有专门的lang文件夹,应该是具有方便翻译的功能。但是这个lang文件夹里的.mo和.po文件到底应该怎么使用呢,我也不知道。woothemes网站的帮助文档以及讨论区都是只有付费用户才有资格进入的,所以作为未付费用户,即使是对于这个官方免费的模板,我也无法从woothemes官网上获得任何帮助资料。于是只得继续求助google,终于得到了这篇非常有用的文章:
以下是全文,有空我会翻译一下这篇。
simple solution create new .po and .mo file put in your theme folder thats it
WordPress uses a standard localization framework called GNU gettext. This framework provides the programmer the ability to mark text inside code as being suitable for localization. The role of the translator is to take these marked pieces of text and produce a language-specific localization. No code needs to change and you do not need to understand how the program works.
Text is marked by wrapping it with special PHP functions. It is not important to know how these functions work, but it will be helpful to recognize them:
__($text) – Looks for a translated version of $text and returns the result_e($text) – Looks for a translated version of $text and echo the result to the screen (i.e. effectively it is echo __($text))__ngettext($single,$plural,$number) – If $number is 1 then looks for a translated version of $single. If number is more than 1 then looks for a translated version of$plural.(Source: Urban Giraffe)
Translating WooThemes
Putting this into context, it means that all our text in our template files (.php files) have been wrapped in gettext functions. This enables us to create a Portable Object File (or .po file) which contains all the text for the theme. This has already been done by us atWooThemes.
The user (that’s you) then has the ability to use a program (we’ll get back to this later) totranslate this .po file into another language, add it to the theme directory, and have a translated theme!
Before you can start to translate the theme, you need to make sure that the theme has been made ready for translation. All our new themes will support localization and include alanguage file (.po file) which contains all the English texts, and soon all our old themes will be upgraded as well.
If you are not certain if your theme has been made ready for translation/localization, you can simply check if there is a “/lang/themename.po” file in your theme folder. If there isn’t one, you will need to download the latest version of the theme and check if the .po file existst there. If it does include a translation file, you will need to upgrade your theme to be able to translate it.
To translate your theme, you will need to use Poedit to translate the includedPortable Object file (.po file), which will then create a Machine Object file (.mo file) which we will upload to our theme folder for WordPress to read.
Follow these steps to translate your .po file into another language and make a .mo file:
Translating the .po file with Poedit
Setup WordPress
Before we can upload our new .mo file, we need to make sure we named it correctly, as it has to match your desired language locale. See the complete list of language codes and country codes to find your exact locale.
We’ll use Norwegian as the example here, so first we find the language code (nb – Norsk Bokmål) and the country code (NO). We than rename our .mo file to nb_NO.mo and upload to the root of our theme folder e.g.
wp-content/themes/themename/nb_NO.mo
The last step is to make sure your WordPress config file, wp-config.php, is setup with your locale, which in this case is Norwegian. So open wp-config.php and check that what you have in your define() function. For Norwegian it will look like this:
define ('WPLANG', 'nb_NO');
Once you have matched the wp-config.php setting to our .mo filename, you should have successfully translated your WooTheme into another language.
We want to make it easier for users to get the theme in their language, so if you have translated your theme into your language, we hope you can share the .mo file with us, so new users don’t have to translate the theme themselves.
Please send us your translated files (.po and .mo) files to support@woothemes.com and we will put your file up in our repository.