equal
deleted
inserted
replaced
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 590 2010-12-20 19:58:37Z nbachiyski $ |
5 * @version $Id: translations.php 718 2012-10-31 00:32:02Z 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'; |
27 $key = $entry->key(); |
27 $key = $entry->key(); |
28 if (false === $key) return false; |
28 if (false === $key) return false; |
29 $this->entries[$key] = &$entry; |
29 $this->entries[$key] = &$entry; |
30 return true; |
30 return true; |
31 } |
31 } |
32 |
32 |
33 function add_entry_or_merge($entry) { |
33 function add_entry_or_merge($entry) { |
34 if (is_array($entry)) { |
34 if (is_array($entry)) { |
35 $entry = new Translation_Entry($entry); |
35 $entry = new Translation_Entry($entry); |
36 } |
36 } |
37 $key = $entry->key(); |
37 $key = $entry->key(); |
119 function merge_with(&$other) { |
119 function merge_with(&$other) { |
120 foreach( $other->entries as $entry ) { |
120 foreach( $other->entries as $entry ) { |
121 $this->entries[$entry->key()] = $entry; |
121 $this->entries[$entry->key()] = $entry; |
122 } |
122 } |
123 } |
123 } |
124 |
124 |
125 function merge_originals_with(&$other) { |
125 function merge_originals_with(&$other) { |
126 foreach( $other->entries as $entry ) { |
126 foreach( $other->entries as $entry ) { |
127 if ( !isset( $this->entries[$entry->key()] ) ) |
127 if ( !isset( $this->entries[$entry->key()] ) ) |
128 $this->entries[$entry->key()] = $entry; |
128 $this->entries[$entry->key()] = $entry; |
129 else |
129 else |
132 } |
132 } |
133 } |
133 } |
134 |
134 |
135 class Gettext_Translations extends Translations { |
135 class Gettext_Translations extends Translations { |
136 /** |
136 /** |
137 * The gettext implmentation of select_plural_form. |
137 * The gettext implementation of select_plural_form. |
138 * |
138 * |
139 * It lives in this class, because there are more than one descendand, which will use it and |
139 * It lives in this class, because there are more than one descendand, which will use it and |
140 * they can't share it effectively. |
140 * they can't share it effectively. |
141 * |
141 * |
142 */ |
142 */ |
146 $this->_nplurals = $nplurals; |
146 $this->_nplurals = $nplurals; |
147 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); |
147 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); |
148 } |
148 } |
149 return call_user_func($this->_gettext_select_plural_form, $count); |
149 return call_user_func($this->_gettext_select_plural_form, $count); |
150 } |
150 } |
151 |
151 |
152 function nplurals_and_expression_from_header($header) { |
152 function nplurals_and_expression_from_header($header) { |
153 if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) { |
153 if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) { |
154 $nplurals = (int)$matches[1]; |
154 $nplurals = (int)$matches[1]; |
155 $expression = trim($this->parenthesize_plural_exression($matches[2])); |
155 $expression = trim($this->parenthesize_plural_exression($matches[2])); |
156 return array($nplurals, $expression); |
156 return array($nplurals, $expression); |
172 } |
172 } |
173 |
173 |
174 /** |
174 /** |
175 * Adds parantheses to the inner parts of ternary operators in |
175 * Adds parantheses to the inner parts of ternary operators in |
176 * plural expressions, because PHP evaluates ternary oerators from left to right |
176 * plural expressions, because PHP evaluates ternary oerators from left to right |
177 * |
177 * |
178 * @param string $expression the expression without parentheses |
178 * @param string $expression the expression without parentheses |
179 * @return string the expression with parentheses added |
179 * @return string the expression with parentheses added |
180 */ |
180 */ |
181 function parenthesize_plural_exression($expression) { |
181 function parenthesize_plural_exression($expression) { |
182 $expression .= ';'; |
182 $expression .= ';'; |
200 $res .= $char; |
200 $res .= $char; |
201 } |
201 } |
202 } |
202 } |
203 return rtrim($res, ';'); |
203 return rtrim($res, ';'); |
204 } |
204 } |
205 |
205 |
206 function make_headers($translation) { |
206 function make_headers($translation) { |
207 $headers = array(); |
207 $headers = array(); |
208 // sometimes \ns are used instead of real new lines |
208 // sometimes \ns are used instead of real new lines |
209 $translation = str_replace('\n', "\n", $translation); |
209 $translation = str_replace('\n', "\n", $translation); |
210 $lines = explode("\n", $translation); |
210 $lines = explode("\n", $translation); |
213 if (!isset($parts[1])) continue; |
213 if (!isset($parts[1])) continue; |
214 $headers[trim($parts[0])] = trim($parts[1]); |
214 $headers[trim($parts[0])] = trim($parts[1]); |
215 } |
215 } |
216 return $headers; |
216 return $headers; |
217 } |
217 } |
218 |
218 |
219 function set_header($header, $value) { |
219 function set_header($header, $value) { |
220 parent::set_header($header, $value); |
220 parent::set_header($header, $value); |
221 if ('Plural-Forms' == $header) { |
221 if ('Plural-Forms' == $header) { |
222 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); |
222 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); |
223 $this->_nplurals = $nplurals; |
223 $this->_nplurals = $nplurals; |
232 * Provides the same interface as Translations, but doesn't do anything |
232 * Provides the same interface as Translations, but doesn't do anything |
233 */ |
233 */ |
234 class NOOP_Translations { |
234 class NOOP_Translations { |
235 var $entries = array(); |
235 var $entries = array(); |
236 var $headers = array(); |
236 var $headers = array(); |
237 |
237 |
238 function add_entry($entry) { |
238 function add_entry($entry) { |
239 return true; |
239 return true; |
240 } |
240 } |
241 |
241 |
242 function set_header($header, $value) { |
242 function set_header($header, $value) { |