WP_Translation_Controller::translate_plural( $plurals, int $number, string $context = , string $textdomain = ‘default’, string $locale = null ): string|false

Translates plurals.

Description

Checks both singular+plural combinations as well as just singulars, in case the translation file does not store the plural.

Parameters

string, 1: string} $plurals { Pair of singular and plural translations.
@type string $0 Singular translation.
@type string $1 Plural translation.
}
$numberintrequired
Number of items.
$contextstringoptional
Context for the string.

Default:''

$textdomainstringoptional
Text domain. Default 'default'.

Default:'default'

$localestringoptional
Locale. Default current locale.

Default:null

Return

string|false Translation on success, false otherwise.

Source

public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', string $locale = null ) {
	if ( '' !== $context ) {
		$context .= "\4";
	}

	$text        = implode( "\0", $plurals );
	$translation = $this->locate_translation( "{$context}{$text}", $textdomain, $locale );

	if ( false === $translation ) {
		$text        = $plurals[0];
		$translation = $this->locate_translation( "{$context}{$text}", $textdomain, $locale );

		if ( false === $translation ) {
			return false;
		}
	}

	/** @var WP_Translation_File $source */
	$source = $translation['source'];
	$num    = $source->get_plural_form( $number );

	// See \Translations::translate_plural().
	return $translation['entries'][ $num ] ?? $translation['entries'][0];
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.