ngettextfilter-hookWP 2.2.0

Allows you to change a string translated by _n().

Such a string is used for labels that depend on a preceding number, for example: 1 peach, 2 peaches, 10 peaches.

This is a filter for _n().

Usage

add_filter( 'ngettext', 'wp_kama_ngettext_filter', 10, 5 );

/**
 * Function for `ngettext` filter-hook.
 * 
 * @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.
 *
 * @return string
 */
function wp_kama_ngettext_filter( $translation, $single, $plural, $number, $domain ){

	// filter...
	return $translation;
}
$translation(string)
The translated text.
$single(string)
The original singular text.
$plural(string)
The original plural text.
$number(integer)
The integer used to select the string for translation.
$domain(string)
The translation identifier specified in translation functions. It determines which .mo translation file is loaded.

Examples

#1 Replace the word items with elements in the WP_List_Table pagination block in the admin area

Translating the items string in the admin table pagination block
## Replace `items` name in wp_list_table pagination box
add_filter( 'ngettext', 'change_wp_list_table_pagination_item_name', 10, 5 );
function change_wp_list_table_pagination_item_name( $translation, $single, $plural, $number, $domain ) {

	if ( $domain === 'default' && in_array( $translation, ['%s item', '%s items'] ) ) {
		return str_replace( '%s item', '%s element', $translation );
	}

	return $translation;
}

Changelog

Since 2.2.0 Introduced.

Where the hook is called

_n()
ngettext
wp-includes/l10n.php 498
$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );

Where the hook is used in WordPress

Usage not found.