1. 首页
  2. WordPress 函数手册

_n()

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

根据提供的数字翻译和检索单数或复数形式。
Translates and retrieves the singular or plural form based on the supplied number.

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


说明(Description)

当您希望根据数字是单数还是复数来使用字符串的适当形式时使用。示例:printf(’%s person’,’%s people’,$count,’text domain’),number_format_i18n($count));


参数(Parameters)

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

源码(Source)

/**
 * Retrieve the plural or single form based on the supplied amount.
 *
 * If the text domain is not set in the $l10n list, then a comparison will be made
 * and either $plural or $single parameters returned.
 *
 * If the text domain does exist, then the parameters $single, $plural, and $number
 * will first be passed to the text domain's ngettext method. Then it will be passed
 * to the 'ngettext' filter hook along with the same parameters. The expected
 * type will be a string.
 *
 * @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 $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 * @return string Either $single or $plural translated text.
 */
function _n( $single, $plural, $number, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translation = $translations->translate_plural( $single, $plural, $number );
	/**
	 * Filter text with its translation when plural option is available.
	 *
	 * @since 2.2.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 $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
}
更新版本 源码位置 使用 被使用
2.8.0 wp-includes/l10n.php 7 5

笔记(Notes)

为星级插件显示“1星”或“x星”。

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