_n()
Translates and retrieves the singular or plural form based on the supplied number (1 comment, 2 comments).
Used when you want to use the appropriate form of a string based on whether a number is singular or plural.
We need to pass translation strings and number to the function, then:
-
If a single number (1) is passed, the function returns the translated value of the first parameter: $single.
-
If a plural is passed (2 3 4), the function returns the translated value of the second parameter: $plural.
- If it is not possible to get the translation of the desired line from the translation file, the function will return the original value of the line.
If the transfer ID ($domain) is specified. The function will pass the $single, $plural, and $number parameters to the Translations::translate_plural() method. Then the result and all parameters will be passed to the ngettext
filter.
To translate a string to singular or plural form based on the supplied number with context, use the similar function _nx().
Hooks from the function
Return
String
. The translated or original string specified in the $single or $plural parameter, depending on the passed $number parameter.
Usage
_n( $single, $plural, $number, $domain );
- $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)
- Text domain. Unique identifier for retrieving translated strings.
Default: 'default'
Examples
#1 Easy to use, for clarity [auto-translate]
echo _n( '%s star', '%s stars', 1 ); // print: %s star echo _n( '%s star', '%s stars', 3 ); // outputs: %s stars
#2 Replace %s
with the desired (int). [auto-translate]
To do this, use the PHP function sprintf(). This is done in all themes:
$rating = 1; echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating ); // Returns: 1 star $rating = 4; echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating ); // Returns: 4 stars $rating = 6; echo sprintf( _n( '%s star', '%s stars', $rating, 'your_textdomain' ), $rating ); // Returns: 6 stars
#3 Example from /wp-admin/edit-comments.php [auto-translate]
if ( $approved > 0 ) $messages[] = sprintf( _n( '%s comment approved', '%s comments approved', $approved ), $approved );
#4 Never perform calculations inside the sprintf() function! [auto-translate]
The following will not work:
$text = sprintf( _n( '%s star', '%s stars', $rating, 'your-text-domain' ), 2 <= $rating ? $rating -1 : $rating );
Right:
$rating = 2 <= $rating ? $rating - 1 : $rating; $text = sprintf( _n( '%s star', '%s stars', $rating, 'your-text-domain' ), $rating );
#5 With a translation file [auto-translate]
Next, it is assumed that there is a translation file, it is registered with
load_plugin_textdomain() and its identifier 'my_textdomain'.
Also in this translation file are translations for plural numbers, then:
echo _n( '%s star', '%s stars', 1, 'textdomain' ); // Returns: %s star echo _n( '%s star', '%s stars', 4, 'textdomain' ); // Returns: %s stars echo _n( '%s star', '%s stars', 6, 'textdomain' ); // Returns: %s stars
#6 Translating the phrase about the number of items in WooCommerce [auto-translate]
// Get the number of items in the cart, let it be 5 $count = WC()->cart->get_cart_contents_count(); //Translate to get "%s, %d goods" $translation = _n( '%s, has %d product', '%s, has %d products', $count, 'woocommerce' ); // cut out the comma, if it is not needed, we get "%s %d goods" $translation = str_replace( ',', '', $translation ); // Substitute data instead of placeholders to get "Only 5 products". $translation = sprintf( $translation, 'Total', $count ) // Clean up and display echo wp_kses_data( $translation );
Changelog
Since 2.8.0 | Introduced. |
Since 5.5.0 | Introduced ngettext-{$domain} filter. |