3 * Class for a set of entries for translation and their associated headers
5 * @version $Id: translations.php 114 2009-05-11 17:30:38Z nbachiyski $
7 * @subpackage translations
10 require_once dirname(__FILE__) . '/entry.php';
13 var $entries = array();
14 var $headers = array();
17 * Add entry to the PO structure
19 * @param object &$entry
20 * @return bool true on success, false if the entry doesn't have a key
22 function add_entry($entry) {
23 if (is_array($entry)) {
24 $entry = new Translation_Entry($entry);
27 if (false === $key) return false;
28 $this->entries[$key] = $entry;
33 * Sets $header PO header to $value
35 * If the header already exists, it will be overwritten
37 * TODO: this should be out of this class, it is gettext specific
39 * @param string $header header name, without trailing :
40 * @param string $value header value, without trailing \n
42 function set_header($header, $value) {
43 $this->headers[$header] = $value;
46 function set_headers(&$headers) {
47 foreach($headers as $header => $value) {
48 $this->set_header($header, $value);
52 function get_header($header) {
53 return isset($this->headers[$header])? $this->headers[$header] : false;
56 function translate_entry(&$entry) {
58 return isset($this->entries[$key])? $this->entries[$key] : false;
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;
68 * Given the number of items, returns the 0-based index of the plural form to use
70 * Here, in the base Translations class, the commong logic for English is implmented:
71 * 0 if there is one element, 1 otherwise
73 * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
76 * @param integer $count number of items
78 function select_plural_form($count) {
79 return 1 == $count? 0 : 1;
82 function get_plural_forms_count() {
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];
96 return 1 == $count? $singular : $plural;
100 * Merge $other in the current object.
102 * @param Object &$other Another Translation object, whose translations will be merged in this one
105 function merge_with(&$other) {
106 $this->entries = array_merge($this->entries, $other->entries);
110 class Gettext_Translations extends Translations {
112 * The gettext implmentation of select_plural_form.
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.
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);
123 return call_user_func($this->_gettext_select_plural_form, $count);
127 * Makes a function, which will return the right translation index, according to the
128 * plural forms header
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);
138 \$index = (int)($plural_expr);
139 return (\$index < $nplurals)? \$index : $nplurals - 1;";
140 $res = create_function('$n', $func_body);
146 * Adds parantheses to the inner parts of ternary operators in
147 * plural expressions, because PHP evaluates ternary oerators from left to right
149 * @param string $expression the expression without parentheses
150 * @return string the expression with parentheses added
152 function _parenthesize_plural_exression($expression) {
156 for ($i = 0; $i < strlen($expression); ++$i) {
157 $char = $expression[$i];
167 $res .= str_repeat(')', $depth) . ';';
174 return rtrim($res, ';');
177 function make_headers($translation) {
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]);
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);