WordPress.org

Make WordPress Core

Changeset 12079


Ignore:
Timestamp:
10/21/09 07:06:55 (4 years ago)
Author:
westi
Message:

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

Location:
trunk/wp-includes/pomo
Files:
5 edited

Legend:

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

    r11626 r12079  
    33 * Contains Translation_Entry class 
    44 * 
    5  * @version $Id: entry.php 115 2009-05-11 18:56:15Z nbachiyski $ 
     5 * @version $Id: entry.php 222 2009-09-07 21:14:23Z nbachiyski $ 
    66 * @package pomo 
    77 * @subpackage entry 
    88 */ 
    99 
    10  
     10if ( !class_exists( 'Translation_Entry' ) ): 
    1111/** 
    1212 * Translation_Entry class encapsulates a translatable string 
     
    6868    } 
    6969} 
    70 ?> 
     70endif; 
  • trunk/wp-includes/pomo/mo.php

    r11626 r12079  
    33 * Class for working with MO files 
    44 * 
    5  * @version $Id: mo.php 106 2009-04-23 19:48:22Z nbachiyski $ 
     5 * @version $Id: mo.php 221 2009-09-07 21:08:21Z nbachiyski $ 
    66 * @package pomo 
    77 * @subpackage mo 
     
    1111require_once dirname(__FILE__) . '/streams.php'; 
    1212 
     13if ( !class_exists( 'MO' ) ): 
    1314class MO extends Gettext_Translations { 
    1415 
     
    9697 
    9798    function get_byteorder($magic) { 
    98  
    9999        // The magic is 0x950412de 
    100100 
     
    103103        $magic_little_64 = (int) 2500072158; 
    104104        // 0xde120495 
    105         $magic_big = ((int) - 569244523) && 0xFFFFFFFF; 
    106          
     105        $magic_big = ((int) - 569244523) & 0xFFFFFFFF; 
    107106        if ($magic_little == $magic || $magic_little_64 == $magic) { 
    108107            return 'little'; 
     
    183182 
    184183} 
    185 ?> 
     184endif; 
  • trunk/wp-includes/pomo/po.php

    r11626 r12079  
    33 * Class for working with PO files 
    44 * 
    5  * @version $Id: po.php 123 2009-05-13 19:35:43Z nbachiyski $ 
     5 * @version $Id: po.php 283 2009-09-23 16:21:51Z nbachiyski $ 
    66 * @package pomo 
    77 * @subpackage po 
     
    1717 * Routines for working with PO files 
    1818 */ 
     19if ( !class_exists( 'PO' ) ): 
    1920class PO extends Gettext_Translations { 
    2021     
     
    317318            } 
    318319        } 
    319         if (array() == array_filter($entry->translations)) $entry->translations = array(); 
     320        if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) { 
     321            $entry->translations = array(); 
     322        } 
    320323        return array('entry' => $entry, 'lineno' => $lineno); 
    321324    } 
     
    358361    } 
    359362} 
    360 ?> 
     363endif; 
  • trunk/wp-includes/pomo/streams.php

    r11626 r12079  
    44 * Based on the classes from Danilo Segan <danilo@kvota.net> 
    55 * 
    6  * @version $Id: streams.php 138 2009-06-23 13:22:09Z nbachiyski $ 
     6 * @version $Id: streams.php 223 2009-09-07 21:20:13Z nbachiyski $ 
    77 * @package pomo 
    88 * @subpackage streams 
     
    1010 
    1111 
     12if ( !class_exists( 'POMO_StringReader' ) ): 
    1213/** 
    1314 * Provides file-like methods for manipulating a string instead 
     
    6263 
    6364} 
     65endif; 
    6466 
     67if ( !class_exists( 'POMO_CachedFileReader' ) ): 
    6568/** 
    6669 * Reads the contents of the file in the beginning. 
     
    7578    } 
    7679} 
     80endif; 
    7781 
     82if ( !class_exists( 'POMO_CachedIntFileReader' ) ): 
    7883/** 
    7984 * Allows reading integers from a file. 
     
    134139    } 
    135140} 
    136  
    137 ?> 
     141endif; 
  • 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.