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