ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
HTMLPurifier_Language Class Reference

Represents a language and defines localizable string formatting and other functions, as well as the localized messages for HTML Purifier. More...

+ Inheritance diagram for HTMLPurifier_Language:
+ Collaboration diagram for HTMLPurifier_Language:

Public Member Functions

 __construct ($config, $context)
 
 load ()
 Loads language object with necessary info from factory cache. More...
 
 getMessage ($key)
 Retrieves a localised message. More...
 
 getErrorName ($int)
 Retrieves a localised error name. More...
 
 listify ($array)
 Converts an array list into a string readable representation. More...
 
 formatMessage ($key, $args=array())
 Formats a localised message with passed parameters. More...
 

Data Fields

 $code = 'en'
 ISO 639 language code of language. More...
 
 $fallback = false
 Fallback language code. More...
 
 $messages = array()
 Array of localizable messages. More...
 
 $errorNames = array()
 Array of localizable error codes. More...
 
 $error = false
 True if no message file was found for this language, so English is being used instead. More...
 
 $_loaded = false
 Has the language object been loaded yet? @type bool. More...
 

Protected Attributes

 $config
 @type HTMLPurifier_Config More...
 
 $context
 @type HTMLPurifier_Context More...
 

Detailed Description

Represents a language and defines localizable string formatting and other functions, as well as the localized messages for HTML Purifier.

Definition at line 7 of file Language.php.

Constructor & Destructor Documentation

◆ __construct()

HTMLPurifier_Language::__construct (   $config,
  $context 
)
Parameters
HTMLPurifier_Config$config
HTMLPurifier_Context$context

Definition at line 63 of file Language.php.

64 {
65 $this->config = $config;
66 $this->context = $context;
67 }
$config
@type HTMLPurifier_Config
Definition: Language.php:52
$context
@type HTMLPurifier_Context
Definition: Language.php:57

References $config, and $context.

Member Function Documentation

◆ formatMessage()

HTMLPurifier_Language::formatMessage (   $key,
  $args = array() 
)

Formats a localised message with passed parameters.

Parameters
string$keystring identifier of message
array$argsParameters to substitute in
Returns
string localised message
Todo:
Implement conditionals? Right now, some messages make reference to line numbers, but those aren't always available

Definition at line 148 of file Language.php.

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 }
listify($array)
Converts an array list into a string readable representation.
Definition: Language.php:123
load()
Loads language object with necessary info from factory cache.
Definition: Language.php:73
Abstract base token class that all others inherit from.
Definition: Token.php:7

References listify(), and load().

+ Here is the call graph for this function:

◆ getErrorName()

HTMLPurifier_Language::getErrorName (   $int)

Retrieves a localised error name.

Parameters
int$interror number, corresponding to PHP's error reporting
Returns
string localised message

Definition at line 107 of file Language.php.

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 }

References load().

+ Here is the call graph for this function:

◆ getMessage()

HTMLPurifier_Language::getMessage (   $key)

Retrieves a localised message.

Parameters
string$keystring identifier of message
Returns
string localised message

Definition at line 91 of file Language.php.

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 }

References load().

Referenced by listify().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ listify()

HTMLPurifier_Language::listify (   $array)

Converts an array list into a string readable representation.

Parameters
array$array
Returns
string

Definition at line 123 of file Language.php.

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 }
getMessage($key)
Retrieves a localised message.
Definition: Language.php:91

References $ret, and getMessage().

Referenced by formatMessage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ load()

HTMLPurifier_Language::load ( )

Loads language object with necessary info from factory cache.

Note
This is a lazy loader

Definition at line 73 of file Language.php.

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 }
static instance($prototype=null)
Retrieve sole instance of the factory.
$code
ISO 639 language code of language.
Definition: Language.php:14

References $code, and HTMLPurifier_LanguageFactory\instance().

Referenced by formatMessage(), getErrorName(), and getMessage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_loaded

HTMLPurifier_Language::$_loaded = false

Has the language object been loaded yet? @type bool.

Todo:
Make it private, fix usage in HTMLPurifier_LanguageTest

Definition at line 47 of file Language.php.

◆ $code

HTMLPurifier_Language::$code = 'en'

ISO 639 language code of language.

Prefers shortest possible version. @type string

Definition at line 14 of file Language.php.

Referenced by load().

◆ $config

HTMLPurifier_Language::$config
protected

@type HTMLPurifier_Config

Definition at line 52 of file Language.php.

Referenced by __construct().

◆ $context

HTMLPurifier_Language::$context
protected

@type HTMLPurifier_Context

Definition at line 57 of file Language.php.

Referenced by __construct().

◆ $error

HTMLPurifier_Language::$error = false

True if no message file was found for this language, so English is being used instead.

Check this if you'd like to notify the user that they've used a non-supported language. @type bool

Definition at line 40 of file Language.php.

◆ $errorNames

HTMLPurifier_Language::$errorNames = array()

Array of localizable error codes.

@type array

Definition at line 32 of file Language.php.

◆ $fallback

HTMLPurifier_Language::$fallback = false

Fallback language code.

@type bool|string

Definition at line 20 of file Language.php.

◆ $messages

HTMLPurifier_Language::$messages = array()

Array of localizable messages.

@type array

Definition at line 26 of file Language.php.


The documentation for this class was generated from the following file: