ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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? More...
 
 $context
 

Protected Attributes

 $config
 Instances of HTMLPurifier_Config and 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 
)

Definition at line 48 of file Language.php.

References $config, and $context.

48  {
49  $this->config = $config;
50  $this->context = $context;
51  }
$config
Instances of HTMLPurifier_Config and HTMLPurifier_Context.
Definition: Language.php:46

Member Function Documentation

◆ formatMessage()

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

Formats a localised message with passed parameters.

Parameters
$keystring identifier of message
$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 117 of file Language.php.

References listify(), and load().

117  {
118  if (!$this->_loaded) $this->load();
119  if (!isset($this->messages[$key])) return "[$key]";
120  $raw = $this->messages[$key];
121  $subst = array();
122  $generator = false;
123  foreach ($args as $i => $value) {
124  if (is_object($value)) {
125  if ($value instanceof HTMLPurifier_Token) {
126  // factor this out some time
127  if (!$generator) $generator = $this->context->get('Generator');
128  if (isset($value->name)) $subst['$'.$i.'.Name'] = $value->name;
129  if (isset($value->data)) $subst['$'.$i.'.Data'] = $value->data;
130  $subst['$'.$i.'.Compact'] =
131  $subst['$'.$i.'.Serialized'] = $generator->generateFromToken($value);
132  // a more complex algorithm for compact representation
133  // could be introduced for all types of tokens. This
134  // may need to be factored out into a dedicated class
135  if (!empty($value->attr)) {
136  $stripped_token = clone $value;
137  $stripped_token->attr = array();
138  $subst['$'.$i.'.Compact'] = $generator->generateFromToken($stripped_token);
139  }
140  $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown';
141  }
142  continue;
143  } elseif (is_array($value)) {
144  $keys = array_keys($value);
145  if (array_keys($keys) === $keys) {
146  // list
147  $subst['$'.$i] = $this->listify($value);
148  } else {
149  // associative array
150  // no $i implementation yet, sorry
151  $subst['$'.$i.'.Keys'] = $this->listify($keys);
152  $subst['$'.$i.'.Values'] = $this->listify(array_values($value));
153  }
154  continue;
155  }
156  $subst['$' . $i] = $value;
157  }
158  return strtr($raw, $subst);
159  }
Abstract base token class that all others inherit from.
Definition: Token.php:6
listify($array)
Converts an array list into a string readable representation.
Definition: Language.php:93
load()
Loads language object with necessary info from factory cache.
Definition: Language.php:57
+ Here is the call graph for this function:

◆ getErrorName()

HTMLPurifier_Language::getErrorName (   $int)

Retrieves a localised error name.

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

Definition at line 84 of file Language.php.

References load().

84  {
85  if (!$this->_loaded) $this->load();
86  if (!isset($this->errorNames[$int])) return "[Error: $int]";
87  return $this->errorNames[$int];
88  }
load()
Loads language object with necessary info from factory cache.
Definition: Language.php:57
+ Here is the call graph for this function:

◆ getMessage()

HTMLPurifier_Language::getMessage (   $key)

Retrieves a localised message.

Parameters
$keystring identifier of message
Returns
string localised message

Definition at line 72 of file Language.php.

References load().

Referenced by listify().

72  {
73  if (!$this->_loaded) $this->load();
74  if (!isset($this->messages[$key])) return "[$key]";
75  return $this->messages[$key];
76  }
load()
Loads language object with necessary info from factory cache.
Definition: Language.php:57
+ 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.

Definition at line 93 of file Language.php.

References $ret, and getMessage().

Referenced by formatMessage().

93  {
94  $sep = $this->getMessage('Item separator');
95  $sep_last = $this->getMessage('Item separator last');
96  $ret = '';
97  for ($i = 0, $c = count($array); $i < $c; $i++) {
98  if ($i == 0) {
99  } elseif ($i + 1 < $c) {
100  $ret .= $sep;
101  } else {
102  $ret .= $sep_last;
103  }
104  $ret .= $array[$i];
105  }
106  return $ret;
107  }
getMessage($key)
Retrieves a localised message.
Definition: Language.php:72
+ 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 57 of file Language.php.

References $code, and HTMLPurifier_LanguageFactory\instance().

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

57  {
58  if ($this->_loaded) return;
60  $factory->loadLanguage($this->code);
61  foreach ($factory->keys as $key) {
62  $this->$key = $factory->cache[$this->code][$key];
63  }
64  $this->_loaded = true;
65  }
$code
ISO 639 language code of language.
Definition: Language.php:13
static instance($prototype=null)
Retrieve sole instance of the factory.
+ 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?

Todo:
Make it private, fix usage in HTMLPurifier_LanguageTest

Definition at line 41 of file Language.php.

◆ $code

HTMLPurifier_Language::$code = 'en'

ISO 639 language code of language.

Prefers shortest possible version

Definition at line 13 of file Language.php.

Referenced by load().

◆ $config

HTMLPurifier_Language::$config
protected

Instances of HTMLPurifier_Config and HTMLPurifier_Context.

Definition at line 46 of file Language.php.

Referenced by __construct().

◆ $context

HTMLPurifier_Language::$context

Definition at line 46 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.

Definition at line 35 of file Language.php.

◆ $errorNames

HTMLPurifier_Language::$errorNames = array()

Array of localizable error codes.

Definition at line 28 of file Language.php.

◆ $fallback

HTMLPurifier_Language::$fallback = false

Fallback language code.

Definition at line 18 of file Language.php.

◆ $messages

HTMLPurifier_Language::$messages = array()

Array of localizable messages.

Definition at line 23 of file Language.php.


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