3 * Class for a set of entries for translation and their associated headers
5 * @version $Id: translations.php 718 2012-10-31 00:32:02Z nbachiyski $
7 * @subpackage translations
10 require_once dirname(__FILE__) . '/entry.php';
12 if ( !class_exists( 'Translations' ) ):
14 var $entries = array();
15 var $headers = array();
18 * Add entry to the PO structure
20 * @param array|Translation_Entry &$entry
21 * @return bool true on success, false if the entry doesn't have a key
23 function add_entry($entry) {
24 if (is_array($entry)) {
25 $entry = new Translation_Entry($entry);
28 if (false === $key) return false;
29 $this->entries[$key] = &$entry;
34 * @param array|Translation_Entry $entry
37 function add_entry_or_merge($entry) {
38 if (is_array($entry)) {
39 $entry = new Translation_Entry($entry);
42 if (false === $key) return false;
43 if (isset($this->entries[$key]))
44 $this->entries[$key]->merge_with($entry);
46 $this->entries[$key] = &$entry;
51 * Sets $header PO header to $value
53 * If the header already exists, it will be overwritten
55 * TODO: this should be out of this class, it is gettext specific
57 * @param string $header header name, without trailing :
58 * @param string $value header value, without trailing \n
60 function set_header($header, $value) {
61 $this->headers[$header] = $value;
65 * @param array $headers
67 function set_headers($headers) {
68 foreach($headers as $header => $value) {
69 $this->set_header($header, $value);
74 * @param string $header
76 function get_header($header) {
77 return isset($this->headers[$header])? $this->headers[$header] : false;
81 * @param Translation_Entry $entry
83 function translate_entry(&$entry) {
85 return isset($this->entries[$key])? $this->entries[$key] : false;
89 * @param string $singular
90 * @param string $context
93 function translate($singular, $context=null) {
94 $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context));
95 $translated = $this->translate_entry($entry);
96 return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular;
100 * Given the number of items, returns the 0-based index of the plural form to use
102 * Here, in the base Translations class, the common logic for English is implemented:
103 * 0 if there is one element, 1 otherwise
105 * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
106 * from their headers.
108 * @param integer $count number of items
110 function select_plural_form($count) {
111 return 1 == $count? 0 : 1;
114 function get_plural_forms_count() {
119 * @param string $singular
120 * @param string $plural
122 * @param string $context
124 function translate_plural($singular, $plural, $count, $context = null) {
125 $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context));
126 $translated = $this->translate_entry($entry);
127 $index = $this->select_plural_form($count);
128 $total_plural_forms = $this->get_plural_forms_count();
129 if ($translated && 0 <= $index && $index < $total_plural_forms &&
130 is_array($translated->translations) &&
131 isset($translated->translations[$index]))
132 return $translated->translations[$index];
134 return 1 == $count? $singular : $plural;
138 * Merge $other in the current object.
140 * @param Object &$other Another Translation object, whose translations will be merged in this one
143 function merge_with(&$other) {
144 foreach( $other->entries as $entry ) {
145 $this->entries[$entry->key()] = $entry;
149 function merge_originals_with(&$other) {
150 foreach( $other->entries as $entry ) {
151 if ( !isset( $this->entries[$entry->key()] ) )
152 $this->entries[$entry->key()] = $entry;
154 $this->entries[$entry->key()]->merge_with($entry);
159 class Gettext_Translations extends Translations {
161 * The gettext implementation of select_plural_form.
163 * It lives in this class, because there are more than one descendand, which will use it and
164 * they can't share it effectively.
168 function gettext_select_plural_form($count) {
169 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
170 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
171 $this->_nplurals = $nplurals;
172 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
174 return call_user_func($this->_gettext_select_plural_form, $count);
178 * @param string $header
181 function nplurals_and_expression_from_header($header) {
182 if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
183 $nplurals = (int)$matches[1];
184 $expression = trim($this->parenthesize_plural_exression($matches[2]));
185 return array($nplurals, $expression);
187 return array(2, 'n != 1');
192 * Makes a function, which will return the right translation index, according to the
193 * plural forms header
194 * @param int $nplurals
195 * @param string $expression
197 function make_plural_form_function($nplurals, $expression) {
198 $expression = str_replace('n', '$n', $expression);
200 \$index = (int)($expression);
201 return (\$index < $nplurals)? \$index : $nplurals - 1;";
202 return create_function('$n', $func_body);
206 * Adds parentheses to the inner parts of ternary operators in
207 * plural expressions, because PHP evaluates ternary oerators from left to right
209 * @param string $expression the expression without parentheses
210 * @return string the expression with parentheses added
212 function parenthesize_plural_exression($expression) {
216 for ($i = 0; $i < strlen($expression); ++$i) {
217 $char = $expression[$i];
227 $res .= str_repeat(')', $depth) . ';';
234 return rtrim($res, ';');
238 * @param string $translation
241 function make_headers($translation) {
243 // sometimes \ns are used instead of real new lines
244 $translation = str_replace('\n', "\n", $translation);
245 $lines = explode("\n", $translation);
246 foreach($lines as $line) {
247 $parts = explode(':', $line, 2);
248 if (!isset($parts[1])) continue;
249 $headers[trim($parts[0])] = trim($parts[1]);
255 * @param string $header
256 * @param string $value
258 function set_header($header, $value) {
259 parent::set_header($header, $value);
260 if ('Plural-Forms' == $header) {
261 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
262 $this->_nplurals = $nplurals;
263 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
269 if ( !class_exists( 'NOOP_Translations' ) ):
271 * Provides the same interface as Translations, but doesn't do anything
273 class NOOP_Translations {
274 var $entries = array();
275 var $headers = array();
277 function add_entry($entry) {
281 function set_header($header, $value) {
284 function set_headers($headers) {
287 function get_header($header) {
291 function translate_entry(&$entry) {
296 * @param string $singular
297 * @param string $context
299 function translate($singular, $context=null) {
303 function select_plural_form($count) {
304 return 1 == $count? 0 : 1;
307 function get_plural_forms_count() {
312 * @param string $singular
313 * @param string $plural
315 * @param string $context
317 function translate_plural($singular, $plural, $count, $context = null) {
318 return 1 == $count? $singular : $plural;
321 function merge_with(&$other) {