|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00007 class HTMLPurifier_Language 00008 { 00009 00013 public $code = 'en'; 00014 00018 public $fallback = false; 00019 00023 public $messages = array(); 00024 00028 public $errorNames = array(); 00029 00035 public $error = false; 00036 00041 public $_loaded = false; 00042 00046 protected $config, $context; 00047 00048 public function __construct($config, $context) { 00049 $this->config = $config; 00050 $this->context = $context; 00051 } 00052 00057 public function load() { 00058 if ($this->_loaded) return; 00059 $factory = HTMLPurifier_LanguageFactory::instance(); 00060 $factory->loadLanguage($this->code); 00061 foreach ($factory->keys as $key) { 00062 $this->$key = $factory->cache[$this->code][$key]; 00063 } 00064 $this->_loaded = true; 00065 } 00066 00072 public function getMessage($key) { 00073 if (!$this->_loaded) $this->load(); 00074 if (!isset($this->messages[$key])) return "[$key]"; 00075 return $this->messages[$key]; 00076 } 00077 00084 public function getErrorName($int) { 00085 if (!$this->_loaded) $this->load(); 00086 if (!isset($this->errorNames[$int])) return "[Error: $int]"; 00087 return $this->errorNames[$int]; 00088 } 00089 00093 public function listify($array) { 00094 $sep = $this->getMessage('Item separator'); 00095 $sep_last = $this->getMessage('Item separator last'); 00096 $ret = ''; 00097 for ($i = 0, $c = count($array); $i < $c; $i++) { 00098 if ($i == 0) { 00099 } elseif ($i + 1 < $c) { 00100 $ret .= $sep; 00101 } else { 00102 $ret .= $sep_last; 00103 } 00104 $ret .= $array[$i]; 00105 } 00106 return $ret; 00107 } 00108 00117 public function formatMessage($key, $args = array()) { 00118 if (!$this->_loaded) $this->load(); 00119 if (!isset($this->messages[$key])) return "[$key]"; 00120 $raw = $this->messages[$key]; 00121 $subst = array(); 00122 $generator = false; 00123 foreach ($args as $i => $value) { 00124 if (is_object($value)) { 00125 if ($value instanceof HTMLPurifier_Token) { 00126 // factor this out some time 00127 if (!$generator) $generator = $this->context->get('Generator'); 00128 if (isset($value->name)) $subst['$'.$i.'.Name'] = $value->name; 00129 if (isset($value->data)) $subst['$'.$i.'.Data'] = $value->data; 00130 $subst['$'.$i.'.Compact'] = 00131 $subst['$'.$i.'.Serialized'] = $generator->generateFromToken($value); 00132 // a more complex algorithm for compact representation 00133 // could be introduced for all types of tokens. This 00134 // may need to be factored out into a dedicated class 00135 if (!empty($value->attr)) { 00136 $stripped_token = clone $value; 00137 $stripped_token->attr = array(); 00138 $subst['$'.$i.'.Compact'] = $generator->generateFromToken($stripped_token); 00139 } 00140 $subst['$'.$i.'.Line'] = $value->line ? $value->line : 'unknown'; 00141 } 00142 continue; 00143 } elseif (is_array($value)) { 00144 $keys = array_keys($value); 00145 if (array_keys($keys) === $keys) { 00146 // list 00147 $subst['$'.$i] = $this->listify($value); 00148 } else { 00149 // associative array 00150 // no $i implementation yet, sorry 00151 $subst['$'.$i.'.Keys'] = $this->listify($keys); 00152 $subst['$'.$i.'.Values'] = $this->listify(array_values($value)); 00153 } 00154 continue; 00155 } 00156 $subst['$' . $i] = $value; 00157 } 00158 return strtr($raw, $subst); 00159 } 00160 00161 } 00162 00163 // vim: et sw=4 sts=4