_n( string $single, string $plural, int $number, string $domain = 'default' ): string
Translates and retrieves the singular or plural form based on the supplied number.
Contents
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
File: wp-includes/l10n.php
.
View all references
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;
}
Hooks
-
apply_filters( 'ngettext',
string $translation ,string $single ,string $plural ,int $number ,string $domain ) -
Filters the singular or plural form of a string.
-
apply_filters( "ngettext_{$domain}",
string $translation ,string $single ,string $plural ,int $number ,string $domain ) -
Filters the singular or plural form of a string for a domain.
Changelog
Version | Description |
---|---|
5.5.0 | Introduced ngettext-{$domain} filter. |
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Display either “1 star” or “x stars” for a star rating plugin.
Important: Never do a calculation inside the
sprintf()
function! The following won’t work:As explained by @sergeybiryukov 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:
This problem is also covered in Codex.
Although it would be possible address this issue changing the plural form for Russian and other slavic languages it would bring a too big effort as explained here.
Example from /wp-admin/edit-comments.php without the use of a text domain for translation.