gettext语法有什么意义?
-
-
感谢大家的精心解答!我真的很感谢这里!Thank all of you for your elaborate answers! I really appreciate this here!
- 0
- 2012-07-09
- Circuit Circus
-
...也许对此还有一个问题:是否可以省略文本域的条目?我挺直地开始将它带到我想翻译的每个字符串中,但随后在Wordpress Codex中认识到一些根本不包含它的示例......Maybe one further question to this: Can the entry of the text-domain be left out? I upright started bring it to every string I wanted to translate but then recognized some examples in the Wordpress codex which don't contain it at all...
- 0
- 2012-07-09
- Circuit Circus
-
...好吧,现在我在此法典页面上遇到了它-对这个多余的问题很抱歉:-)... Okay, now I came across it in this codex page - sorry for this redundant question :-)
- 0
- 2012-07-09
- Circuit Circus
-
3 个回答
- 投票数
-
- 2012-07-07
__
(双下划线)是基本翻译功能.它会翻译一个字符串并将其作为字符串返回._e
与__
相同,但是立即回显结果._x
是上下文转换功能.它还有第二种选择,可以为进行翻译的人员提供上下文._ex
与_x
相同,但是回显结果.使用
_x
的示例:$string = _x( 'Buffalo', 'an animal', 'plugin-domain' ); $string = _x( 'Buffalo', 'a city in New York', 'plugin-domain' ); $string = _x( 'Buffalo', 'a verb meaning to confuse somebody', 'plugin-domain' );
有时相同的字符串在其他语言中可能会有所不同.为翻译人员提供上下文可以帮助他们选择合适的单词.
快捷功能:
-
esc_attr__
:与__
等效,但也通过esc_attr
运行结果. -
esc_html__
:与__
等效,但也通过esc_html
运行结果. -
esc_attr_e
:与_e
等效,但也通过esc_attr
运行结果. -
esc_html_e
:与_e
等效,但也通过esc_html
运行结果. -
esc_attr_x
:与_x
等效,但也通过esc_attr
运行结果. -
esc_html_x
:与_x
等效,但也通过esc_html
运行结果.
_n
是复数处理程序.示例:$string = sprintf( _n( 'You have %d taco.', 'You have %d tacos.', $number, 'plugin-domain'), $number );
在该示例中,有两种方法可以说出炸玉米饼的数量,具体取决于是否为单数.第一次使用$number告诉
_n
函数使用哪个版本. $number的第二次使用发生在sprintf中,用字符串中的实际数字替换%d.没有等效于
_n
的回波函数,但是有一个名为_nx
的函数.它是_n
和_x
的组合.多元化和背景._n_noop
是一个特殊的代码.它用于翻译复数字符串,但实际上并不立即执行翻译.如果您想使字符串集中但实际上在其他地方进行工作,这将很有用.在其他地方实际起作用的函数是translate_nooped_plural
.示例:
$holder = _n_noop('You have %d taco.', 'You have %d tacos.', 'plugin-domain'); // ... later ... $string = sprintf( translate_nooped_plural( $holder, $count ), $count );
这没什么用,但是对组织来说很方便.例如,如果将所有字符串都放在一个文件中,然后在其他地方引用它们,那么仅
_n
不可能实现,您需要类似_n_noop
的工具_nx_noop
与_n_noop
相同,但也可以为翻译人员提供上下文,与_x
相同.请注意,您可以将域放入noop函数调用中,或放入translate_nooped_plural函数调用中.对于您的组织而言,哪种更有意义.如果两个都有域,则传递给noop的域将获胜.
number_format_i18n
等同于PHP内置的number_format ,但是它增加了对小数等内容的处理,这在其他语言环境中是不同的.date_i18n
等同于PHP内置的 date ,以及所有相关的处理方法.月名称,日名称等此外,从不违反法律.只是提醒. :)
__
(double underscore) is the base translate function. It translates a string and returns it as a string._e
does the same as__
, but echo's the result immediately._x
is the contextual translate function. It has a second option to provide context to people doing the translation._ex
is the same as_x
, but echo's the result.Example of using
_x
:$string = _x( 'Buffalo', 'an animal', 'plugin-domain' ); $string = _x( 'Buffalo', 'a city in New York', 'plugin-domain' ); $string = _x( 'Buffalo', 'a verb meaning to confuse somebody', 'plugin-domain' );
Sometimes the same string can be different in other languages. Providing context to the translators can help them pick the right words.
Shortcut functions:
esc_attr__
: Equivalent to__
but also runs the result throughesc_attr
.esc_html__
: Equivalent to__
but also runs the result throughesc_html
.esc_attr_e
: Equivalent to_e
but also runs the result throughesc_attr
.esc_html_e
: Equivalent to_e
but also runs the result throughesc_html
.esc_attr_x
: Equivalent to_x
but also runs the result throughesc_attr
.esc_html_x
: Equivalent to_x
but also runs the result throughesc_html
.
_n
is the pluralization handler. Example:$string = sprintf( _n( 'You have %d taco.', 'You have %d tacos.', $number, 'plugin-domain'), $number );
In that example, there's two ways to say the number of tacos, depending on if it's singular or not. The first use of $number tells the
_n
function which version to use. The second use of $number happens in the sprintf, to replace the %d with the actual number in the string.There is no echo function equivalent for
_n
, but there is a function named_nx
. It's a combination of_n
and_x
. Pluralization and context._n_noop
is a special one. It's used for translating pluralized strings, but not actually performing the translation immediately. This is useful if you want to make the strings centralized but actually do the work elsewhere. The function that actually does the work elsewhere istranslate_nooped_plural
.Example:
$holder = _n_noop('You have %d taco.', 'You have %d tacos.', 'plugin-domain'); // ... later ... $string = sprintf( translate_nooped_plural( $holder, $count ), $count );
This isn't used much, but can be handy for organization. If you put all your strings in one file, for example, then reference them elsewhere, this wouldn't be possible with just
_n
, you need something like_n_noop
to do that._nx_noop
is the same as_n_noop
, but also can take a context for the translators, same as_x
.Note that you can put the domain into either the noop function call, or into the translate_nooped_plural function call. Whichever makes more sense for your organization. If both have a domain, then the one passed to the noop call wins.
number_format_i18n
is the equivalent to PHP's built-in number_format, but it adds in the handling for things like decimals and so on, which are different in other locales.date_i18n
is the equivalent to PHP's built-in date, with all the pertinent handling there as well. Month names, day names, etc.Also, never break the laws. Just a reminder. :)
-
哇,这真的是一个很好的解释!非常感谢您的帮助!现在我明白了.Wow, this is really a good explanation of it! Thank you very much for helping! Now I see clear.
- 0
- 2012-07-09
- Circuit Circus
-
- 2012-07-07
__(),_e()和_x(),_ex()
__()
和_e()
本质上都是translate()
(请勿直接使用)并且几乎相同.区别在于
__()
返回翻译后的字符串,并且_e()
会回显它.两者都需要输入一个字符串作为必需参数,并且通常(尽管是可选的)也输入一个文本域.类似地,有
_x()
和_ex()
,可让您指定可以描述字符串出现位置的上下文.如果您的项目包含数十个可翻译字符串,那么使用上下文很有意义.此外,请注意
_n()
和< a href=" http://codex.wordpress.org/Function_Reference/_nx" rel="nofollow">_nx()
用于复数.常见用法示例
$output = '<label for="some_field">' . _x( 'Some Information.', 'Some Form Field', 'your-text-domain' ) . '</label>' . '<input type="text" name="some_field" value="" />' . '<p class="description">' . _x( 'Here you can enter some info.', 'Some Form Field', 'your-text-domain' ) . '</p>'; return $output;
参数
__( $text, $domain ) _e( $text, $domain ) _x( $text, $context, $domain ) _ex( $text, $context, $domain ) _n( $single, $plural, $number $domain ) _nx( $single, $plural, $number, $context, $domain )
除
$number
以外的所有参数均为字符串. 除了$domain
外,所有其他项都是必需的.变量和sprintf()具有更高的灵活性
如果您的字符串包含可变数字或单词,请使用
sprintf()
:$stars = get_post_meta( $post->ID, 'rating', true ); $title = get_the_title( $post->ID ); $output = '<p>' . sprintf( _x( 'The movie titled %2$s received a %1$d star rating.', 'Movie Description', 'your-text-domain' ), $stars, $title ) . '</p>'; return $output;
其他资源
有关即将推出的WordPress I18n Ninja的一些其他资源:
__(), _e() and _x(), _ex()
__()
and_e()
are essentially both a wrapper oftranslate()
(do not use directly) and almost the same.The difference lies in that
__()
returns the translated string and_e()
echoes it. Both need to be fed a string as a required parameter and usually, though optional, also a textdomain.Analogously, there's
_x()
and_ex()
, which let you specify a context that can describe where the string appears. If your project includes more than a few tens of translatable strings, using context makes a lot of sense.Also, note the existence of
_n()
and_nx()
for plurals.Example of common usage
$output = '<label for="some_field">' . _x( 'Some Information.', 'Some Form Field', 'your-text-domain' ) . '</label>' . '<input type="text" name="some_field" value="" />' . '<p class="description">' . _x( 'Here you can enter some info.', 'Some Form Field', 'your-text-domain' ) . '</p>'; return $output;
Parameters
__( $text, $domain ) _e( $text, $domain ) _x( $text, $context, $domain ) _ex( $text, $context, $domain ) _n( $single, $plural, $number $domain ) _nx( $single, $plural, $number, $context, $domain )
All parameters but
$number
are strings. All but$domain
are required.Further flexibility with variables and sprintf()
If your strings will contain variable numbers or words, use
sprintf()
:$stars = get_post_meta( $post->ID, 'rating', true ); $title = get_the_title( $post->ID ); $output = '<p>' . sprintf( _x( 'The movie titled %2$s received a %1$d star rating.', 'Movie Description', 'your-text-domain' ), $stars, $title ) . '</p>'; return $output;
Additional resources
Some additional resources for the upcoming WordPress I18n Ninja:
-
另请参阅[法典中"杂项"下的"本地化"](http://codex.wordpress.org/Function_Reference/#Miscellaneous_Functions),以获取功能的完整细分和详细说明.Also check out ["Localization" under "Miscellaneous" in the codex](http://codex.wordpress.org/Function_Reference/#Miscellaneous_Functions) for a complete breakdown of functions & detailed explanations.
- 0
- 2012-07-07
- TheDeadMedic
-
这就是[wp-polyglots相遇的地方](http://wppolyglots.wordpress.com/).& this is the place [where wp-polyglots meet](http://wppolyglots.wordpress.com/).
- 0
- 2012-07-07
- brasofilo
-
@Johannes Pille:在我发布我的帖子之前,我没有注意到您的回答,我应该删除我的帖子吗?@Johannes Pille: I didn't notice your answer until I'd posted mine, should I delete my post?
- 0
- 2012-07-07
- Jeremy Jared
-
@JeremyJared不,为什么?进一步的信息不是一件坏事,对吗?我认为您的答案经过深思熟虑.向我+1.@JeremyJared No, why? Further info can't be a bad thing, can it?! I think your answer is well thought through. +1 from me.
- 0
- 2012-07-07
- Johannes Pille
-
@TheDeadMedic编辑为"其他资源".@TheDeadMedic Edited into "additional resources".
- 0
- 2012-07-07
- Johannes Pille
-
- 2012-07-07
我不是翻译专家,但是WordPress Codex页面上有很好的文档,并说明了使用每个实例的原因.
从法典页面上:
__()
在消息作为参数传递给另一个函数时使用;
_e()
用于将消息直接写到页面.有关这两个功能的更多信息:__('message')
在本地化模块中搜索"消息"的翻译,并将翻译传递给PHP return语句.如果找不到"message"的翻译,则仅返回"message".
_e('message')
在本地化模块中搜索"消息"的翻译,并将翻译传递给PHPecho语句.如果找不到"消息"的翻译,则仅回显"消息".
请注意,如果您要国际化主题或插件,则应使用
"Text Domain"
.gettext框架可处理大多数WordPress.但是,WordPress发行版中有一些地方无法使用gettext:
- 主要的WordPress README文件-它是静态HTML文件,而不是PHP文件,因此无法通过gettext函数运行.
- 在加载gettext之前的WordPress加载周期的早期,会生成一些错误消息.
希望能回答您的问题,如果不能让我们知道,也许有人可以提供帮助,或者我可以做更多研究.
I'm not an expert on translations, but the WordPress Codex Page has good documentation and explains the reason to use each instance.
From the codex pages:
__()
Is used when the message is passed as an argument to another function;
_e()
is used to write the message directly to the page. More detail on these two functions:__('message')
Searches the localization module for the translation of 'message', and passes the translation to the PHP return statement. If no translation is found for 'message', it just returns 'message'.
_e('message')
Searches the localization module for the translation of 'message', and passes the translation to the PHP echo statement. If no translation is found for 'message', it just echoes 'message'.
Note that if you are internationalizing a Theme or Plugin, you should use a
"Text Domain"
.The gettext framework takes care of most of WordPress. However, there are a few places in the WordPress distribution where gettext cannot be used:
- The main WordPress README file -- it's a static HTML file, not a PHP file, so it cannot be run through the gettext functions.
- A few error messages are generated very early in the WordPress loading cycle, before gettext is loaded.
Additional info regarding when gettext doesn't work
Hopefully that answers your question, if not let us know and maybe someone else can help or I can do some more research.
到目前为止,我已经在Wordpress中处理了一些翻译,并试图阅读正式的gettext文档,但并没有获得可能是一件简单的事情的意思:__(,_e(等)等开头之间有什么区别??甚至更多:旁边还有哪些人?预先感谢!
坦率