WP_Translation_Controller::translate_plural()publicWP 6.5.0

Translates plurals.

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

Method of the class: WP_Translation_Controller{}

No Hooks.

Return

String|false. Translation on success, false otherwise.

Usage

$WP_Translation_Controller = new WP_Translation_Controller();
$WP_Translation_Controller->translate_plural( $plurals, $number, $context, $textdomain, ?string $locale );
$plurals(array) (required)

Pair of singular and plural translations.

  • 0(string)
    Singular translation.

  • 1(string)
    Plural translation.
$number(int) (required)
Number of items.
$context(string)
Context for the string.
Default: empty string
$textdomain(string)
Text domain.
Default: 'default'
?string $locale **
-
Default: null

Changelog

Since 6.5.0 Introduced.

WP_Translation_Controller::translate_plural() code WP 6.7.1

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];
}