]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pomo/translations.php
Wizard 2.8.2-scripts
[autoinstalls/wordpress.git] / wp-includes / pomo / translations.php
1 <?php
2 /**
3  * Class for a set of entries for translation and their associated headers
4  *
5  * @version $Id: translations.php 114 2009-05-11 17:30:38Z nbachiyski $
6  * @package pomo
7  * @subpackage translations
8  */
9
10 require_once dirname(__FILE__) . '/entry.php';
11
12 class Translations {
13         var $entries = array();
14         var $headers = array();
15
16         /**
17          * Add entry to the PO structure
18          *
19          * @param object &$entry
20          * @return bool true on success, false if the entry doesn't have a key
21          */
22         function add_entry($entry) {
23                 if (is_array($entry)) {
24                         $entry = new Translation_Entry($entry);
25                 }
26                 $key = $entry->key();
27                 if (false === $key) return false;
28                 $this->entries[$key] = $entry;
29                 return true;
30         }
31
32         /**
33          * Sets $header PO header to $value
34          *
35          * If the header already exists, it will be overwritten
36          *
37          * TODO: this should be out of this class, it is gettext specific
38          *
39          * @param string $header header name, without trailing :
40          * @param string $value header value, without trailing \n
41          */
42         function set_header($header, $value) {
43                 $this->headers[$header] = $value;
44         }
45
46         function set_headers(&$headers) {
47                 foreach($headers as $header => $value) {
48                         $this->set_header($header, $value);
49                 }
50         }
51
52         function get_header($header) {
53                 return isset($this->headers[$header])? $this->headers[$header] : false;
54         }
55
56         function translate_entry(&$entry) {
57                 $key = $entry->key();
58                 return isset($this->entries[$key])? $this->entries[$key] : false;
59         }
60
61         function translate($singular, $context=null) {
62                 $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context));
63                 $translated = $this->translate_entry($entry);
64                 return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular;
65         }
66
67         /**
68          * Given the number of items, returns the 0-based index of the plural form to use
69          *
70          * Here, in the base Translations class, the commong logic for English is implmented:
71          *      0 if there is one element, 1 otherwise
72          *
73          * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
74          * from their headers.
75          *
76          * @param integer $count number of items
77          */
78         function select_plural_form($count) {
79                 return 1 == $count? 0 : 1;
80         }
81
82         function get_plural_forms_count() {
83                 return 2;
84         }
85
86         function translate_plural($singular, $plural, $count, $context = null) {
87                 $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context));
88                 $translated = $this->translate_entry($entry);
89                 $index = $this->select_plural_form($count);
90                 $total_plural_forms = $this->get_plural_forms_count();
91                 if ($translated && 0 <= $index && $index < $total_plural_forms &&
92                                 is_array($translated->translations) &&
93                                 isset($translated->translations[$index]))
94                         return $translated->translations[$index];
95                 else
96                         return 1 == $count? $singular : $plural;
97         }
98
99         /**
100          * Merge $other in the current object.
101          *
102          * @param Object &$other Another Translation object, whose translations will be merged in this one
103          * @return void
104          **/
105         function merge_with(&$other) {
106                 $this->entries = array_merge($this->entries, $other->entries);
107         }
108 }
109
110 class Gettext_Translations extends Translations {
111         /**
112          * The gettext implmentation of select_plural_form.
113          *
114          * It lives in this class, because there are more than one descendand, which will use it and
115          * they can't share it effectively.
116          *
117          */
118         function gettext_select_plural_form($count) {
119                 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);
122                 }
123                 return call_user_func($this->_gettext_select_plural_form, $count);
124         }
125
126         /**
127          * Makes a function, which will return the right translation index, according to the
128          * plural forms header
129          */
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;
143         }
144
145         /**
146          * Adds parantheses to the inner parts of ternary operators in
147          * plural expressions, because PHP evaluates ternary oerators from left to right
148          * 
149          * @param string $expression the expression without parentheses
150          * @return string the expression with parentheses added
151          */
152         function _parenthesize_plural_exression($expression) {
153                 $expression .= ';';
154                 $res = '';
155                 $depth = 0;
156                 for ($i = 0; $i < strlen($expression); ++$i) {
157                         $char = $expression[$i];
158                         switch ($char) {
159                                 case '?':
160                                         $res .= ' ? (';
161                                         $depth++;
162                                         break;
163                                 case ':':
164                                         $res .= ') : (';
165                                         break;
166                                 case ';':
167                                         $res .= str_repeat(')', $depth) . ';';
168                                         $depth= 0;
169                                         break;
170                                 default:
171                                         $res .= $char;
172                         }
173                 }
174                 return rtrim($res, ';');
175         }
176         
177         function make_headers($translation) {
178                 $headers = array();
179                 // sometimes \ns are used instead of real new lines
180                 $translation = str_replace('\n', "\n", $translation);
181                 $lines = explode("\n", $translation);
182                 foreach($lines as $line) {
183                         $parts = explode(':', $line, 2);
184                         if (!isset($parts[1])) continue;
185                         $headers[trim($parts[0])] = trim($parts[1]);
186                 }
187                 return $headers;
188         }
189
190         function set_header($header, $value) {
191                 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 }
198
199 ?>