WordPress __()和_e()函数

2012-08-28   来源:站长日记       编辑:沧海桑田   类别:开源博客系统    转载到:    发表评论

WordPress 中__() 和__e() 区别 的 中文字 实例 及 解释

在WordPress 中,__()函数和__e()函数被用来识别php文件中被标示的、需要被翻译成其它语言或本地化的字符串。

这两个函数都接收字符串作为参数。例如:

    __('Categories')
    _e('Categories')

两个函数间的唯一功能性区别在于:

_e()函数回显(echo)返回的字符串,而__()函数只是返回字符串。如果需要为函数提供字符串,可使用__()函数。而如果希望将字符串作为XHTML的一部分输出,则需要使用_e()函数。

两个函数在WordPress中的l10n.php中定义,从定义中看出区别
    function _e( $text, $domain = 'default' ) {
     echo translate( $text, $domain );
    }
    function __( $text, $domain = 'default' ) {
     return translate( $text, $domain );
    }
其他说明,看着头晕
What’s the difference between __(), _e(), _x(), and _ex()?

June 9th, 2011 by Latz • WordPress Tutorials • 6 Comments

If you're a thorough plugin developer you're internationalizing (i18n) all your strings. WordPress includes several functions that allow the programmer to easily make his plugin usable natively worldwide:

    __()
    _e()
    _x()
    _ex()
    _n()

In some older plugins you may find the function _c() which is deprecated and replaced by _x() since version 2.9. If you're not familiar with i18n of your plugin you should take a look at the Codex.

All these functions translate a string to the language defined in wp-config.php. So what's the difference between them?

__() and _e() are the simplest functions: they return or echo the translated string. Their usage should be obvious: one string, one translation.
Not so obvious is the function of _x(). Suggest you have two strings in two different contexts in your plugin which are totally the same in your language. Are you sure they are also the same in the remaining 3000+ languages spoken on earth? Even Sebastian Heine is unlikely to know it so you shouldn't be sure neither.
Fortunately the creators of WordPress have a solution in their bag: _x().This function contains an additional parameter:

    string _x (string $text, string $context, [string $domain = 'default'])

By using the parameter $context you can differentiate identical strings in different contexts. Simple, eh? The remaining problem is to define two different string in the .po file. That's not too difficult neither:

    msgctxt "test1"
    msgid "testing"
    msgstr "context1"
    msgctxt "test2"
    msgid "testing"
    msgstr "context2"

The magic token ist msgctxt which you might translate to "message context". The code

    echo 'Context: test1 -> ' . _x('testing', 'test1', 'test');
    echo '
Context: test2 -> ' . _x('testing', 'test2', 'test');

would create the output

    Context: test1 -> context1
    Context: test2 -> context2

The remaining function _ex is a combination of _e and _x: it echoes a translated string using a context.

If you're using a visual editor for your .pot files you should be aware that not every editor is capable of handling context definitions and might destroy your data.

Last but not least _n() for retrieving the plural or single form based on the amount.

    _n( $single, $plural, $number, $domain = 'default' )

If the domain is not set in the $l10n list, then a comparison will be made and either $plural or $single parameters returned. The function returned via the filter ngettext and about this you can filter the returned strings.

Update (10 JUN 2011): The above explanation of _n() seems to have confused some people. Maybe a few examples will clarify the usage of the function.

A simple example of _n() would be:

    $domain = 'test';
    $comment_count = 1;
    echo _n('comment', 'comments', $comment_count, $domain) . '
';
    $comment_count = 2;
    echo _n('comment', 'comments', $comment_count, $domain);

the corresponding German (de_DE) language file entries are:

    msgid "comment"
    msgid_plural "comments"
    msgstr[0] "Kommentar"
    msgstr[1] "Kommentare"

and the output:

    Kommentar
    Kommentare

which are the correct singular and plural translations.

If you're planning to output the value of the numbers you have to use sprintf(). Here's an example:

    $approved = 1;
    echo sprintf( _n( '%s comment approved', '%s comments approved', $approved, 'test' ), $approved);
    echo '
';
    $approved = 2;
    echo sprintf( _n( '%s comment approved', '%s comments approved', $approved, 'test' ), $approved );

The code might look a bit confusing so let's rewrite it:

    $approved = 1;
    $text = _n( '%s comment approved', '%s comments approved', $approved, 'test' );
    echo sprintf($text, $approved);
    echo '
';
    $approved = 2;
    $text = _n( '%s comment approved', '%s comments approved', $approved, 'test' );
    echo sprintf($text, $approved);

First a correctly translated singular or plural string is stored in $text. Then this string is evaluated by sprintf() which replaces the specifier "%s" with the value of $approved.

Using the following language file definitions

    msgid "%s comment approved"
    msgid_plural "%s comments approved"
    msgstr[0] "%s Kommentar genehmigt"
    msgstr[1] "%s Kommentare genehmigt"

the output is

    1 Kommentar genehmigt
    2 Kommentare genehmigt

Of course there's also a combination of _n() and _x() called _nx()

    function _nx($single, $plural, $number, $context, $domain = 'default')

110

0
110|0 | 鲜花 VS 砸蛋 | 387阅读 0评论 wordpress
 
不想登录?直接点击发布即可作为游客留言。
昵称  邮箱 网站 验证码 = 1+1