WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/ n

Description

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.

Usage

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

Parameters

$single
(string) (required) The text that will be used if $number is 1 (or if the locale uses the singular form for non-1 numbers)
Default: None
$plural
(string) (required) The text that will be used if $number is plural
Default: None
$number
(int) (required) The number to compare against to use either $single or $plural
Default: None
$domain
(string) (optional) Domain to retrieve the translated text
Default: 'default'

Return Values

(string) 
Either $single or $plural translated text.

Examples

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

$rating = '3';

$text = sprintf( _n( '%s star', '%s stars', $rating, 'your-text-domain' ), $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, 'your-text-domain' ), 
    2 <= $rating ? $rating -1 : $rating
);

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

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

Notes

  • Uses the 'ngettext' filter.
  • l10n is an abbreviation for localization.

Change Log

Source File

_n() is located in wp-includes/l10n.php.

Related

L10n: translate(), __(), _e(), _n(), _x(), _ex(), _nx(), esc_attr__(), esc_attr_e(), esc_attr_x(), esc_html__(), esc_html_e(), esc_html_x(), _n_noop(), _nx_noop(), translate_nooped_plural()

See also index of Function Reference and index of Template Tags.