3 require_once 'HTMLPurifier/LanguageFactory.php';
5 class HTMLPurifier_Language
9 * ISO 639 language code of language. Prefers shortest possible version
14 * Fallback language code
16 var $fallback = false;
19 * Array of localizable messages
21 var $messages = array();
24 * Array of localizable error codes
26 var $errorNames = array();
29 * Has the language object been loaded yet?
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;
45 * Loads language object with necessary info from factory cache
46 * @note This is a lazy loader
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;
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];
70 * Retrieves a localised error name.
71 * @param $int integer error number, corresponding to PHP's error
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];
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');
88 for ($i = 0, $c = count($array); $i < $c; $i++
) {
90 } elseif ($i +
1 < $c) {
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];
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';
134 } elseif (is_array($value)) {
135 $keys = array_keys($value);
136 if (array_keys($keys) === $keys) {
138 $subst['$'.$i] = $this->listify($value);
141 // no $i implementation yet, sorry
142 $subst['$'.$i.'.Keys'] = $this->listify($keys);
143 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
147 $subst['$' . $i] = $value;
149 return strtr($raw, $subst);