19 * Add entry to the PO structure |
19 * Add entry to the PO structure |
20 * |
20 * |
21 * @param array|Translation_Entry $entry |
21 * @param array|Translation_Entry $entry |
22 * @return bool true on success, false if the entry doesn't have a key |
22 * @return bool true on success, false if the entry doesn't have a key |
23 */ |
23 */ |
24 function add_entry( $entry ) { |
24 public function add_entry( $entry ) { |
25 if ( is_array( $entry ) ) { |
25 if ( is_array( $entry ) ) { |
26 $entry = new Translation_Entry( $entry ); |
26 $entry = new Translation_Entry( $entry ); |
27 } |
27 } |
28 $key = $entry->key(); |
28 $key = $entry->key(); |
29 if ( false === $key ) { |
29 if ( false === $key ) { |
61 * TODO: this should be out of this class, it is gettext specific |
61 * TODO: this should be out of this class, it is gettext specific |
62 * |
62 * |
63 * @param string $header header name, without trailing : |
63 * @param string $header header name, without trailing : |
64 * @param string $value header value, without trailing \n |
64 * @param string $value header value, without trailing \n |
65 */ |
65 */ |
66 function set_header( $header, $value ) { |
66 public function set_header( $header, $value ) { |
67 $this->headers[ $header ] = $value; |
67 $this->headers[ $header ] = $value; |
68 } |
68 } |
69 |
69 |
70 /** |
70 /** |
71 * @param array $headers |
71 * @param array $headers |
72 */ |
72 */ |
73 function set_headers( $headers ) { |
73 public function set_headers( $headers ) { |
74 foreach ( $headers as $header => $value ) { |
74 foreach ( $headers as $header => $value ) { |
75 $this->set_header( $header, $value ); |
75 $this->set_header( $header, $value ); |
76 } |
76 } |
77 } |
77 } |
78 |
78 |
79 /** |
79 /** |
80 * @param string $header |
80 * @param string $header |
81 */ |
81 */ |
82 function get_header( $header ) { |
82 public function get_header( $header ) { |
83 return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false; |
83 return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false; |
84 } |
84 } |
85 |
85 |
86 /** |
86 /** |
87 * @param Translation_Entry $entry |
87 * @param Translation_Entry $entry |
88 */ |
88 */ |
89 function translate_entry( &$entry ) { |
89 public function translate_entry( &$entry ) { |
90 $key = $entry->key(); |
90 $key = $entry->key(); |
91 return isset( $this->entries[ $key ] ) ? $this->entries[ $key ] : false; |
91 return isset( $this->entries[ $key ] ) ? $this->entries[ $key ] : false; |
92 } |
92 } |
93 |
93 |
94 /** |
94 /** |
95 * @param string $singular |
95 * @param string $singular |
96 * @param string $context |
96 * @param string $context |
97 * @return string |
97 * @return string |
98 */ |
98 */ |
99 function translate( $singular, $context = null ) { |
99 public function translate( $singular, $context = null ) { |
100 $entry = new Translation_Entry( |
100 $entry = new Translation_Entry( |
101 array( |
101 array( |
102 'singular' => $singular, |
102 'singular' => $singular, |
103 'context' => $context, |
103 'context' => $context, |
104 ) |
104 ) |
116 * This function should be overridden by the subclasses. For example MO/PO can derive the logic |
116 * This function should be overridden by the subclasses. For example MO/PO can derive the logic |
117 * from their headers. |
117 * from their headers. |
118 * |
118 * |
119 * @param int $count number of items |
119 * @param int $count number of items |
120 */ |
120 */ |
121 function select_plural_form( $count ) { |
121 public function select_plural_form( $count ) { |
122 return 1 == $count ? 0 : 1; |
122 return 1 == $count ? 0 : 1; |
123 } |
123 } |
124 |
124 |
125 /** |
125 /** |
126 * @return int |
126 * @return int |
127 */ |
127 */ |
128 function get_plural_forms_count() { |
128 public function get_plural_forms_count() { |
129 return 2; |
129 return 2; |
130 } |
130 } |
131 |
131 |
132 /** |
132 /** |
133 * @param string $singular |
133 * @param string $singular |
134 * @param string $plural |
134 * @param string $plural |
135 * @param int $count |
135 * @param int $count |
136 * @param string $context |
136 * @param string $context |
137 */ |
137 */ |
138 function translate_plural( $singular, $plural, $count, $context = null ) { |
138 public function translate_plural( $singular, $plural, $count, $context = null ) { |
139 $entry = new Translation_Entry( |
139 $entry = new Translation_Entry( |
140 array( |
140 array( |
141 'singular' => $singular, |
141 'singular' => $singular, |
142 'plural' => $plural, |
142 'plural' => $plural, |
143 'context' => $context, |
143 'context' => $context, |
157 |
157 |
158 /** |
158 /** |
159 * Merge $other in the current object. |
159 * Merge $other in the current object. |
160 * |
160 * |
161 * @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference). |
161 * @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference). |
162 * @return void |
162 */ |
163 */ |
163 public function merge_with( &$other ) { |
164 function merge_with( &$other ) { |
|
165 foreach ( $other->entries as $entry ) { |
164 foreach ( $other->entries as $entry ) { |
166 $this->entries[ $entry->key() ] = $entry; |
165 $this->entries[ $entry->key() ] = $entry; |
167 } |
166 } |
168 } |
167 } |
169 |
168 |
170 /** |
169 /** |
171 * @param object $other |
170 * @param object $other |
172 */ |
171 */ |
173 function merge_originals_with( &$other ) { |
172 public function merge_originals_with( &$other ) { |
174 foreach ( $other->entries as $entry ) { |
173 foreach ( $other->entries as $entry ) { |
175 if ( ! isset( $this->entries[ $entry->key() ] ) ) { |
174 if ( ! isset( $this->entries[ $entry->key() ] ) ) { |
176 $this->entries[ $entry->key() ] = $entry; |
175 $this->entries[ $entry->key() ] = $entry; |
177 } else { |
176 } else { |
178 $this->entries[ $entry->key() ]->merge_with( $entry ); |
177 $this->entries[ $entry->key() ]->merge_with( $entry ); |
188 * It lives in this class, because there are more than one descendand, which will use it and |
187 * It lives in this class, because there are more than one descendand, which will use it and |
189 * they can't share it effectively. |
188 * they can't share it effectively. |
190 * |
189 * |
191 * @param int $count |
190 * @param int $count |
192 */ |
191 */ |
193 function gettext_select_plural_form( $count ) { |
192 public function gettext_select_plural_form( $count ) { |
194 if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) { |
193 if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) { |
195 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) ); |
194 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) ); |
196 $this->_nplurals = $nplurals; |
195 $this->_nplurals = $nplurals; |
197 $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression ); |
196 $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression ); |
198 } |
197 } |
201 |
200 |
202 /** |
201 /** |
203 * @param string $header |
202 * @param string $header |
204 * @return array |
203 * @return array |
205 */ |
204 */ |
206 function nplurals_and_expression_from_header( $header ) { |
205 public function nplurals_and_expression_from_header( $header ) { |
207 if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) { |
206 if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) { |
208 $nplurals = (int) $matches[1]; |
207 $nplurals = (int) $matches[1]; |
209 $expression = trim( $matches[2] ); |
208 $expression = trim( $matches[2] ); |
210 return array( $nplurals, $expression ); |
209 return array( $nplurals, $expression ); |
211 } else { |
210 } else { |
218 * plural forms header |
217 * plural forms header |
219 * |
218 * |
220 * @param int $nplurals |
219 * @param int $nplurals |
221 * @param string $expression |
220 * @param string $expression |
222 */ |
221 */ |
223 function make_plural_form_function( $nplurals, $expression ) { |
222 public function make_plural_form_function( $nplurals, $expression ) { |
224 try { |
223 try { |
225 $handler = new Plural_Forms( rtrim( $expression, ';' ) ); |
224 $handler = new Plural_Forms( rtrim( $expression, ';' ) ); |
226 return array( $handler, 'get' ); |
225 return array( $handler, 'get' ); |
227 } catch ( Exception $e ) { |
226 } catch ( Exception $e ) { |
228 // Fall back to default plural-form function. |
227 // Fall back to default plural-form function. |
235 * plural expressions, because PHP evaluates ternary oerators from left to right |
234 * plural expressions, because PHP evaluates ternary oerators from left to right |
236 * |
235 * |
237 * @param string $expression the expression without parentheses |
236 * @param string $expression the expression without parentheses |
238 * @return string the expression with parentheses added |
237 * @return string the expression with parentheses added |
239 */ |
238 */ |
240 function parenthesize_plural_exression( $expression ) { |
239 public function parenthesize_plural_exression( $expression ) { |
241 $expression .= ';'; |
240 $expression .= ';'; |
242 $res = ''; |
241 $res = ''; |
243 $depth = 0; |
242 $depth = 0; |
244 for ( $i = 0; $i < strlen( $expression ); ++$i ) { |
243 for ( $i = 0; $i < strlen( $expression ); ++$i ) { |
245 $char = $expression[ $i ]; |
244 $char = $expression[ $i ]; |
264 |
263 |
265 /** |
264 /** |
266 * @param string $translation |
265 * @param string $translation |
267 * @return array |
266 * @return array |
268 */ |
267 */ |
269 function make_headers( $translation ) { |
268 public function make_headers( $translation ) { |
270 $headers = array(); |
269 $headers = array(); |
271 // Sometimes \n's are used instead of real new lines. |
270 // Sometimes \n's are used instead of real new lines. |
272 $translation = str_replace( '\n', "\n", $translation ); |
271 $translation = str_replace( '\n', "\n", $translation ); |
273 $lines = explode( "\n", $translation ); |
272 $lines = explode( "\n", $translation ); |
274 foreach ( $lines as $line ) { |
273 foreach ( $lines as $line ) { |
283 |
282 |
284 /** |
283 /** |
285 * @param string $header |
284 * @param string $header |
286 * @param string $value |
285 * @param string $value |
287 */ |
286 */ |
288 function set_header( $header, $value ) { |
287 public function set_header( $header, $value ) { |
289 parent::set_header( $header, $value ); |
288 parent::set_header( $header, $value ); |
290 if ( 'Plural-Forms' === $header ) { |
289 if ( 'Plural-Forms' === $header ) { |
291 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) ); |
290 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) ); |
292 $this->_nplurals = $nplurals; |
291 $this->_nplurals = $nplurals; |
293 $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression ); |
292 $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression ); |
302 */ |
301 */ |
303 class NOOP_Translations { |
302 class NOOP_Translations { |
304 public $entries = array(); |
303 public $entries = array(); |
305 public $headers = array(); |
304 public $headers = array(); |
306 |
305 |
307 function add_entry( $entry ) { |
306 public function add_entry( $entry ) { |
308 return true; |
307 return true; |
309 } |
308 } |
310 |
309 |
311 /** |
310 /** |
312 * @param string $header |
311 * @param string $header |
313 * @param string $value |
312 * @param string $value |
314 */ |
313 */ |
315 function set_header( $header, $value ) { |
314 public function set_header( $header, $value ) { |
316 } |
315 } |
317 |
316 |
318 /** |
317 /** |
319 * @param array $headers |
318 * @param array $headers |
320 */ |
319 */ |
321 function set_headers( $headers ) { |
320 public function set_headers( $headers ) { |
322 } |
321 } |
323 |
322 |
324 /** |
323 /** |
325 * @param string $header |
324 * @param string $header |
326 * @return false |
325 * @return false |
327 */ |
326 */ |
328 function get_header( $header ) { |
327 public function get_header( $header ) { |
329 return false; |
328 return false; |
330 } |
329 } |
331 |
330 |
332 /** |
331 /** |
333 * @param Translation_Entry $entry |
332 * @param Translation_Entry $entry |
334 * @return false |
333 * @return false |
335 */ |
334 */ |
336 function translate_entry( &$entry ) { |
335 public function translate_entry( &$entry ) { |
337 return false; |
336 return false; |
338 } |
337 } |
339 |
338 |
340 /** |
339 /** |
341 * @param string $singular |
340 * @param string $singular |
342 * @param string $context |
341 * @param string $context |
343 */ |
342 */ |
344 function translate( $singular, $context = null ) { |
343 public function translate( $singular, $context = null ) { |
345 return $singular; |
344 return $singular; |
346 } |
345 } |
347 |
346 |
348 /** |
347 /** |
349 * @param int $count |
348 * @param int $count |
350 * @return bool |
349 * @return bool |
351 */ |
350 */ |
352 function select_plural_form( $count ) { |
351 public function select_plural_form( $count ) { |
353 return 1 == $count ? 0 : 1; |
352 return 1 == $count ? 0 : 1; |
354 } |
353 } |
355 |
354 |
356 /** |
355 /** |
357 * @return int |
356 * @return int |
358 */ |
357 */ |
359 function get_plural_forms_count() { |
358 public function get_plural_forms_count() { |
360 return 2; |
359 return 2; |
361 } |
360 } |
362 |
361 |
363 /** |
362 /** |
364 * @param string $singular |
363 * @param string $singular |
365 * @param string $plural |
364 * @param string $plural |
366 * @param int $count |
365 * @param int $count |
367 * @param string $context |
366 * @param string $context |
368 */ |
367 */ |
369 function translate_plural( $singular, $plural, $count, $context = null ) { |
368 public function translate_plural( $singular, $plural, $count, $context = null ) { |
370 return 1 == $count ? $singular : $plural; |
369 return 1 == $count ? $singular : $plural; |
371 } |
370 } |
372 |
371 |
373 /** |
372 /** |
374 * @param object $other |
373 * @param object $other |
375 */ |
374 */ |
376 function merge_with( &$other ) { |
375 public function merge_with( &$other ) { |
377 } |
376 } |
378 } |
377 } |
379 endif; |
378 endif; |