_nx()
云策文档标注
概述
_nx() 是 WordPress 中一个结合了 _n() 和 _x() 功能的翻译函数,用于根据提供的数字返回单数或复数形式的字符串,并支持 gettext 上下文。它适用于需要根据数量选择适当字符串形式且需上下文消歧的场景。
关键要点
- 函数功能:基于数字返回单数或复数形式的翻译字符串,包含上下文信息。
- 参数说明:$single(单数字符串)、$plural(复数字符串)、$number(数字)、$context(上下文)、$domain(文本域,可选,默认 'default')。
- 返回值:返回翻译后的单数或复数形式字符串。
- 相关 Hook:包括 apply_filters('ngettext_with_context', ...) 和 apply_filters("ngettext_with_context_{$domain}", ...),用于过滤翻译结果。
- 内部实现:调用 get_translations_for_domain() 和 Translations::translate_plural() 进行翻译处理。
代码示例
printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );注意事项
- 上下文参数 $context 用于为翻译者提供消歧信息,确保在不同场景下正确翻译。
- 文本域 $domain 默认为 'default',可根据需要指定以从特定翻译文件中检索字符串。
- 函数自 WordPress 2.8.0 引入,并在 5.5.0 版本增加了针对特定文本域的过滤器。
原文内容
Translates and retrieves the singular or plural form based on the supplied number, with gettext context.
Description
This is a hybrid of _n() and _x() . It supports context and plurals.
Used when you want to use the appropriate form of a string with context based on whether a number is singular or plural.
Example of a generic phrase which is disambiguated via the context parameter:
printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );
Parameters
$singlestringrequired-
The text to be used if the number is singular.
$pluralstringrequired-
The text to be used if the number is plural.
$numberintrequired-
The number to compare against to use either the singular or plural form.
$contextstringrequired-
Context information for the translators.
$domainstringoptional-
Text domain. Unique identifier for retrieving translated strings.
Default'default'.
Source
function _nx( $single, $plural, $number, $context, $domain = 'default' ) {
$translations = get_translations_for_domain( $domain );
$translation = $translations->translate_plural( $single, $plural, $number, $context );
/**
* Filters the singular or plural form of a string with gettext context.
*
* @since 2.8.0
*
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
$translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
/**
* Filters the singular or plural form of a string with gettext context for a domain.
*
* The dynamic portion of the hook name, `$domain`, refers to the text domain.
*
* @since 5.5.0
*
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
$translation = apply_filters( "ngettext_with_context_{$domain}", $translation, $single, $plural, $number, $context, $domain );
return $translation;
}
Hooks
- apply_filters( ‘ngettext_with_context’, string $translation, string $single, string $plural, int $number, string $context, string $domain )
-
Filters the singular or plural form of a string with gettext context.
- apply_filters( “ngettext_with_context_{$domain}”, string $translation, string $single, string $plural, int $number, string $context, string $domain )
-
Filters the singular or plural form of a string with gettext context for a domain.