Make WordPress Core


Ignore:
Timestamp:
10/21/2009 07:06:55 AM (17 years ago)
Author:
westi
Message:

Merge updated pomo code. Includes new NOOP_Translations class see #10971 props nbachiyski.

File:
1 edited

Legend:

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

    r11626 r12079  
    33 * Class for a set of entries for translation and their associated headers
    44 *
    5  * @version $Id: translations.php 114 2009-05-11 17:30:38Z nbachiyski $
     5 * @version $Id: translations.php 291 2009-10-21 05:46:08Z nbachiyski $
    66 * @package pomo
    77 * @subpackage translations
    … …  
    1010require_once dirname(__FILE__) . '/entry.php';
    1111
     12if ( !class_exists( 'Translations' ) ):
    1213class Translations {
    1314        var $entries = array();
    … …  
    2627                $key = $entry->key();
    2728                if (false === $key) return false;
    28                 $this->entries[$key] = $entry;
     29                $this->entries[$key] = &$entry;
    2930                return true;
    3031        }
    … …  
    118119        function gettext_select_plural_form($count) {
    119120                if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
    120                         $plural_header = $this->get_header('Plural-Forms');
    121                         $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($plural_header);
     121                        list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
     122                        $this->_nplurals = $nplurals;
     123                        $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
    122124                }
    123125                return call_user_func($this->_gettext_select_plural_form, $count);
     126        }
     127       
     128        function nplurals_and_expression_from_header($header) {
     129                if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
     130                        $nplurals = (int)$matches[1];
     131                        $expression = trim($this->parenthesize_plural_exression($matches[2]));
     132                        return array($nplurals, $expression);
     133                } else {
     134                        return array(2, 'n != 1');
     135                }
    124136        }
    125137
    … …  
    128140         * plural forms header
    129141         */
    130         function _make_gettext_select_plural_form($plural_header) {
    131                 $res = create_function('$count', 'return 1 == $count? 0 : 1;');
    132                 if ($plural_header && (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $plural_header, $matches))) {
    133                         $nplurals = (int)$matches[1];
    134                         $this->_nplurals = $nplurals;
    135                         $plural_expr = trim($this->_parenthesize_plural_exression($matches[2]));
    136                         $plural_expr = str_replace('n', '$n', $plural_expr);
    137                         $func_body = "
    138                                 \$index = (int)($plural_expr);
    139                                 return (\$index < $nplurals)? \$index : $nplurals - 1;";
    140                         $res = create_function('$n', $func_body);
    141                 }
    142                 return $res;
     142        function make_plural_form_function($nplurals, $expression) {
     143                $expression = str_replace('n', '$n', $expression);
     144                $func_body = "
     145                        \$index = (int)($expression);
     146                        return (\$index < $nplurals)? \$index : $nplurals - 1;";
     147                return create_function('$n', $func_body);
    143148        }
    144149
    … …  
    150155         * @return string the expression with parentheses added
    151156         */
    152         function _parenthesize_plural_exression($expression) {
     157        function parenthesize_plural_exression($expression) {
    153158                $expression .= ';';
    154159                $res = '';
    … …  
    187192                return $headers;
    188193        }
    189 
     194       
    190195        function set_header($header, $value) {
    191196                parent::set_header($header, $value);
    192                 if ('Plural-Forms' == $header)
    193                         $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($value);
    194         }
    195 
    196        
     197                if ('Plural-Forms' == $header) {
     198                        list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
     199                        $this->_nplurals = $nplurals;
     200                        $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
     201                }
     202        }
    197203}
    198 
    199 ?>
     204endif;
     205
     206if ( !class_exists( 'NOOP_Translations' ) ):
     207/**
     208 * Provides the same interface as Translations, but doesn't do anything
     209 */
     210class NOOP_Translations {
     211        var $entries = array();
     212        var $headers = array();
     213       
     214        function add_entry($entry) {
     215                return true;
     216        }
     217
     218        function set_header($header, $value) {
     219        }
     220
     221        function set_headers(&$headers) {
     222        }
     223
     224        function get_header($header) {
     225                return false;
     226        }
     227
     228        function translate_entry(&$entry) {
     229                return false;
     230        }
     231
     232        function translate($singular, $context=null) {
     233                return $singular;
     234        }
     235
     236        function select_plural_form($count) {
     237                return 1 == $count? 0 : 1;
     238        }
     239
     240        function get_plural_forms_count() {
     241                return 2;
     242        }
     243
     244        function translate_plural($singular, $plural, $count, $context = null) {
     245                        return 1 == $count? $singular : $plural;
     246        }
     247
     248        function merge_with(&$other) {
     249        }
     250}
     251endif;
Note: See TracChangeset for help on using the changeset viewer.