_n()WP 2.8.0

Translates and retrieves the singular or plural form based on the supplied number (1 comment, 2 comments).

Used when you want to use the appropriate form of a string based on whether a number is singular or plural.

We need to pass translation strings and number to the function, then:

  • If a single number (1) is passed, the function returns the translated value of the first parameter: $single.

  • If a plural is passed (2 3 4), the function returns the translated value of the second parameter: $plural.

  • If it is not possible to get the translation of the desired line from the translation file, the function will return the original value of the line.

If the transfer ID ($domain) is specified. The function will pass the $single, $plural, and $number parameters to the Translations::translate_plural() method. Then the result and all parameters will be passed to the ngettext filter.

To translate a string to singular or plural form based on the supplied number with context, use the similar function _nx().

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

Hooks from the function

Return

String. The translated or original string specified in the $single or $plural parameter, depending on the passed $number parameter.

Usage

_n( $single, $plural, $number, $domain );
$single(string) (required)
The text to be used if the number is singular.
$plural(string) (required)
The text to be used if the number is plural.
$number(int) (required)
The number to compare against to use either the singular or plural form.
$domain(string)
Text domain. Unique identifier for retrieving translated strings.
Default: 'default'

Examples

0

#1 Easy to use, for clarity [auto-translate]

echo _n( '%s star', '%s stars', 1 );
// print: %s star

echo _n( '%s star', '%s stars', 3 );
// outputs: %s stars
0

#2 With a translation file [auto-translate]

Next, it is assumed that there is a translation file, it is registered with
load_plugin_textdomain() and its identifier 'my_textdomain'.

Also in this translation file are translations for plural numbers, then:

echo _n( '%s star', '%s stars', 1, 'textdomain' );
// Returns: %s star

echo _n( '%s star', '%s stars', 4, 'textdomain' );
// Returns: %s stars

echo _n( '%s star', '%s stars', 6, 'textdomain' );
// Returns: %s stars
0

#3 Replace %s with the desired (int). [auto-translate]

To do this, use the PHP function sprintf(). This is done in all themes:

$rating = 1;
echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating );
// Returns: 1 star

$rating = 4;
echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating );
// Returns: 4 stars

$rating = 6;
echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating );
// Returns: 6 stars
0

#4 Example from /wp-admin/edit-comments.php [auto-translate]

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

#5 Translating the phrase about the number of items in WooCommerce [auto-translate]

// Get the number of items in the cart, let it be 5
$count = WC()->cart->get_cart_contents_count();

//Translate to get "%s, %d goods"
$translation = _n( '%s, has %d product', '%s, has %d products', $count, 'woocommerce' );

// cut out the comma, if it is not needed, we get "%s %d goods"
$translation = str_replace( ',', '', $translation );

// Substitute data instead of placeholders to get "Only 5 products".
$translation = sprintf( $translation, 'Total', $count )

// Clean up and display
echo wp_kses_data( $translation );
0

#6 Never perform calculations inside the sprintf() function! [auto-translate]

The following will not work:

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

Right:

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

Changelog

Since 2.8.0 Introduced.
Since 5.5.0 Introduced ngettext-{$domain} filter.

_n() code WP 6.4.3

function _n( $single, $plural, $number, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translation  = $translations->translate_plural( $single, $plural, $number );

	/**
	 * Filters the singular or plural form of a string.
	 *
	 * @since 2.2.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 $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );

	/**
	 * Filters the singular or plural form of a string 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 $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( "ngettext_{$domain}", $translation, $single, $plural, $number, $domain );

	return $translation;
}