MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / lib / htmlpurifier / HTMLPurifier / Language.php
blobc9a3c20fe2f7efc5a58e3666a9a5ce6f819873b6
1 <?php
3 require_once 'HTMLPurifier/LanguageFactory.php';
5 class HTMLPurifier_Language
8 /**
9 * ISO 639 language code of language. Prefers shortest possible version
11 var $code = 'en';
13 /**
14 * Fallback language code
16 var $fallback = false;
18 /**
19 * Array of localizable messages
21 var $messages = array();
23 /**
24 * Array of localizable error codes
26 var $errorNames = array();
28 /**
29 * Has the language object been loaded yet?
30 * @private
32 var $_loaded = false;
34 /**
35 * Instances of HTMLPurifier_Config and HTMLPurifier_Context
37 var $config, $context;
39 function HTMLPurifier_Language($config, &$context) {
40 $this->config = $config;
41 $this->context =& $context;
44 /**
45 * Loads language object with necessary info from factory cache
46 * @note This is a lazy loader
48 function load() {
49 if ($this->_loaded) return;
50 $factory = HTMLPurifier_LanguageFactory::instance();
51 $factory->loadLanguage($this->code);
52 foreach ($factory->keys as $key) {
53 $this->$key = $factory->cache[$this->code][$key];
55 $this->_loaded = true;
58 /**
59 * Retrieves a localised message.
60 * @param $key string identifier of message
61 * @return string localised message
63 function getMessage($key) {
64 if (!$this->_loaded) $this->load();
65 if (!isset($this->messages[$key])) return "[$key]";
66 return $this->messages[$key];
69 /**
70 * Retrieves a localised error name.
71 * @param $int integer error number, corresponding to PHP's error
72 * reporting
73 * @return string localised message
75 function getErrorName($int) {
76 if (!$this->_loaded) $this->load();
77 if (!isset($this->errorNames[$int])) return "[Error: $int]";
78 return $this->errorNames[$int];
81 /**
82 * Converts an array list into a string readable representation
84 function listify($array) {
85 $sep = $this->getMessage('Item separator');
86 $sep_last = $this->getMessage('Item separator last');
87 $ret = '';
88 for ($i = 0, $c = count($array); $i < $c; $i++) {
89 if ($i == 0) {
90 } elseif ($i + 1 < $c) {
91 $ret .= $sep;
92 } else {
93 $ret .= $sep_last;
95 $ret .= $array[$i];
97 return $ret;
101 * Formats a localised message with passed parameters
102 * @param $key string identifier of message
103 * @param $args Parameters to substitute in
104 * @return string localised message
105 * @todo Implement conditionals? Right now, some messages make
106 * reference to line numbers, but those aren't always available
108 function formatMessage($key, $args = array()) {
109 if (!$this->_loaded) $this->load();
110 if (!isset($this->messages[$key])) return "[$key]";
111 $raw = $this->messages[$key];
112 $subst = array();
113 $generator = false;
114 foreach ($args as $i => $value) {
115 if (is_object($value)) {
116 if (is_a($value, 'HTMLPurifier_Token')) {
117 // factor this out some time
118 if (!$generator) $generator = $this->context->get('Generator');
119 if (isset($value->name)) $subst['$'.$i.'.Name'] = $value->name;
120 if (isset($value->data)) $subst['$'.$i.'.Data'] = $value->data;
121 $subst['$'.$i.'.Compact'] =
122 $subst['$'.$i.'.Serialized'] = $generator->generateFromToken($value);
123 // a more complex algorithm for compact representation
124 // could be introduced for all types of tokens. This
125 // may need to be factored out into a dedicated class
126 if (!empty($value->attr)) {
127 $stripped_token = $value->copy();
128 $stripped_token->attr = array();
129 $subst['$'.$i.'.Compact'] = $generator->generateFromToken($stripped_token);
131 $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown';
133 continue;
134 } elseif (is_array($value)) {
135 $keys = array_keys($value);
136 if (array_keys($keys) === $keys) {
137 // list
138 $subst['$'.$i] = $this->listify($value);
139 } else {
140 // associative array
141 // no $i implementation yet, sorry
142 $subst['$'.$i.'.Keys'] = $this->listify($keys);
143 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
145 continue;
147 $subst['$' . $i] = $value;
149 return strtr($raw, $subst);