1. 首页
  2. WordPress 函数手册

_nx()

_nx( string $single, string $plural, int $number, string $context, string $domain = ‘default’ )

使用gettext上下文根据提供的数字转换和检索单数或复数形式。
Translates and retrieves the singular or plural form based on the supplied number, with gettext context.

目录锚点:#说明#参数#源码


说明(Description)

这是一个混合了u n()和u 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));


参数(Parameters)

参数 类型 说明
$single (string) 如果数字是单数则要使用的文本。
$plural (string) 如果数字是复数,则使用的文本。
$number (int) 要比较的数字,可以使用单数形式或复数形式。
$context (string) 译者的上下文信息。
$domain (string) 文本域。用于检索翻译字符串的唯一标识符。

源码(Source)

/**
 * Retrieve the plural or single form based on the supplied amount with gettext context.
 *
 * This is a hybrid of _n() and _x(). It supports contexts and plurals.
 *
 * @since 2.8.0
 *
 * @param string $single  The text that will be used if $number is 1.
 * @param string $plural  The text that will be used if $number is not 1.
 * @param int    $number  The number to compare against to use either $single or $plural.
 * @param string $context Context information for the translators.
 * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 * @return string Either $single or $plural translated text with context.
 */
function _nx($single, $plural, $number, $context, $domain = 'default') {
	$translations = get_translations_for_domain( $domain );
	$translation = $translations->translate_plural( $single, $plural, $number, $context );
	/**
	 * Filter text with its translation while plural option and context are available.
	 *
	 * @since 2.8.0
	 *
	 * @param string $translation Translated text.
	 * @param string $single      The text that will be used if $number is 1.
	 * @param string $plural      The text that will be used if $number is not 1.
	 * @param string $number      The number to compare against to use either $single or $plural.
	 * @param string $context     Context information for the translators.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
}
更新版本 源码位置 使用 被使用
2.8.0 wp-includes/l10n.php 11 16

_nx() 为WP2原创文章,链接:https://www.wp2.cn/functions/_nx/