3 * Class for a set of entries for translation and their associated headers
5 * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
7 * @subpackage translations
10 require_once dirname(__FILE__) . '/entry.php';
12 if ( ! class_exists( 'Translations', false ) ):
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 overridden 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;
117 function get_plural_forms_count() {
122 * @param string $singular
123 * @param string $plural
125 * @param string $context
127 function translate_plural($singular, $plural, $count, $context = null) {
128 $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context));
129 $translated = $this->translate_entry($entry);
130 $index = $this->select_plural_form($count);
131 $total_plural_forms = $this->get_plural_forms_count();
132 if ($translated && 0 <= $index && $index < $total_plural_forms &&
133 is_array($translated->translations) &&
134 isset($translated->translations[$index]))
135 return $translated->translations[$index];
137 return 1 == $count? $singular : $plural;
141 * Merge $other in the current object.
143 * @param Object &$other Another Translation object, whose translations will be merged in this one
146 function merge_with(&$other) {
147 foreach( $other->entries as $entry ) {
148 $this->entries[$entry->key()] = $entry;
153 * @param object $other
155 function merge_originals_with(&$other) {
156 foreach( $other->entries as $entry ) {
157 if ( !isset( $this->entries[$entry->key()] ) )
158 $this->entries[$entry->key()] = $entry;
160 $this->entries[$entry->key()]->merge_with($entry);
165 class Gettext_Translations extends Translations {
167 * The gettext implementation of select_plural_form.
169 * It lives in this class, because there are more than one descendand, which will use it and
170 * they can't share it effectively.
174 function gettext_select_plural_form($count) {
175 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
176 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
177 $this->_nplurals = $nplurals;
178 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
180 return call_user_func($this->_gettext_select_plural_form, $count);
184 * @param string $header
187 function nplurals_and_expression_from_header($header) {
188 if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
189 $nplurals = (int)$matches[1];
190 $expression = trim($this->parenthesize_plural_exression($matches[2]));
191 return array($nplurals, $expression);
193 return array(2, 'n != 1');
198 * Makes a function, which will return the right translation index, according to the
199 * plural forms header
200 * @param int $nplurals
201 * @param string $expression
203 function make_plural_form_function($nplurals, $expression) {
204 $expression = str_replace('n', '$n', $expression);
206 \$index = (int)($expression);
207 return (\$index < $nplurals)? \$index : $nplurals - 1;";
208 return create_function('$n', $func_body);
212 * Adds parentheses to the inner parts of ternary operators in
213 * plural expressions, because PHP evaluates ternary oerators from left to right
215 * @param string $expression the expression without parentheses
216 * @return string the expression with parentheses added
218 function parenthesize_plural_exression($expression) {
222 for ($i = 0; $i < strlen($expression); ++$i) {
223 $char = $expression[$i];
233 $res .= str_repeat(')', $depth) . ';';
240 return rtrim($res, ';');
244 * @param string $translation
247 function make_headers($translation) {
249 // sometimes \ns are used instead of real new lines
250 $translation = str_replace('\n', "\n", $translation);
251 $lines = explode("\n", $translation);
252 foreach($lines as $line) {
253 $parts = explode(':', $line, 2);
254 if (!isset($parts[1])) continue;
255 $headers[trim($parts[0])] = trim($parts[1]);
261 * @param string $header
262 * @param string $value
264 function set_header($header, $value) {
265 parent::set_header($header, $value);
266 if ('Plural-Forms' == $header) {
267 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
268 $this->_nplurals = $nplurals;
269 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
275 if ( ! class_exists( 'NOOP_Translations', false ) ):
277 * Provides the same interface as Translations, but doesn't do anything
279 class NOOP_Translations {
280 var $entries = array();
281 var $headers = array();
283 function add_entry($entry) {
289 * @param string $header
290 * @param string $value
292 function set_header($header, $value) {
297 * @param array $headers
299 function set_headers($headers) {
303 * @param string $header
306 function get_header($header) {
311 * @param Translation_Entry $entry
314 function translate_entry(&$entry) {
319 * @param string $singular
320 * @param string $context
322 function translate($singular, $context=null) {
331 function select_plural_form($count) {
332 return 1 == $count? 0 : 1;
338 function get_plural_forms_count() {
343 * @param string $singular
344 * @param string $plural
346 * @param string $context
348 function translate_plural($singular, $plural, $count, $context = null) {
349 return 1 == $count? $singular : $plural;
353 * @param object $other
355 function merge_with(&$other) {