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 * 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.
36 * Has the language object been loaded yet?
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;
52 * Loads language object with necessary info from factory cache
53 * @note This is a lazy loader
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;
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];
77 * Retrieves a localised error name.
78 * @param $int integer error number, corresponding to PHP's error
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];
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');
95 for ($i = 0, $c = count($array); $i < $c; $i++
) {
97 } elseif ($i +
1 < $c) {
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];
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';
141 } elseif (is_array($value)) {
142 $keys = array_keys($value);
143 if (array_keys($keys) === $keys) {
145 $subst['$'.$i] = $this->listify($value);
148 // no $i implementation yet, sorry
149 $subst['$'.$i.'.Keys'] = $this->listify($keys);
150 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
154 $subst['$' . $i] = $value;
156 return strtr($raw, $subst);