3 * Class for a set of entries for translation and their associated headers
5 * @version $Id: translations.php 35 2009-02-16 12:54:57Z 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) {
24 if (false === $key) return false;
25 $this->entries[$key] = &$entry;
30 * Sets $header PO header to $value
32 * If the header already exists, it will be overwritten
34 * TODO: this should be out of this class, it is gettext specific
36 * @param string $header header name, without trailing :
37 * @param string $value header value, without trailing \n
39 function set_header($header, $value) {
40 $this->headers[$header] = $value;
43 function set_headers(&$headers) {
44 foreach($headers as $header => $value) {
45 $this->set_header($header, $value);
49 function get_header($header) {
50 return isset($this->headers[$header])? $this->headers[$header] : false;
53 function translate_entry(&$entry) {
55 return isset($this->entries[$key])? $this->entries[$key] : false;
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;
65 * Given the number of items, returns the 0-based index of the plural form to use
67 * Here, in the base Translations class, the commong logic for English is implmented:
68 * 0 if there is one element, 1 otherwise
70 * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
73 * @param integer $count number of items
75 function select_plural_form($count) {
76 return 1 == $count? 0 : 1;
79 function get_plural_forms_count() {
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];
93 return 1 == $count? $singular : $plural;
97 * The gettext implmentation of select_plural_form.
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.
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);
108 return call_user_func($this->_gettext_select_plural_form, $count);
112 * Makes a function, which will return the right translation index, according to the
113 * plural forms header
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);
123 \$index = (int)($plural_expr);
124 return (\$index < $nplurals)? \$index : $nplurals - 1;";
125 $res = create_function('$n', $func_body);
131 * Adds parantheses to the inner parts of ternary operators in
132 * plural expressions, because PHP evaluates ternary oerators from left to right
134 * @param string $expression the expression without parentheses
135 * @return string the expression with parentheses added
137 function _parenthesize_plural_exression($expression) {
141 for ($i = 0; $i < strlen($expression); ++$i) {
142 $char = $expression[$i];
152 $res .= str_repeat(')', $depth) . ';';
159 return rtrim($res, ';');
163 * Merge $other in the current object.
165 * @param Object &$other Another Translation object, whose translations will be merged in this one
168 function merge_with(&$other) {
169 $this->entries = array_merge($this->entries, $other->entries);