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 object &$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;
33 function add_entry_or_merge($entry) {
34 if (is_array($entry)) {
35 $entry = new Translation_Entry($entry);
38 if (false === $key) return false;
39 if (isset($this->entries[$key]))
40 $this->entries[$key]->merge_with($entry);
42 $this->entries[$key] = &$entry;
47 * Sets $header PO header to $value
49 * If the header already exists, it will be overwritten
51 * TODO: this should be out of this class, it is gettext specific
53 * @param string $header header name, without trailing :
54 * @param string $value header value, without trailing \n
56 function set_header($header, $value) {
57 $this->headers[$header] = $value;
60 function set_headers(&$headers) {
61 foreach($headers as $header => $value) {
62 $this->set_header($header, $value);
66 function get_header($header) {
67 return isset($this->headers[$header])? $this->headers[$header] : false;
70 function translate_entry(&$entry) {
72 return isset($this->entries[$key])? $this->entries[$key] : false;
75 function translate($singular, $context=null) {
76 $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context));
77 $translated = $this->translate_entry($entry);
78 return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular;
82 * Given the number of items, returns the 0-based index of the plural form to use
84 * Here, in the base Translations class, the common logic for English is implemented:
85 * 0 if there is one element, 1 otherwise
87 * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
90 * @param integer $count number of items
92 function select_plural_form($count) {
93 return 1 == $count? 0 : 1;
96 function get_plural_forms_count() {
100 function translate_plural($singular, $plural, $count, $context = null) {
101 $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context));
102 $translated = $this->translate_entry($entry);
103 $index = $this->select_plural_form($count);
104 $total_plural_forms = $this->get_plural_forms_count();
105 if ($translated && 0 <= $index && $index < $total_plural_forms &&
106 is_array($translated->translations) &&
107 isset($translated->translations[$index]))
108 return $translated->translations[$index];
110 return 1 == $count? $singular : $plural;
114 * Merge $other in the current object.
116 * @param Object &$other Another Translation object, whose translations will be merged in this one
119 function merge_with(&$other) {
120 foreach( $other->entries as $entry ) {
121 $this->entries[$entry->key()] = $entry;
125 function merge_originals_with(&$other) {
126 foreach( $other->entries as $entry ) {
127 if ( !isset( $this->entries[$entry->key()] ) )
128 $this->entries[$entry->key()] = $entry;
130 $this->entries[$entry->key()]->merge_with($entry);
135 class Gettext_Translations extends Translations {
137 * The gettext implementation of select_plural_form.
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.
143 function gettext_select_plural_form($count) {
144 if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
145 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
146 $this->_nplurals = $nplurals;
147 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
149 return call_user_func($this->_gettext_select_plural_form, $count);
152 function nplurals_and_expression_from_header($header) {
153 if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
154 $nplurals = (int)$matches[1];
155 $expression = trim($this->parenthesize_plural_exression($matches[2]));
156 return array($nplurals, $expression);
158 return array(2, 'n != 1');
163 * Makes a function, which will return the right translation index, according to the
164 * plural forms header
166 function make_plural_form_function($nplurals, $expression) {
167 $expression = str_replace('n', '$n', $expression);
169 \$index = (int)($expression);
170 return (\$index < $nplurals)? \$index : $nplurals - 1;";
171 return create_function('$n', $func_body);
175 * Adds parantheses to the inner parts of ternary operators in
176 * plural expressions, because PHP evaluates ternary oerators from left to right
178 * @param string $expression the expression without parentheses
179 * @return string the expression with parentheses added
181 function parenthesize_plural_exression($expression) {
185 for ($i = 0; $i < strlen($expression); ++$i) {
186 $char = $expression[$i];
196 $res .= str_repeat(')', $depth) . ';';
203 return rtrim($res, ';');
206 function make_headers($translation) {
208 // sometimes \ns are used instead of real new lines
209 $translation = str_replace('\n', "\n", $translation);
210 $lines = explode("\n", $translation);
211 foreach($lines as $line) {
212 $parts = explode(':', $line, 2);
213 if (!isset($parts[1])) continue;
214 $headers[trim($parts[0])] = trim($parts[1]);
219 function set_header($header, $value) {
220 parent::set_header($header, $value);
221 if ('Plural-Forms' == $header) {
222 list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
223 $this->_nplurals = $nplurals;
224 $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
230 if ( !class_exists( 'NOOP_Translations' ) ):
232 * Provides the same interface as Translations, but doesn't do anything
234 class NOOP_Translations {
235 var $entries = array();
236 var $headers = array();
238 function add_entry($entry) {
242 function set_header($header, $value) {
245 function set_headers(&$headers) {
248 function get_header($header) {
252 function translate_entry(&$entry) {
256 function translate($singular, $context=null) {
260 function select_plural_form($count) {
261 return 1 == $count? 0 : 1;
264 function get_plural_forms_count() {
268 function translate_plural($singular, $plural, $count, $context = null) {
269 return 1 == $count? $singular : $plural;
272 function merge_with(&$other) {