_n_noop()WP 2.5.0

Prepares a translation string considering plural forms, but without immediate translation.

This is needed when the translation is not performed immediately but later, for example in the admin area. When you want to preserve the structure of the translation string where there are plural forms and use it later when the int will be known.

Example:

$message = _n_noop( '%s post', '%s posts', 'text-domain' );
// $message contains an array of translation string data

// translate later
$message = sprintf(
	translate_nooped_plural( $message, $count, 'text-domain' ),
	number_format_i18n( $count )
);

This function is always used in conjunction with translate_nooped_plural().

Use _nx_noop() when you need to add context to the translation.

1 time — 0.0000069 sec (speed of light) | 50000 times — 0.01 sec (speed of light)

No Hooks.

Returns

Array. An array of information about the translation string.

Usage

_n_noop( $singular, $plural, $domain );
$singular(string) (required)
The singular form for localization.
$plural(string) (required)
The plural form for localization.
$domain(string)
Text domain. A unique identifier for retrieving translated strings.
Default: null

Examples

2

#1 Using _n_noop() in register_post_status()

_n_noop() in WP is most often used in the parameters of the function register_post_status() to set plural translation templates when registering a status, and then when using the strings in the admin (when the number is known), WP creates a string based on the specified template.

register_post_status( 'featured', [
	'label'       => 'Featured',
	'label_count' => _n_noop( 'Featured <span class="count">(%s)</span>', 'Featured <span class="count">(%s)</span>' ),
	'public'      => true,
] );
0

#2 Output of the time interval considering declension

A short example demonstrates how to use _n_noop() + translate_nooped_plural() to select the correct form of the word and return a string interval (“N minutes”, “N hours”, etc.) based on the number of seconds.

/**
 * Very simple demo: converting seconds into “N minutes”, “N hours”, etc.
 */
function demo_interval( $seconds ) {
	$chunks = [
		YEAR_IN_SECONDS   => _n_noop( '%s year',   '%s years'   ),
		MONTH_IN_SECONDS  => _n_noop( '%s month',  '%s months'  ),
		WEEK_IN_SECONDS   => _n_noop( '%s week',   '%s weeks'   ),
		DAY_IN_SECONDS    => _n_noop( '%s day',    '%s days'    ),
		HOUR_IN_SECONDS   => _n_noop( '%s hour',   '%s hours'   ),
		MINUTE_IN_SECONDS => _n_noop( '%s minute', '%s minutes' ),
		1                 => _n_noop( '%s second', '%s seconds' ),
	];

	foreach ( $chunks as $size => $nooped ) {
		if ( $seconds < $size ) {
			continue;
		}

		$count = floor( $seconds / $size );

		return sprintf( translate_nooped_plural( $nooped, $count ), $count );
	}

	return __( 'now' );
}

Changelog

Since 2.5.0 Introduced.

_n_noop() code WP 6.9.1

function _n_noop( $singular, $plural, $domain = null ) {
	return array(
		0          => $singular,
		1          => $plural,
		'singular' => $singular,
		'plural'   => $plural,
		'context'  => null,
		'domain'   => $domain,
	);
}