Changeset 12079 for trunk/wp-includes/pomo/translations.php
- Timestamp:
- 10/21/2009 07:06:55 AM (16 years ago)
- File:
-
- 1 edited
-
trunk/wp-includes/pomo/translations.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/pomo/translations.php
r11626 r12079 3 3 * Class for a set of entries for translation and their associated headers 4 4 * 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 $ 6 6 * @package pomo 7 7 * @subpackage translations … … 10 10 require_once dirname(__FILE__) . '/entry.php'; 11 11 12 if ( !class_exists( 'Translations' ) ): 12 13 class Translations { 13 14 var $entries = array(); … … 26 27 $key = $entry->key(); 27 28 if (false === $key) return false; 28 $this->entries[$key] = $entry;29 $this->entries[$key] = &$entry; 29 30 return true; 30 31 } … … 118 119 function gettext_select_plural_form($count) { 119 120 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); 122 124 } 123 125 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 } 124 136 } 125 137 … … 128 140 * plural forms header 129 141 */ 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); 143 148 } 144 149 … … 150 155 * @return string the expression with parentheses added 151 156 */ 152 function _parenthesize_plural_exression($expression) {157 function parenthesize_plural_exression($expression) { 153 158 $expression .= ';'; 154 159 $res = ''; … … 187 192 return $headers; 188 193 } 189 194 190 195 function set_header($header, $value) { 191 196 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 } 197 203 } 198 199 ?> 204 endif; 205 206 if ( !class_exists( 'NOOP_Translations' ) ): 207 /** 208 * Provides the same interface as Translations, but doesn't do anything 209 */ 210 class 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 } 251 endif;
Note: See TracChangeset
for help on using the changeset viewer.