]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pomo/translations.php
Wordpress 2.8
[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 35 2009-02-16 12:54:57Z 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                 $key = $entry->key();
24                 if (false === $key) return false;
25                 $this->entries[$key] = &$entry;
26                 return true;
27         }
28
29         /**
30          * Sets $header PO header to $value
31          *
32          * If the header already exists, it will be overwritten
33          *
34          * TODO: this should be out of this class, it is gettext specific
35          *
36          * @param string $header header name, without trailing :
37          * @param string $value header value, without trailing \n
38          */
39         function set_header($header, $value) {
40                 $this->headers[$header] = $value;
41         }
42
43         function set_headers(&$headers) {
44                 foreach($headers as $header => $value) {
45                         $this->set_header($header, $value);
46                 }
47         }
48
49         function get_header($header) {
50                 return isset($this->headers[$header])? $this->headers[$header] : false;
51         }
52
53         function translate_entry(&$entry) {
54                 $key = $entry->key();
55                 return isset($this->entries[$key])? $this->entries[$key] : false;
56         }
57
58         function translate($singular, $context=null) {
59                 $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context));
60                 $translated = $this->translate_entry($entry);
61                 return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular;
62         }
63
64         /**
65          * Given the number of items, returns the 0-based index of the plural form to use
66          *
67          * Here, in the base Translations class, the commong logic for English is implmented:
68          *      0 if there is one element, 1 otherwise
69          *
70          * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
71          * from their headers.
72          *
73          * @param integer $count number of items
74          */
75         function select_plural_form($count) {
76                 return 1 == $count? 0 : 1;
77         }
78
79         function get_plural_forms_count() {
80                 return 2;
81         }
82
83         function translate_plural($singular, $plural, $count, $context = null) {
84                 $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context));
85                 $translated = $this->translate_entry($entry);
86                 $index = $this->select_plural_form($count);
87                 $total_plural_forms = $this->get_plural_forms_count();
88                 if ($translated && 0 <= $index && $index < $total_plural_forms &&
89                                 is_array($translated->translations) &&
90                                 count($translated->translations) == $total_plural_forms)
91                         return $translated->translations[$index];
92                 else
93                         return 1 == $count? $singular : $plural;
94         }
95
96         /**
97          * The gettext implmentation of select_plural_form.
98          *
99          * It lives in this class, because there are more than one descendand, which will use it and
100          * they can't share it effectively.
101          *
102          */
103         function gettext_select_plural_form($count) {
104                 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
105                         $plural_header = $this->get_header('Plural-Forms');
106                         $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($plural_header);
107                 }
108                 return call_user_func($this->_gettext_select_plural_form, $count);
109         }
110
111         /**
112          * Makes a function, which will return the right translation index, according to the
113          * plural forms header
114          */
115         function _make_gettext_select_plural_form($plural_header) {
116                 $res = create_function('$count', 'return 1 == $count? 0 : 1;');
117                 if ($plural_header && (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $plural_header, $matches))) {
118                         $nplurals = (int)$matches[1];
119                         $this->_nplurals = $nplurals;
120                         $plural_expr = trim($this->_parenthesize_plural_exression($matches[2]));
121                         $plural_expr = str_replace('n', '$n', $plural_expr);
122                         $func_body = "
123                                 \$index = (int)($plural_expr);
124                                 return (\$index < $nplurals)? \$index : $nplurals - 1;";
125                         $res = create_function('$n', $func_body);
126                 }
127                 return $res;
128         }
129
130         /**
131          * Adds parantheses to the inner parts of ternary operators in
132          * plural expressions, because PHP evaluates ternary oerators from left to right
133          *
134          * @param string $expression the expression without parentheses
135          * @return string the expression with parentheses added
136          */
137         function _parenthesize_plural_exression($expression) {
138                 $expression .= ';';
139                 $res = '';
140                 $depth = 0;
141                 for ($i = 0; $i < strlen($expression); ++$i) {
142                         $char = $expression[$i];
143                         switch ($char) {
144                                 case '?':
145                                         $res .= ' ? (';
146                                         $depth++;
147                                         break;
148                                 case ':':
149                                         $res .= ') : (';
150                                         break;
151                                 case ';':
152                                         $res .= str_repeat(')', $depth) . ';';
153                                         $depth= 0;
154                                         break;
155                                 default:
156                                         $res .= $char;
157                         }
158                 }
159                 return rtrim($res, ';');
160         }
161
162         /**
163          * Merge $other in the current object.
164          *
165          * @param Object &$other Another Translation object, whose translations will be merged in this one
166          * @return void
167          **/
168         function merge_with(&$other) {
169                 $this->entries = array_merge($this->entries, $other->entries);
170         }
171 }
172
173 ?>