]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/htmlpurifier/HTMLPurifier/Language.php
remove autoload section in composer.json
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / Language.php
CommitLineData
d4949327
NL
1<?php\r
2\r
3/**\r
4 * Represents a language and defines localizable string formatting and\r
5 * other functions, as well as the localized messages for HTML Purifier.\r
6 */\r
7class HTMLPurifier_Language\r
8{\r
9\r
10 /**\r
11 * ISO 639 language code of language. Prefers shortest possible version.\r
12 * @type string\r
13 */\r
14 public $code = 'en';\r
15\r
16 /**\r
17 * Fallback language code.\r
18 * @type bool|string\r
19 */\r
20 public $fallback = false;\r
21\r
22 /**\r
23 * Array of localizable messages.\r
24 * @type array\r
25 */\r
26 public $messages = array();\r
27\r
28 /**\r
29 * Array of localizable error codes.\r
30 * @type array\r
31 */\r
32 public $errorNames = array();\r
33\r
34 /**\r
35 * True if no message file was found for this language, so English\r
36 * is being used instead. Check this if you'd like to notify the\r
37 * user that they've used a non-supported language.\r
38 * @type bool\r
39 */\r
40 public $error = false;\r
41\r
42 /**\r
43 * Has the language object been loaded yet?\r
44 * @type bool\r
45 * @todo Make it private, fix usage in HTMLPurifier_LanguageTest\r
46 */\r
47 public $_loaded = false;\r
48\r
49 /**\r
50 * @type HTMLPurifier_Config\r
51 */\r
52 protected $config;\r
53\r
54 /**\r
55 * @type HTMLPurifier_Context\r
56 */\r
57 protected $context;\r
58\r
59 /**\r
60 * @param HTMLPurifier_Config $config\r
61 * @param HTMLPurifier_Context $context\r
62 */\r
63 public function __construct($config, $context)\r
64 {\r
65 $this->config = $config;\r
66 $this->context = $context;\r
67 }\r
68\r
69 /**\r
70 * Loads language object with necessary info from factory cache\r
71 * @note This is a lazy loader\r
72 */\r
73 public function load()\r
74 {\r
75 if ($this->_loaded) {\r
76 return;\r
77 }\r
78 $factory = HTMLPurifier_LanguageFactory::instance();\r
79 $factory->loadLanguage($this->code);\r
80 foreach ($factory->keys as $key) {\r
81 $this->$key = $factory->cache[$this->code][$key];\r
82 }\r
83 $this->_loaded = true;\r
84 }\r
85\r
86 /**\r
87 * Retrieves a localised message.\r
88 * @param string $key string identifier of message\r
89 * @return string localised message\r
90 */\r
91 public function getMessage($key)\r
92 {\r
93 if (!$this->_loaded) {\r
94 $this->load();\r
95 }\r
96 if (!isset($this->messages[$key])) {\r
97 return "[$key]";\r
98 }\r
99 return $this->messages[$key];\r
100 }\r
101\r
102 /**\r
103 * Retrieves a localised error name.\r
104 * @param int $int error number, corresponding to PHP's error reporting\r
105 * @return string localised message\r
106 */\r
107 public function getErrorName($int)\r
108 {\r
109 if (!$this->_loaded) {\r
110 $this->load();\r
111 }\r
112 if (!isset($this->errorNames[$int])) {\r
113 return "[Error: $int]";\r
114 }\r
115 return $this->errorNames[$int];\r
116 }\r
117\r
118 /**\r
119 * Converts an array list into a string readable representation\r
120 * @param array $array\r
121 * @return string\r
122 */\r
123 public function listify($array)\r
124 {\r
125 $sep = $this->getMessage('Item separator');\r
126 $sep_last = $this->getMessage('Item separator last');\r
127 $ret = '';\r
128 for ($i = 0, $c = count($array); $i < $c; $i++) {\r
129 if ($i == 0) {\r
130 } elseif ($i + 1 < $c) {\r
131 $ret .= $sep;\r
132 } else {\r
133 $ret .= $sep_last;\r
134 }\r
135 $ret .= $array[$i];\r
136 }\r
137 return $ret;\r
138 }\r
139\r
140 /**\r
141 * Formats a localised message with passed parameters\r
142 * @param string $key string identifier of message\r
143 * @param array $args Parameters to substitute in\r
144 * @return string localised message\r
145 * @todo Implement conditionals? Right now, some messages make\r
146 * reference to line numbers, but those aren't always available\r
147 */\r
148 public function formatMessage($key, $args = array())\r
149 {\r
150 if (!$this->_loaded) {\r
151 $this->load();\r
152 }\r
153 if (!isset($this->messages[$key])) {\r
154 return "[$key]";\r
155 }\r
156 $raw = $this->messages[$key];\r
157 $subst = array();\r
158 $generator = false;\r
159 foreach ($args as $i => $value) {\r
160 if (is_object($value)) {\r
161 if ($value instanceof HTMLPurifier_Token) {\r
162 // factor this out some time\r
163 if (!$generator) {\r
164 $generator = $this->context->get('Generator');\r
165 }\r
166 if (isset($value->name)) {\r
167 $subst['$'.$i.'.Name'] = $value->name;\r
168 }\r
169 if (isset($value->data)) {\r
170 $subst['$'.$i.'.Data'] = $value->data;\r
171 }\r
172 $subst['$'.$i.'.Compact'] =\r
173 $subst['$'.$i.'.Serialized'] = $generator->generateFromToken($value);\r
174 // a more complex algorithm for compact representation\r
175 // could be introduced for all types of tokens. This\r
176 // may need to be factored out into a dedicated class\r
177 if (!empty($value->attr)) {\r
178 $stripped_token = clone $value;\r
179 $stripped_token->attr = array();\r
180 $subst['$'.$i.'.Compact'] = $generator->generateFromToken($stripped_token);\r
181 }\r
182 $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown';\r
183 }\r
184 continue;\r
185 } elseif (is_array($value)) {\r
186 $keys = array_keys($value);\r
187 if (array_keys($keys) === $keys) {\r
188 // list\r
189 $subst['$'.$i] = $this->listify($value);\r
190 } else {\r
191 // associative array\r
192 // no $i implementation yet, sorry\r
193 $subst['$'.$i.'.Keys'] = $this->listify($keys);\r
194 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));\r
195 }\r
196 continue;\r
197 }\r
198 $subst['$' . $i] = $value;\r
199 }\r
200 return strtr($raw, $subst);\r
201 }\r
202}\r
203\r
204// vim: et sw=4 sts=4\r