Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Language.php
blobc0833b7f7945e830e369071f042058c88d782367
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 * True if no message file was found for this language, so English
30 * is being used instead. Check this if you'd like to notify the
31 * user that they've used a non-supported language.
33 var $error = false;
35 /**
36 * Has the language object been loaded yet?
37 * @private
39 var $_loaded = false;
41 /**
42 * Instances of HTMLPurifier_Config and HTMLPurifier_Context
44 var $config, $context;
46 function HTMLPurifier_Language($config, &$context) {
47 $this->config = $config;
48 $this->context =& $context;
51 /**
52 * Loads language object with necessary info from factory cache
53 * @note This is a lazy loader
55 function load() {
56 if ($this->_loaded) return;
57 $factory = HTMLPurifier_LanguageFactory::instance();
58 $factory->loadLanguage($this->code);
59 foreach ($factory->keys as $key) {
60 $this->$key = $factory->cache[$this->code][$key];
62 $this->_loaded = true;
65 /**
66 * Retrieves a localised message.
67 * @param $key string identifier of message
68 * @return string localised message
70 function getMessage($key) {
71 if (!$this->_loaded) $this->load();
72 if (!isset($this->messages[$key])) return "[$key]";
73 return $this->messages[$key];
76 /**
77 * Retrieves a localised error name.
78 * @param $int integer error number, corresponding to PHP's error
79 * reporting
80 * @return string localised message
82 function getErrorName($int) {
83 if (!$this->_loaded) $this->load();
84 if (!isset($this->errorNames[$int])) return "[Error: $int]";
85 return $this->errorNames[$int];
88 /**
89 * Converts an array list into a string readable representation
91 function listify($array) {
92 $sep = $this->getMessage('Item separator');
93 $sep_last = $this->getMessage('Item separator last');
94 $ret = '';
95 for ($i = 0, $c = count($array); $i < $c; $i++) {
96 if ($i == 0) {
97 } elseif ($i + 1 < $c) {
98 $ret .= $sep;
99 } else {
100 $ret .= $sep_last;
102 $ret .= $array[$i];
104 return $ret;
108 * Formats a localised message with passed parameters
109 * @param $key string identifier of message
110 * @param $args Parameters to substitute in
111 * @return string localised message
112 * @todo Implement conditionals? Right now, some messages make
113 * reference to line numbers, but those aren't always available
115 function formatMessage($key, $args = array()) {
116 if (!$this->_loaded) $this->load();
117 if (!isset($this->messages[$key])) return "[$key]";
118 $raw = $this->messages[$key];
119 $subst = array();
120 $generator = false;
121 foreach ($args as $i => $value) {
122 if (is_object($value)) {
123 if (is_a($value, 'HTMLPurifier_Token')) {
124 // factor this out some time
125 if (!$generator) $generator = $this->context->get('Generator');
126 if (isset($value->name)) $subst['$'.$i.'.Name'] = $value->name;
127 if (isset($value->data)) $subst['$'.$i.'.Data'] = $value->data;
128 $subst['$'.$i.'.Compact'] =
129 $subst['$'.$i.'.Serialized'] = $generator->generateFromToken($value);
130 // a more complex algorithm for compact representation
131 // could be introduced for all types of tokens. This
132 // may need to be factored out into a dedicated class
133 if (!empty($value->attr)) {
134 $stripped_token = $value->copy();
135 $stripped_token->attr = array();
136 $subst['$'.$i.'.Compact'] = $generator->generateFromToken($stripped_token);
138 $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown';
140 continue;
141 } elseif (is_array($value)) {
142 $keys = array_keys($value);
143 if (array_keys($keys) === $keys) {
144 // list
145 $subst['$'.$i] = $this->listify($value);
146 } else {
147 // associative array
148 // no $i implementation yet, sorry
149 $subst['$'.$i.'.Keys'] = $this->listify($keys);
150 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
152 continue;
154 $subst['$' . $i] = $value;
156 return strtr($raw, $subst);