4 * Represents a language and defines localizable string formatting and
5 * other functions, as well as the localized messages for HTML Purifier.
7 class HTMLPurifier_Language
11 * ISO 639 language code of language. Prefers shortest possible version.
17 * Fallback language code.
20 public $fallback = false;
23 * Array of localizable messages.
26 public $messages = array();
29 * Array of localizable error codes.
32 public $errorNames = array();
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.
40 public $error = false;
43 * Has the language object been loaded yet?
45 * @todo Make it private, fix usage in HTMLPurifier_LanguageTest
47 public $_loaded = false;
50 * @type HTMLPurifier_Config
55 * @type HTMLPurifier_Context
60 * @param HTMLPurifier_Config $config
61 * @param HTMLPurifier_Context $context
63 public function __construct($config, $context)
65 $this->config
= $config;
66 $this->context
= $context;
70 * Loads language object with necessary info from factory cache
71 * @note This is a lazy loader
73 public function load()
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;
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
) {
96 if (!isset($this->messages
[$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
) {
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
123 public function listify($array)
125 $sep = $this->getMessage('Item separator');
126 $sep_last = $this->getMessage('Item separator last');
128 for ($i = 0, $c = count($array); $i < $c; $i++
) {
130 } elseif ($i +
1 < $c) {
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
) {
153 if (!isset($this->messages
[$key])) {
156 $raw = $this->messages
[$key];
159 foreach ($args as $i => $value) {
160 if (is_object($value)) {
161 if ($value instanceof HTMLPurifier_Token
) {
162 // factor this out some time
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';
185 } elseif (is_array($value)) {
186 $keys = array_keys($value);
187 if (array_keys($keys) === $keys) {
189 $subst['$'.$i] = $this->listify($value);
192 // no $i implementation yet, sorry
193 $subst['$'.$i.'.Keys'] = $this->listify($keys);
194 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
198 $subst['$' . $i] = $value;
200 return strtr($raw, $subst);
204 // vim: et sw=4 sts=4