I18N: Fix plural forms parsing in `WP_Translation_File`.
Ensures the plural expression from the translation file header is correctly parsed. Prevents silent failures in the attempt to create the plural form function. Adds additional tests. Props Chouby. See #59656. Built from https://develop.svn.wordpress.org/trunk@57518 git-svn-id: http://core.svn.wordpress.org/trunk@57019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
18c47606f2
commit
953e148764
|
@ -207,7 +207,7 @@ abstract class WP_Translation_File {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the plural form for a count.
|
||||
* Returns the plural form for a given number.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
|
@ -219,9 +219,9 @@ abstract class WP_Translation_File {
|
|||
$this->parse_file();
|
||||
}
|
||||
|
||||
// In case a plural form is specified as a header, but no function included, build one.
|
||||
if ( null === $this->plural_forms && isset( $this->headers['plural-forms'] ) ) {
|
||||
$this->plural_forms = $this->make_plural_form_function( $this->headers['plural-forms'] );
|
||||
$expression = $this->get_plural_expression_from_header( $this->headers['plural-forms'] );
|
||||
$this->plural_forms = $this->make_plural_form_function( $expression );
|
||||
}
|
||||
|
||||
if ( is_callable( $this->plural_forms ) ) {
|
||||
|
@ -231,6 +231,7 @@ abstract class WP_Translation_File {
|
|||
* @var int $result Plural form.
|
||||
*/
|
||||
$result = call_user_func( $this->plural_forms, $number );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
@ -238,6 +239,22 @@ abstract class WP_Translation_File {
|
|||
return ( 1 === $number ? 0 : 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plural forms expression as a tuple.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @param string $header Plural-Forms header string.
|
||||
* @return string Plural forms expression.
|
||||
*/
|
||||
protected function get_plural_expression_from_header( $header ) {
|
||||
if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
|
||||
return trim( $matches[2] );
|
||||
}
|
||||
|
||||
return 'n != 1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a function, which will return the right translation index, according to the
|
||||
* plural forms header.
|
||||
|
@ -247,7 +264,7 @@ abstract class WP_Translation_File {
|
|||
* @param string $expression Plural form expression.
|
||||
* @return callable(int $num): int Plural forms function.
|
||||
*/
|
||||
public function make_plural_form_function( string $expression ): callable {
|
||||
protected function make_plural_form_function( string $expression ): callable {
|
||||
try {
|
||||
$handler = new Plural_Forms( rtrim( $expression, ';' ) );
|
||||
return array( $handler, 'get' );
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.5-alpha-57517';
|
||||
$wp_version = '6.5-alpha-57518';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue