如何将你的 WordPress 插件、主题国际化或本地化你喜欢的主题、插件

WordPress今天刚好在WordPress中文文档站上修改、翻译了一篇关于如何将WP插件国际化的文章,认为对主题模板也很适用。再加上最近读到的一篇英文的关于中文化WP主题的博客文章,略有感触。所以想将一些这两篇文章没有提及的,但可能对制作和汉化主题模板又有帮助的写在这,仅供有兴趣的朋友做做参考。

如果你想先阅读我刚提到的这两篇文章,请分别点击下列连接:

  1. 插件国际化
  2. Localizing WordPress Themes and Plugins

我就不重复上述两篇文章提到的所有技术细节,主要想分享值得注意的两个细节。

一, 插件和主题模板的 mo 语言文件取名和该上传到什么地方。

大家知道,如果想汉化整个 WordPress,就是下载或自己生成一个叫 zh_CN.mo 的文件然后上传到 wp-content/languages 子目录里,然后修改 wp-config.php文件就行了。但在主题和插件的本地化中略有不同。

在插件中,.mo 文件的名字应该与插件国际化“文本域”名称有联系,格式是 $textdomain-ll-CC.mo。比方说,我的一个插件叫 superit,那么我的汉化文件名就应该取成 superit-zh_CN.mo。以此类推可知,$textdomain 是文本域, ll 是语言名, CC 是国家区域代号。注意:在文本域与语言、国家代码之间是小横线(-),而在语言和国家代码之间是下划线(_)。

然而主题中的 mo 文件仍可以取名为 zh_CN.mo 如果你用的函数是 load_theme_textdomain 的话。

为什么有这样的差别了,那时因为在你安装的 WordPress 的 wp-includes/l10n.php 文件里对 load_plugin_textdomain 和 load_theme_textdomain 是这样定义的。

function load_plugin_textdomain($domain, $path = false) {
$locale = get_locale();
if ( empty($locale) )
$locale = ‘en_US’;

if ( false === $path )
$path = PLUGINDIR;

$mofile = ABSPATH . “$path/$domain-$locale.mo”;
load_textdomain($domain, $mofile);
}

function load_theme_textdomain($domain) {
$locale = get_locale();
if ( empty($locale) )
$locale = ‘en_US’;

$mofile = get_template_directory() . “/$locale.mo”;
load_textdomain($domain, $mofile);
}

从上可以看出,插件使用本地化的语言mo文件是还加上了 load_plugin_textdomain 的第二个参数的值。而 load_theme_textdomain 并没有这样。

同样的道理是做好的 mo 文件上传的路径也不一样。

插件的还要取决于我在上面提到的那个 load_plugin_textdomain($domain, $path)函数中的第二个参数$path。假如我在我的插件 functions.php 文件用到的是 load_plugin_textdomain (‘superit’,’wp-content/plugins/superit’); 则我应该将我的 superit-zh_CN.mo 上传到我的插件子目录 wp-content/plugins/superit 里面。

如果我的函数写成是 load_plugin_textdomain (‘superit’,’wp-content/plugins’); 那将 .mo 文件上传到 wp-content/plugins 子目录里就行了,若不小心还是上传到 superit 插件子目录里,那这插件可能得不到汉化。所以,汉化时请注意你要用的插件里的 load_plugin_textdomain 路径参数。

而主题却不一样,汉化时只要把做好的 zh_CN.mo 上传到你使用的主题的那个子目录里然后保证你在 wp-config.php 里启用中文就行 了。

二、 怎样在插件和主题里调用 gettext 的函数子程序

按照 WP 官方文档的推荐,在插件和主题里应该生成独特的“文本域”来区分 WP 核心部分的本地化。

实现上述需求要用 load_plugin_textdomain ($domain, $path) 函数子程序。在这里,$domain 应该是你自己原选好的独特的“文本域”,一般是你插件或主题的名字。而$path就是语言 mo 文件所在的位置。

什么时候调用 load_plugin_textdomain ?基本原则是越晚越好,因为有些多语言的插件可以造成一些地域性属性值的改变,但它又需要在 __(), __e()这些函数被调用之前。

我在我一个叫 superit 插件(它自带文件夹)里使用的方法 是将下面这段代码放在 functions.php 文件里:

<?php
add_action (‘init’, ‘my_plugin_init’);

function my_plugin_init ()
{
load_plugin_textdomain (‘superit’,’wp-content/plugins/superit’);
}

?>

当然,在主题本地化中就是用 load_theme_textdomain($domain)了,理由见上。所以我的叫 superit 的主题应该有这样的代码:

<?php
add_action (‘init’, ‘my_theme_init’);

function my_theme_init ()
{
load_theme_textdomain (‘superit’);
}

?>

如果在使用过程中收到 header error 的错误信息,将这段代码取消就没事了。所以提醒大家注意这个load_plugin_textdomain 和 load_theme_textdomain 的位置。

7 Responses to “如何将你的 WordPress 插件、主题国际化或本地化你喜欢的主题、插件”

  1. Sterling Montez

    Thanks for the link. What a bunch of crap that is. However I am right with her on the fact of IF Jill and I say IF Jill went to AA that is HER business. Ramona is a hard core drunk on the show and everyone can see it. Jill is mean and nasty and deserves what she gets BUT that was a very low blow. Even for Ramona.

    [Reply]

  2. aa

    I have been surfing online more than 2 hours today, yet

    I never found any interesting article like yours. It’s pretty worth enough

    for me. In my view, if all site owners and bloggers made good content as you
    did, the net

    will be a lot more useful than ever before.|
    I could not resist commenting. Very well written!|
    I will immediately clutch

    your rss feed as I can’t find your email subscription link or newsletter service.
    Do you

    have any? Kindly let me know in order that I may subscribe.

    Thanks.|
    It’s the best time to make some plans for the future

    and it is time to be happy. I have read this post and if I could I

    want to suggest you few interesting things or tips. Perhaps
    you could write next articles referring to

    this article. I want to read even more things about it!|
    It is perfect time to make a few plans for the long
    run and it’s time to be happy. I have

    learn this post and if I may just I wish to counsel you few attention-grabbing issues or suggestions.

    Perhaps you could write next articles

    referring to this article. I desire to learn

    more issues about it!
    It is lovely worth sufficient for

    me. Personally, if all webmasters and bloggers made just right content as you probably did, the web
    will probably be much more useful than ever before.|
    Ahaa, its fastidious discussion

    on the topic of this piece of writing here at this web
    site, I

    have read all that, so at this time me also commenting here.|
    I am sure this post has touched all the internet

    visitors, its really really pleasant

    piece of writing on building up new webpage.|
    Wow, this article is good, my sister is analyzing these

    things, so I am going to let know her.|
    bookmarked!!, I love your

    site!|
    Way cool! Some very valid points! I appreciate you penning

    this post and the rest of the

    website is very good.|
    Hi, I do believe this is a great blog. I stumbledupon it
    😉 I may come

    back yet again since i have saved as a favorite it.
    Money and freedom is the best way to change, may you be
    rich and continue to guide

    others.|
    Woah! I’m really digging the template/theme of this

    blog. It’s simple, yet effective. A lot of times it’s

    challenging to get that “perfect balance” between usability and visual
    appearance. I must say you’ve done a superb job with this.

    Also, the blog loads extremely fast for me on Safari.
    Excellent Blog!|
    These are really fantastic ideas in about blogging.

    You have touched some good points here.

    Any way keep up wrinting.|
    I like what you guys are up too. Such clever work and

    coverage! Keep up the wonderful works guys I’ve added you guys
    to my blogroll.|
    Hey! Someone in my Myspace group

    shared this site with us so I came to look it over. I’m definitely enjoying the
    information. I’m bookmarking and will be tweeting this to my followers!
    Outstanding blog and superb design.|
    Everyone loves what you guys are up too. This type of clever work and

    reporting! Keep up the great works guys I’ve

    included you guys to my personal blogroll.|
    Hey would you mind stating which blog

    platform you’re working with? I’m going to start my own

    blog in the near future but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and

    Drupal. The reason I ask is because your design seems

    different then most blogs and I’m looking for something unique.

    P.S Apologies for getting off-topic but I

    had to ask!|
    Hello would you mind letting me know which

    webhost you’re utilizing? I’ve loaded

    your blog in 3 completely different web browsers and I must
    say this blog loads a lot faster then most. Can you

    recommend a good web hosting provider at a

    reasonable price?

    Thanks, I appreciate it!|
    I love it when individuals get together and share

    views. Great site, continue the good work!|
    Thank you for the good writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you! However, how

    could we communicate?|
    Howdy just wanted to give you a quick heads up. The

    text in your article seem to be running off the screen in

    Opera. I’m not sure if this is a

    formatting issue or something to do with

    browser compatibility but I thought I’d post to let you know.
    The style

    and design look great though! Hope you get the

    problem solved soon. Thanks|
    This is a topic that is near to my heart… Take care! Where are your contact

    details though?|
    It’s very straightforward to find out any

    matter on net as compared to books, as I found this

    piece of writing at this web site.|
    Does your website have a contact page? I’m having a tough time locating it but, I’d
    like to send you an email.

    I’ve got some ideas for your blog you

    might be interested in hearing. Either way, great site and I look

    forward to seeing it improve over time.|
    Hello! I’ve been reading your weblog for a long time now and
    finally got the

    bravery to go ahead and give you a shout out from
    Kingwood Texas! Just

    wanted to mention keep up the good job!|
    Greetings from Florida! I’m

    bored at work so I decided to browse your

    site on my iphone during lunch break. I love the

    info you provide here and can’t wait to take a look

    when I get home. I’m amazed at how fast your blog loaded

    on my cell phone .. I’m not even using WIFI, just 3G ..
    Anyhow, excellent

    blog!|
    Its such as you learn my thoughts! You seem
    to grasp a lot approximately this,

    such as you wrote the book in it or something.
    I feel that you just can do with

    a few percent to drive the message

    home a little bit, but instead of that, that

    is magnificent blog. A

    fantastic read. I will certainly be back.|
    I visited many sites

    however the audio feature for audio songs current at this
    site is actually excellent.|
    Hi there, i read your blog from time to time and i

    own a similar one and i was just curious if you get
    a lot of spam

    responses? If so how do you stop it, any plugin or anything
    you can suggest? I get so much

    lately it’s driving me crazy so any support is very

    much appreciated.|
    Greetings! Very helpful advice within this

    article! It is the little changes that

    produce the

    most significant changes. Thanks a lot for sharing!|
    I absolutely love your blog..

    Great colors & theme. Did you make this site yourself? Please

    reply back as I’m wanting to create my own personal site and would love to find out where you
    got this from or exactly what the theme is called.

    Thank you!|
    Hi there! This article could not be

    written any better! Reading through this post reminds me
    of my previous roommate! He

    constantly kept talking about this. I am

    going to forward this

    post to him. Fairly certain he’s going to have a

    good read. Thanks for sharing!|
    Whoa! This blog looks exactly like my old one! It’s

    on a totally different subject but it has pretty much

    the same page layout and design. Great choice of colors!|
    There’s certainly a lot to know about this issue. I like

    all the points you have made.|
    You’ve made some really good points there. I

    checked on the net to learn more about the

    issue and found most individuals will go along with your views on this web site.|
    Hi there, I log on to your blog

    daily. Your story-telling style is witty, keep doing what you’re doing!|
    I just couldn’t leave your

    website before suggesting that I extremely enjoyed the usual info an individual

    provide on your visitors? Is gonna be

    again

    continuously to check up on new posts|
    I needed to thank you for this very good read!! I definitely

    loved every little bit of it. I have got you

    book marked to check out

    new things you post…|
    What’s up, just wanted to tell you, I enjoyed this article.

    It was inspiring. Keep on posting!|
    Hi there, I enjoy reading through your post.

    I like to write a little comment to support you.|
    I always spent my half an hour to read this web site’s articles all the time along with a mug of coffee.|
    I for all time emailed this webpage post page to all my associates,

    for the reason that if like to read it

    afterward my friends will too.|
    My coder is trying to persuade me to move to .net

    from PHP. I have always disliked the idea because of the costs.
    But he’s

    tryiong none the less. I’ve been using WordPress on several
    websites for about a year and am

    anxious about switching to another platform.
    I have heard

    excellent things about blogengine.net. Is there a

    way I can import all my wordpress posts into it?

    Any help would be really appreciated!|
    Hi! I could have sworn I’ve

    visited this website before but after

    going through some of the

    posts I realized it’s new to me. Anyhow,

    I’m definitely delighted I discovered it and I’ll be bookmarking it and checking

    back frequently!|
    Great article! That is the type of information that should be shared

    around the internet. Shame on the search

    engines for now not positioning this put

    up upper! Come on over and consult with my site . Thanks =)|
    Heya i am for the first time here. I found this board and I find

    It truly useful & it helped me out much. I hope to give something

    back and help others like you helped me.|
    Hi, I do believe your site may be having web browser

    compatibility problems. When I look at your site in Safari, it
    looks fine but when opening in Internet Explorer, it has

    some overlapping issues. I merely wanted to provide you

    with a quick heads up! Aside from

    that, excellent site!|
    Somebody essentially help to

    make critically posts I would state. That is the very first time I frequented your

    website page and up to now? I amazed with the research you made
    to create this

    particular publish incredible. Fantastic process!|
    Heya i am for the primary time here. I found this

    board and I in finding It really useful & it helped

    me out much. I’m hoping to offer something again and help others
    such as

    you aided me.|
    Good day! I just want to offer you a huge thumbs up for

    your great information you have got here on this
    post. I will be returning to

    your website for more soon.|
    I all the time used to read piece of

    writing in news papers but now as I am a user of web so from now I am using net for

    content, thanks to web.|
    Your means of describing the

    whole thing in this post is actually nice, all be capable
    of simply be

    aware of it, Thanks a lot.|
    Hello there, I discovered your web site by means

    of Google even as

    looking for a similar subject,

    your web site got here up, it looks good. I have bookmarked it in my
    google

    bookmarks.
    Hello there, simply turned into alert to your blog through Google, and found

    that it is truly informative. I’m gonna be careful for brussels.
    I will appreciate in the event you

    proceed this in future. Numerous people might be

    benefited out of your writing. Cheers!|
    I am curious to find out what blog platform you have been utilizing?
    I’m having some

    small security problems with my latest blog and I’d like
    to find something more safeguarded. Do you

    have any solutions?|
    I’m really impressed with your writing skills
    as well

    as with the layout on your weblog. Is this a paid theme or
    did you

    customize it yourself? Either way keep up the excellent

    quality writing, it’s rare to see a nice blog like this one nowadays.|
    I’m extremely inspired with your writing talents as well as with the layout in your

    weblog. Is this a paid theme or

    did you modify it your self? Anyway keep

    up the nice high quality writing, it is rare

    to look a great weblog like this one today..|
    Hello, Neat post. There is an issue along with your website in internet explorer,

    might test this? IE still is the marketplace chief and
    a big part of folks will omit your fantastic writing due to this problem.|
    I’m not sure where you are getting your info, but

    great topic. I needs to spend some time learning much more or

    understanding more. Thanks for magnificent

    info I was looking for this info for my mission.|
    Hi, i think that i saw you visited my weblog

    so i came to “return the favor”.I’m trying to find \

    [Reply]

Leave a Reply

Your email address will not be published. Required fields are marked *