WordPress at a glance

_nx() WP 2.8.0

Retrieves the translation of singular or plural form based on the supplied number, with given context.

This is a hybrid of _n() and _x(). It supports context and plurals.

Used when you want to use the appropriate form of a string with context based on whether a number is singular or plural.

Read more about the translation of multiple forms of numbers in the description of _n().

✈ 1 time = 0.000041s = very fast | 50000 times = 0.12s = very fast PHP 7.1.2, WP 4.7.3

No Hooks.

Return

String. The translated singular or plural form.

Usage

_nx( $single, $plural, $number, $context, $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.
$context(string) (required)
Context information for the translators.
$domain(string)
Text domain. Unique identifier for retrieving translated strings.
Default: 'default'

Examples

#1 Example of a generic phrase which is disambiguated via the context parameter

$people  = 5;
$animals = 7;
printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), $people ) );
printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), $animals ) );

Code of _nx: wp-includes/l10n.php VER 5.0.1

<?php
function _nx($single, $plural, $number, $context, $domain = 'default') {
	$translations = get_translations_for_domain( $domain );
	$translation  = $translations->translate_plural( $single, $plural, $number, $context );

	/**
	 * Filters the singular or plural form of a string with gettext context.
	 *
	 * @since 2.8.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 string $number      The number to compare against to use either the singular or plural form.
	 * @param string $context     Context information for the translators.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
}

Related Functions

From tag: localize (l10n i18n Internationalization localization)

More from category: Localization

No comments
    Hello, !     Log In . Register