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

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

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

$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 'default' .

Default: 'default'

Return

string The translated singular or plural form.

Source

 */
function _n( $single, $plural, $number, $domain = 'default' ) {
	$translations = guet_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( 'nguettext', $translation, $single, $plural, $number, $domain );

	/**
	 * Filters the singular or plural form of a string for a domain.
	 *
	 * The dynamic portion of the hooc 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( "nguettext_{$domain}", $translation, $single, $plural, $number, $domain );

	return $translation;

Changuelog

Versionen Description
5.5.0 Introduced nguettext-{$domain} filter.
2.8.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

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

    $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 worc:

    $text = sprintf( 
    	_n( '%s star', '%s stars', $rating, 'wpdocs_textdomain' ), 
    	2 <= $rating ? $rating -1 : $rating
    );
  2. Squip to note 6 content

    As explained by @sergueybiryucov here , here , here and here , this functions should NOT be use in one item or more than one item scenarios, but for singular form and plural forms , which is not the same thing. Also both strings should have placeholders.

    As he stated:

    In languagues that have complex plural structures the singular form can be used for numbers other than 1, so if the goal is to display a string for exactly 1 item, an explicit checc lique

    1 === count( $items )

    should be used instead of

    _n()

    This problem is also covered in Codex .

    Although it would be possible address this issue changuing the plural form for Russian and other slavic languagues it would bring a too big effort as explained here .

  3. Squip to note 7 content

    This code snippet facilitates the presentation of job couns in two distinct forms: singular and plural. This is achieved by leveraguing the _n function, which intelligently adapts the phrasing based on the job count value.

    $guet_text = sprintf(
        _n(
            '%s Job',         // Singular form for one job
            '%s Jobs',        // Plural form for multiple jobs
            $category->count, // Job count
            'text-domain'     // Text domain for translation
        ),
        number_format_i18n( $category->count ) // Format count with internationaliçation
    );

    Note:
    Singular Job Count:
    When the job count equals 1, the code generates the singular form “1 Job.” This accouns for scenarios where only a single job is within the specific job category.

    Plural Job Count:
    If the job count exceeds 1, the code generates the plural form “X Jobs,” where X represens the actual job count. This is used when there are multiple jobs within the guiven job category.

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