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

Translates and retrieves the singular or plural form based on the supplied number.


Description Description

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

Example:

printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );

Parameters Parameters

$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) (Optional) Text domain. Unique identifier for retrieving translated strings.

Default value: 'default'


Top ↑

Return Return

(string) The translated singular or plural form.


Top ↑

Source Source

File: wp-includes/l10n.php

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 string $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.
	 */
	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

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

    $rating = '3';
    
    $text = sprintf( _n( '%s star', '%s stars', $rating, 'wpdocs_textdomain' ), $rating );
    
    // "3 stars"
    echo $text;
    

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

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

You must log in before being able to contribute a note or feedback.