1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Class for a set of entries for translation and their associated headers |
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 * @package pomo |
6 * @package pomo |
7 * @subpackage translations |
7 * @subpackage translations |
8 */ |
8 */ |
9 |
9 |
10 require_once dirname(__FILE__) . '/entry.php'; |
10 require_once dirname(__FILE__) . '/entry.php'; |
11 |
11 |
|
12 if ( !class_exists( 'Translations' ) ): |
12 class Translations { |
13 class Translations { |
13 var $entries = array(); |
14 var $entries = array(); |
14 var $headers = array(); |
15 var $headers = array(); |
15 |
16 |
16 /** |
17 /** |
115 * they can't share it effectively. |
116 * they can't share it effectively. |
116 * |
117 * |
117 */ |
118 */ |
118 function gettext_select_plural_form($count) { |
119 function gettext_select_plural_form($count) { |
119 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) { |
120 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) { |
120 $plural_header = $this->get_header('Plural-Forms'); |
121 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); |
121 $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($plural_header); |
122 $this->_nplurals = $nplurals; |
|
123 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); |
122 } |
124 } |
123 return call_user_func($this->_gettext_select_plural_form, $count); |
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 |
126 /** |
138 /** |
127 * Makes a function, which will return the right translation index, according to the |
139 * Makes a function, which will return the right translation index, according to the |
128 * plural forms header |
140 * plural forms header |
129 */ |
141 */ |
130 function _make_gettext_select_plural_form($plural_header) { |
142 function make_plural_form_function($nplurals, $expression) { |
131 $res = create_function('$count', 'return 1 == $count? 0 : 1;'); |
143 $expression = str_replace('n', '$n', $expression); |
132 if ($plural_header && (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $plural_header, $matches))) { |
144 $func_body = " |
133 $nplurals = (int)$matches[1]; |
145 \$index = (int)($expression); |
134 $this->_nplurals = $nplurals; |
146 return (\$index < $nplurals)? \$index : $nplurals - 1;"; |
135 $plural_expr = trim($this->_parenthesize_plural_exression($matches[2])); |
147 return create_function('$n', $func_body); |
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; |
|
143 } |
148 } |
144 |
149 |
145 /** |
150 /** |
146 * Adds parantheses to the inner parts of ternary operators in |
151 * Adds parantheses to the inner parts of ternary operators in |
147 * plural expressions, because PHP evaluates ternary oerators from left to right |
152 * plural expressions, because PHP evaluates ternary oerators from left to right |
148 * |
153 * |
149 * @param string $expression the expression without parentheses |
154 * @param string $expression the expression without parentheses |
150 * @return string the expression with parentheses added |
155 * @return string the expression with parentheses added |
151 */ |
156 */ |
152 function _parenthesize_plural_exression($expression) { |
157 function parenthesize_plural_exression($expression) { |
153 $expression .= ';'; |
158 $expression .= ';'; |
154 $res = ''; |
159 $res = ''; |
155 $depth = 0; |
160 $depth = 0; |
156 for ($i = 0; $i < strlen($expression); ++$i) { |
161 for ($i = 0; $i < strlen($expression); ++$i) { |
157 $char = $expression[$i]; |
162 $char = $expression[$i]; |
184 if (!isset($parts[1])) continue; |
189 if (!isset($parts[1])) continue; |
185 $headers[trim($parts[0])] = trim($parts[1]); |
190 $headers[trim($parts[0])] = trim($parts[1]); |
186 } |
191 } |
187 return $headers; |
192 return $headers; |
188 } |
193 } |
189 |
194 |
190 function set_header($header, $value) { |
195 function set_header($header, $value) { |
191 parent::set_header($header, $value); |
196 parent::set_header($header, $value); |
192 if ('Plural-Forms' == $header) |
197 if ('Plural-Forms' == $header) { |
193 $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($value); |
198 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); |
194 } |
199 $this->_nplurals = $nplurals; |
195 |
200 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); |
196 |
201 } |
|
202 } |
197 } |
203 } |
198 |
204 endif; |
199 ?> |
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; |