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.
<?php _n( $single, $plural, $number, $domain ) ?>
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 );
_n() is located in wp-includes/l10n.php
.
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()