Source for file Language.php

Documentation is available at Language.php

  1. <?php
  2.  
  3. /**
  4.  * Represents a language and defines localizable string formatting and
  5.  * other functions, as well as the localized messages for HTML Purifier.
  6.  */
  7. {
  8.     
  9.     /**
  10.      * ISO 639 language code of language. Prefers shortest possible version
  11.      */
  12.     public $code = 'en';
  13.     
  14.     /**
  15.      * Fallback language code
  16.      */
  17.     public $fallback = false;
  18.     
  19.     /**
  20.      * Array of localizable messages
  21.      */
  22.     public $messages = array();
  23.     
  24.     /**
  25.      * Array of localizable error codes
  26.      */
  27.     public $errorNames = array();
  28.     
  29.     /**
  30.      * True if no message file was found for this language, so English
  31.      * is being used instead. Check this if you'd like to notify the
  32.      * user that they've used a non-supported language.
  33.      */
  34.     public $error = false;
  35.     
  36.     /**
  37.      * Has the language object been loaded yet?
  38.      * @todo Make it private, fix usage in HTMLPurifier_LanguageTest
  39.      */
  40.     public $_loaded = false;
  41.     
  42.     /**
  43.      * Instances of HTMLPurifier_Config and HTMLPurifier_Context
  44.      */
  45.     protected $config$context;
  46.     
  47.     public function __construct($config$context{
  48.         $this->config  = $config;
  49.         $this->context = $context;
  50.     }
  51.     
  52.     /**
  53.      * Loads language object with necessary info from factory cache
  54.      * @note This is a lazy loader
  55.      */
  56.     public function load({
  57.         if ($this->_loadedreturn;
  58.         $factory HTMLPurifier_LanguageFactory::instance();
  59.         $factory->loadLanguage($this->code);
  60.         foreach ($factory->keys as $key{
  61.             $this->$key $factory->cache[$this->code][$key];
  62.         }
  63.         $this->_loaded = true;
  64.     }
  65.     
  66.     /**
  67.      * Retrieves a localised message.
  68.      * @param $key string identifier of message
  69.      * @return string localised message
  70.      */
  71.     public function getMessage($key{
  72.         if (!$this->_loaded$this->load();
  73.         if (!isset($this->messages[$key])) return "[$key]";
  74.         return $this->messages[$key];
  75.     }
  76.     
  77.     /**
  78.      * Retrieves a localised error name.
  79.      * @param $int integer error number, corresponding to PHP's error
  80.      *              reporting
  81.      * @return string localised message
  82.      */
  83.     public function getErrorName($int{
  84.         if (!$this->_loaded$this->load();
  85.         if (!isset($this->errorNames[$int])) return "[Error: $int]";
  86.         return $this->errorNames[$int];
  87.     }
  88.     
  89.     /**
  90.      * Converts an array list into a string readable representation
  91.      */
  92.     public function listify($array{
  93.         $sep      $this->getMessage('Item separator');
  94.         $sep_last $this->getMessage('Item separator last');
  95.         $ret '';
  96.         for ($i 0$c count($array)$i $c$i++{
  97.             if ($i == 0{
  98.             elseif ($i $c{
  99.                 $ret .= $sep;
  100.             else {
  101.                 $ret .= $sep_last;
  102.             }
  103.             $ret .= $array[$i];
  104.         }
  105.         return $ret;
  106.     }
  107.     
  108.     /**
  109.      * Formats a localised message with passed parameters
  110.      * @param $key string identifier of message
  111.      * @param $args Parameters to substitute in
  112.      * @return string localised message
  113.      * @todo Implement conditionals? Right now, some messages make
  114.      *      reference to line numbers, but those aren't always available
  115.      */
  116.     public function formatMessage($key$args array()) {
  117.         if (!$this->_loaded$this->load();
  118.         if (!isset($this->messages[$key])) return "[$key]";
  119.         $raw $this->messages[$key];
  120.         $subst array();
  121.         $generator false;
  122.         foreach ($args as $i => $value{
  123.             if (is_object($value)) {
  124.                 if ($value instanceof HTMLPurifier_Token{
  125.                     // factor this out some time
  126.                     if (!$generator$generator $this->context->get('Generator');
  127.                     if (isset($value->name)) $subst['$'.$i.'.Name'$value->name;
  128.                     if (isset($value->data)) $subst['$'.$i.'.Data'$value->data;
  129.                     $subst['$'.$i.'.Compact'
  130.                     $subst['$'.$i.'.Serialized'$generator->generateFromToken($value);
  131.                     // a more complex algorithm for compact representation
  132.                     // could be introduced for all types of tokens. This
  133.                     // may need to be factored out into a dedicated class
  134.                     if (!empty($value->attr)) {
  135.                         $stripped_token clone $value;
  136.                         $stripped_token->attr array();
  137.                         $subst['$'.$i.'.Compact'$generator->generateFromToken($stripped_token);
  138.                     }
  139.                     $subst['$'.$i.'.Line'$value->line $value->line 'unknown';
  140.                 }
  141.                 continue;
  142.             elseif (is_array($value)) {
  143.                 $keys array_keys($value);
  144.                 if (array_keys($keys=== $keys{
  145.                     // list
  146.                     $subst['$'.$i$this->listify($value);
  147.                 else {
  148.                     // associative array
  149.                     // no $i implementation yet, sorry
  150.                     $subst['$'.$i.'.Keys'$this->listify($keys);
  151.                     $subst['$'.$i.'.Values'$this->listify(array_values($value));
  152.                 }
  153.                 continue;
  154.             }
  155.             $subst['$' $i$value;
  156.         }
  157.         return strtr($raw$subst);
  158.     }
  159.     
  160. }

Documentation generated on Thu, 19 Jun 2008 18:49:40 -0400 by phpDocumentor 1.4.2