_n()对单复数进行翻译

目录

描述

Retrieve the plural or single form based on the amount.

If the domain is not set in the $l10n list, then a comparison will be made and either $plural or $single parameters returned.

If the domain does exist, then the parameters $single, $plural, and $number will first be passed to the 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.

_n() 描述

用法

<?php _n$single$plural$number$domain ?>

_n() 用法

参数

$single

(string) (必填) The text that will be used if $number is 1 (or if the locale uses the singular form for non-1 numbers)

默认值: None

$plural

(string) (必填) The text that will be used if $number is plural

默认值: None

$number

(int) (必填) The number to compare against to use either $single or $plural

默认值: None

$domain

(string) (可选) Domain to retrieve the translated text

默认值: 'default'

_n() 参数

返回值

(string) 

Either $single or $plural translated text.

_n() 返回值

示例

Display either "1 star" or "x stars" for a star rating plugin.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$rating = '3';

$text = sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating );

// "3 stars"
echo $text;

Important: Never do a calculation inside the sprintf() function! The following won't work:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$text = sprintf( 
    _n( '%s star', '%s stars', $rating, 'your_textdomain' ), 
    2 <= $rating ? $rating -1 : $rating
);

Example from /wp-admin/edit-comments.php without the use of a text domain for translation.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

if ( $approved > 0 )
    $messages[] = sprintf( _n( '%s comment approved', '%s comments approved', $approved ), $approved );

_n() 示例

注意

  • 使用到 the 'ngettext' 过滤器.
  • l10n is an abbreviation for localization.

_n() 注意

历史

  • 添加于 版本: 2.8.0

_n() 历史

源文件

_n() 函数的代码位于 wp-includes/l10n.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 );
}

_n() 源文件

相关