ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Language.php
Go to the documentation of this file.
1<?php
2
8{
9
14 public $code = 'en';
15
20 public $fallback = false;
21
26 public $messages = array();
27
32 public $errorNames = array();
33
40 public $error = false;
41
47 public $_loaded = false;
48
52 protected $config;
53
57 protected $context;
58
63 public function __construct($config, $context)
64 {
65 $this->config = $config;
66 $this->context = $context;
67 }
68
73 public function load()
74 {
75 if ($this->_loaded) {
76 return;
77 }
79 $factory->loadLanguage($this->code);
80 foreach ($factory->keys as $key) {
81 $this->$key = $factory->cache[$this->code][$key];
82 }
83 $this->_loaded = true;
84 }
85
91 public function getMessage($key)
92 {
93 if (!$this->_loaded) {
94 $this->load();
95 }
96 if (!isset($this->messages[$key])) {
97 return "[$key]";
98 }
99 return $this->messages[$key];
100 }
101
107 public function getErrorName($int)
108 {
109 if (!$this->_loaded) {
110 $this->load();
111 }
112 if (!isset($this->errorNames[$int])) {
113 return "[Error: $int]";
114 }
115 return $this->errorNames[$int];
116 }
117
123 public function listify($array)
124 {
125 $sep = $this->getMessage('Item separator');
126 $sep_last = $this->getMessage('Item separator last');
127 $ret = '';
128 for ($i = 0, $c = count($array); $i < $c; $i++) {
129 if ($i == 0) {
130 } elseif ($i + 1 < $c) {
131 $ret .= $sep;
132 } else {
133 $ret .= $sep_last;
134 }
135 $ret .= $array[$i];
136 }
137 return $ret;
138 }
139
148 public function formatMessage($key, $args = array())
149 {
150 if (!$this->_loaded) {
151 $this->load();
152 }
153 if (!isset($this->messages[$key])) {
154 return "[$key]";
155 }
156 $raw = $this->messages[$key];
157 $subst = array();
158 $generator = false;
159 foreach ($args as $i => $value) {
160 if (is_object($value)) {
161 if ($value instanceof HTMLPurifier_Token) {
162 // factor this out some time
163 if (!$generator) {
164 $generator = $this->context->get('Generator');
165 }
166 if (isset($value->name)) {
167 $subst['$'.$i.'.Name'] = $value->name;
168 }
169 if (isset($value->data)) {
170 $subst['$'.$i.'.Data'] = $value->data;
171 }
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);
181 }
182 $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown';
183 }
184 continue;
185 } elseif (is_array($value)) {
186 $keys = array_keys($value);
187 if (array_keys($keys) === $keys) {
188 // list
189 $subst['$'.$i] = $this->listify($value);
190 } else {
191 // associative array
192 // no $i implementation yet, sorry
193 $subst['$'.$i.'.Keys'] = $this->listify($keys);
194 $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
195 }
196 continue;
197 }
198 $subst['$' . $i] = $value;
199 }
200 return strtr($raw, $subst);
201 }
202}
203
204// vim: et sw=4 sts=4
static instance($prototype=null)
Retrieve sole instance of the factory.
Represents a language and defines localizable string formatting and other functions,...
Definition: Language.php:8
$config
@type HTMLPurifier_Config
Definition: Language.php:52
formatMessage($key, $args=array())
Formats a localised message with passed parameters.
Definition: Language.php:148
$code
ISO 639 language code of language.
Definition: Language.php:14
getErrorName($int)
Retrieves a localised error name.
Definition: Language.php:107
getMessage($key)
Retrieves a localised message.
Definition: Language.php:91
$_loaded
Has the language object been loaded yet? @type bool.
Definition: Language.php:47
listify($array)
Converts an array list into a string readable representation.
Definition: Language.php:123
$errorNames
Array of localizable error codes.
Definition: Language.php:32
$error
True if no message file was found for this language, so English is being used instead.
Definition: Language.php:40
$messages
Array of localizable messages.
Definition: Language.php:26
$fallback
Fallback language code.
Definition: Language.php:20
$context
@type HTMLPurifier_Context
Definition: Language.php:57
load()
Loads language object with necessary info from factory cache.
Definition: Language.php:73
__construct($config, $context)
Definition: Language.php:63
Abstract base token class that all others inherit from.
Definition: Token.php:7