]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/pomo/entry.php
Translation API! zomigod. First pass. See Issue 52.
[Github/YOURLS.git] / includes / pomo / entry.php
1 <?php\r
2 /**\r
3  * Contains Translation_Entry class\r
4  *\r
5  * @version $Id: entry.php 718 2012-10-31 00:32:02Z nbachiyski $\r
6  * @package pomo\r
7  * @subpackage entry\r
8  */\r
9 \r
10 if ( !class_exists( 'Translation_Entry' ) ):\r
11 /**\r
12  * Translation_Entry class encapsulates a translatable string\r
13  */\r
14 class Translation_Entry {\r
15 \r
16         /**\r
17          * Whether the entry contains a string and its plural form, default is false\r
18          *\r
19          * @var boolean\r
20          */\r
21         var $is_plural = false;\r
22 \r
23         var $context = null;\r
24         var $singular = null;\r
25         var $plural = null;\r
26         var $translations = array();\r
27         var $translator_comments = '';\r
28         var $extracted_comments = '';\r
29         var $references = array();\r
30         var $flags = array();\r
31 \r
32         /**\r
33          * @param array $args associative array, support following keys:\r
34          *      - singular (string) -- the string to translate, if omitted and empty entry will be created\r
35          *      - plural (string) -- the plural form of the string, setting this will set {@link $is_plural} to true\r
36          *      - translations (array) -- translations of the string and possibly -- its plural forms\r
37          *      - context (string) -- a string differentiating two equal strings used in different contexts\r
38          *      - translator_comments (string) -- comments left by translators\r
39          *      - extracted_comments (string) -- comments left by developers\r
40          *      - references (array) -- places in the code this strings is used, in relative_to_root_path/file.php:linenum form\r
41          *      - flags (array) -- flags like php-format\r
42          */\r
43         function Translation_Entry($args=array()) {\r
44                 // if no singular -- empty object\r
45                 if (!isset($args['singular'])) {\r
46                         return;\r
47                 }\r
48                 // get member variable values from args hash\r
49                 foreach ($args as $varname => $value) {\r
50                         $this->$varname = $value;\r
51                 }\r
52                 if (isset($args['plural'])) $this->is_plural = true;\r
53                 if (!is_array($this->translations)) $this->translations = array();\r
54                 if (!is_array($this->references)) $this->references = array();\r
55                 if (!is_array($this->flags)) $this->flags = array();\r
56         }\r
57 \r
58         /**\r
59          * Generates a unique key for this entry\r
60          *\r
61          * @return string|bool the key or false if the entry is empty\r
62          */\r
63         function key() {\r
64                 if (is_null($this->singular)) return false;\r
65                 // prepend context and EOT, like in MO files\r
66                 return is_null($this->context)? $this->singular : $this->context.chr(4).$this->singular;\r
67         }\r
68 \r
69         function merge_with(&$other) {\r
70                 $this->flags = array_unique( array_merge( $this->flags, $other->flags ) );\r
71                 $this->references = array_unique( array_merge( $this->references, $other->references ) );\r
72                 if ( $this->extracted_comments != $other->extracted_comments ) {\r
73                         $this->extracted_comments .= $other->extracted_comments;\r
74                 }\r
75 \r
76         }\r
77 }\r
78 endif;