_nx()WP 2.8.0

Переводит и возвращает единственную или множественную форму числа на основе указанного контекста. Аналог _n(), только с учетом контекста.

Это объединение функций _n() и _x(). Функция поддерживает числа и контекст.

Функция используется, когда нужно использовать определенную строку перевода в зависимости от переданного числа и все это в указанном контексте перевода.

Подробнее о переводе множественных форм числа читайте в описании _n()

Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.

1 раз — 0.000041 сек (очень быстро) | 50000 раз — 0.12 сек (очень быстро) | PHP 7.1.2, WP 4.7.3

Возвращает

Строку. Переведенную строку под указанное число.

Использование

_nx( $single, $plural, $number, $context, $domain );
$single(строка) (обязательный)
Текст, который будет использован для числа 1 (единственного). 1 яблоко, 21 яблоко...
$plural(строка) (обязательный)
Текст, который будет использован для числа больше чем 1 (множественного). 2 яблока, 3 яблока...
$number(число) (обязательный)
Число на основе которого будет выбрана строка перевода.
$context(строка) (обязательный)
Контекст в котором делается перевод.
$domain(строка)
ID перевода. К этому ID сначала добавляются строки перевода через load_textdomain(), а затем по нему же получаются сам перевод.
По умолчанию: 'default'

Примеры

0

#1 Перевод множественного числа на основе контекста.

Тут $people_num - это число, а group of people - это контекст.

printf(
	_nx( '%s group', '%s groups', $people_num, 'group of people', 'mydomain' ),
	$people_num
);

Список изменений

С версии 2.8.0 Введена.
С версии 5.5.0 Introduced ngettext_with_context-{$domain} filter.

Код _nx() WP 6.4.3

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 int    $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.
	 */
	$translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );

	/**
	 * Filters the singular or plural form of a string with gettext context 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 $context     Context information for the translators.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( "ngettext_with_context_{$domain}", $translation, $single, $plural, $number, $context, $domain );

	return $translation;
}