_nx ( $single, $plural, $number, $context, $domain = 'default' )
参数
  • (string) $single The text to be used if the number is singular.
    Required:
  • (string) $plural The text to be used if the number is plural.
    Required:
  • (int) $number The number to compare against to use either the singular or plural form.
    Required:
  • (string) $context Context information for the translators.
    Required:
  • (string) $domain Optional. Text domain. Unique identifier for retrieving translated strings. Default 'default'.
    Required:
    Default: 'default'
返回值
  • (string) The translated singular or plural form.
定义位置
相关方法
_n_x_ex_nc_nx_noop
引入
2.8.0
弃用
-

_nx: 此函数用于翻译一个带有单数和复数形式以及上下文的字符串。

根据提供的数字和gettext上下文,翻译和检索单数或复数形式。

这是一个_n()和_x()的混合体。它支持上下文和复数。

当你想根据一个数字是单数还是复数来使用带有上下文的字符串的适当形式时,可以使用。

通过上下文参数消除歧义的通用短语的例子。

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 ) )。

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;
}

常见问题

FAQs
查看更多 >