_nx_noop()
云策文档标注
概述
_nx_noop() 函数用于在 POT 文件中注册带有上下文信息的复数可翻译字符串,但不进行实际翻译。它适用于需要根据数量动态处理复数形式的场景,例如在后期结合 translate_nooped_plural() 函数使用。
关键要点
- 注册复数字符串:将单数和复数形式及上下文信息存储在数组中,供后续翻译使用。
- 参数说明:接受 $singular(单数形式)、$plural(复数形式)、$context(上下文)和可选的 $domain(文本域)。
- 返回值:返回一个包含翻译信息的数组,包括原始字符串和上下文。
- 使用场景:常用于需要根据动态数量(如文章或评论数量)显示不同复数形式的国际化字符串。
代码示例
$messages = array(
'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
);
$message = $messages[ $type ];
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );注意事项
- 此函数本身不执行翻译,仅注册字符串;实际翻译需配合 translate_nooped_plural() 函数。
- 确保提供正确的上下文参数以消除歧义,特别是在类似短语有不同含义时。
- 从 WordPress 2.8.0 版本开始引入,兼容性良好。
原文内容
Registers plural strings with gettext context in POT file, but does not translate them.
Description
Used when you want to keep structures with translatable plural strings and use them later when the number is known.
Example of a generic phrase which is disambiguated via the context parameter:
$messages = array(
'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
);
...
$message = $messages[ $type ];
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
Parameters
$singularstringrequired-
Singular form to be localized.
$pluralstringrequired-
Plural form to be localized.
$contextstringrequired-
Context information for the translators.
$domainstringoptional-
Text domain. Unique identifier for retrieving translated strings.
Default:
null
Source
function _nx_noop( $singular, $plural, $context, $domain = null ) {
return array(
0 => $singular,
1 => $plural,
2 => $context,
'singular' => $singular,
'plural' => $plural,
'context' => $context,
'domain' => $domain,
);
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Codex
$labels = array( 'draft' => array( _nx_noop( '%s Draft', '%s Drafts', 'post' ), _nx_noop( '%s Draft', '%s Drafts', 'page' ) ), 'publish' => array( _nx_noop( '%s Published', '%s Published', 'post' ), _nx_noop( '%s Published', '%s Published', 'page' ) ), ); if ( $post_type == 'page' ) { $labels = $labels[ $post_status ][1]; } else { $labels = $labels[ $post_status ][0]; } $usable_text = sprintf( translate_nooped_plural( $labels, $count, $domain ), $count );