WP_Translation_Controller::translate_plural( array $plurals, int $number, string $context, string $textdomain, string|null $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

$pluralsarrayrequired
Pair of singular and plural translations.
  • 0 string
    Singular translation.
  • 1 string
    Plural translation.
$numberintrequired
Number of items.
$contextstringoptional
Context for the string. Default empty string.
$textdomainstringoptional
Text domain. Default 'default'.
$localestring|nulloptional
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.