WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/04/2017 01:29:59 AM (4 years ago)
Author:
pento
Message:

I18N: Introduce the Plural_Forms class.

Historically, we've evaluated the plural forms for each language using create_function(). This is being deprecated in PHP 7.2, so needs to be replaced.

The Plural_Forms class parses the Plural-Forms header from the PO file, and internally caches the result of all subsequent plural form tests, allowing it to match the performance of the existing code.

Props rmccue.
Fixes #41562.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pomo/translations.php

    r41686 r41722  
    88 */
    99
     10require_once dirname(__FILE__) . '/plural-forms.php';
    1011require_once dirname(__FILE__) . '/entry.php';
    1112
     
    188189        if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
    189190            $nplurals = (int)$matches[1];
    190             $expression = trim($this->parenthesize_plural_exression($matches[2]));
     191            $expression = trim( $matches[2] );
    191192            return array($nplurals, $expression);
    192193        } else {
     
    202203     */
    203204    function make_plural_form_function($nplurals, $expression) {
    204         $expression = str_replace('n', '$n', $expression);
    205         $func_body = "
    206             \$index = (int)($expression);
    207             return (\$index < $nplurals)? \$index : $nplurals - 1;";
    208         return create_function('$n', $func_body);
     205        try {
     206            $handler = new Plural_Forms( rtrim( $expression, ';' ) );
     207            return array( $handler, 'get' );
     208        } catch ( Exception $e ) {
     209            // Fall back to default plural-form function.
     210            return $this->make_plural_form_function( 2, 'n != 1' );
     211        }
    209212    }
    210213
Note: See TracChangeset for help on using the changeset viewer.