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.
} $number
intrequired- Number of items.
$context
stringoptional- Context for the string.
Default:
''
$textdomain
stringoptional- Text domain. Default
'default'
.Default:
'default'
$locale
stringoptional- Locale. Default current locale.
Default:
null
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
Version | Description |
---|---|
6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.