WordPress.org

Make WordPress Core

Changeset 41722


Ignore:
Timestamp:
10/04/2017 01:29:59 AM (2 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.

Location:
trunk
Files:
2 added
6 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
  • trunk/tests/phpunit/tests/pomo/mo.php

    r36788 r41722  
    11<?php
    22
     3/**
     4 * @group pomo
     5 */
    36class Tests_POMO_MO extends WP_UnitTestCase {
    47
  • trunk/tests/phpunit/tests/pomo/noopTranslations.php

    r25002 r41722  
    11<?php
    22
     3/**
     4 * @group pomo
     5 */
    36class Tests_POMO_NOOPTranslations extends WP_UnitTestCase {
    47    function setUp() {
  • trunk/tests/phpunit/tests/pomo/po.php

    r26500 r41722  
    11<?php
    22
     3/**
     4 * @group pomo
     5 */
    36class Tests_POMO_PO extends WP_UnitTestCase {
    47    function setUp() {
  • trunk/tests/phpunit/tests/pomo/translationEntry.php

    r25002 r41722  
    11<?php
    22
     3/**
     4 * @group pomo
     5 */
    36class Tests_POMO_TranslationEntry extends WP_UnitTestCase {
    47
  • trunk/tests/phpunit/tests/pomo/translations.php

    r25002 r41722  
    11<?php
     2
     3/**
     4 * @group pomo
     5 */
    26class Tests_POMO_Translations extends WP_UnitTestCase {
    37
Note: See TracChangeset for help on using the changeset viewer.