[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/i18n/ -> sfMessageFormat.class.php (source)

   1  <?php
   2  
   3  /**
   4   * sfMessageFormat class file.
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the BSD License.
   8   *
   9   * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  10   *
  11   * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  12   * The latest version of PRADO can be obtained from:
  13   * {@link http://prado.sourceforge.net/}
  14   *
  15   * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
  16   * @version    $Id: sfMessageFormat.class.php 2834 2006-11-27 14:09:05Z fabien $
  17   * @package    symfony
  18   * @subpackage i18n
  19   */
  20  
  21  /**
  22   * Get the encoding utilities
  23   */
  24  require_once(dirname(__FILE__).'/util.php');
  25  
  26  /**
  27   * sfMessageFormat class.
  28   * 
  29   * Format a message, that is, for a particular message find the 
  30   * translated message. The following is an example using 
  31   * a SQLite database to store the translation message. 
  32   * Create a new message format instance and echo "Hello"
  33   * in simplified Chinese. This assumes that the world "Hello"
  34   * is translated in the database.
  35   *
  36   * <code>
  37   *  $source = sfMessageSource::factory('SQLite', 'sqlite://messages.db');
  38   *  $source->setCulture('zh_CN');
  39   *  $source->setCache(new sfMessageCache('./tmp'));
  40   *
  41   *  $formatter = new sfMessageFormat($source); 
  42   *  
  43   *  echo $formatter->format('Hello');
  44   * </code>
  45   *
  46   * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  47   * @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
  48   * @package System.I18N.core
  49   */
  50  class sfMessageFormat
  51  {
  52    /**
  53     * The message source.
  54     * @var sfMessageSource
  55     */
  56    protected $source;
  57  
  58    /**
  59     * A list of loaded message catalogues.
  60     * @var array
  61     */
  62    protected $catagloues = array();
  63  
  64    /**
  65     * The translation messages.
  66     * @var array
  67     */
  68    protected $messages = array();
  69  
  70    /**
  71     * A list of untranslated messages.
  72     * @var array
  73     */
  74    protected $untranslated = array();
  75  
  76    /**
  77     * The prefix and suffix to append to untranslated messages.
  78     * @var array
  79     */
  80    protected $postscript = array('', '');
  81  
  82    /**
  83     * Set the default catalogue.
  84     * @var string 
  85     */
  86    public $Catalogue;
  87  
  88    /**
  89     * Output encoding charset
  90     * @var string
  91     */
  92    protected $charset = 'UTF-8';
  93  
  94    /**
  95     * Constructor.
  96     * Create a new instance of sfMessageFormat using the messages
  97     * from the supplied message source.
  98     *
  99     * @param MessageSource the source of translation messages.
 100     * @param string charset for the message output.
 101     */
 102    function __construct(sfIMessageSource $source, $charset = 'UTF-8')
 103    {
 104      $this->source = $source;
 105      $this->setCharset($charset);
 106    }
 107  
 108    /** 
 109     * Sets the charset for message output.
 110     *
 111     * @param string charset, default is UTF-8
 112     */
 113    public function setCharset($charset)
 114    {
 115      $this->charset = $charset;
 116    }
 117  
 118    /**
 119     * Gets the charset for message output. Default is UTF-8.
 120     *
 121     * @return string charset, default UTF-8
 122     */
 123    public function getCharset()
 124    {
 125      return $this->charset;
 126    }
 127    
 128    /**
 129     * Load the message from a particular catalogue. A listed
 130     * loaded catalogues is kept to prevent reload of the same
 131     * catalogue. The load catalogue messages are stored
 132     * in the $this->message array.
 133     *
 134     * @param string message catalogue to load.
 135     */
 136    protected function loadCatalogue($catalogue)
 137    {
 138      if (in_array($catalogue,$this->catagloues))
 139      {
 140        return;
 141      }
 142  
 143      if ($this->source->load($catalogue))
 144      {
 145        $this->messages[$catalogue] = $this->source->read();
 146        $this->catagloues[] = $catalogue;
 147      }
 148    }
 149  
 150    /**
 151     * Format the string. That is, for a particular string find
 152     * the corresponding translation. Variable subsitution is performed
 153     * for the $args parameter. A different catalogue can be specified
 154     * using the $catalogue parameter.
 155     * The output charset is determined by $this->getCharset();
 156     *
 157     * @param string the string to translate.
 158     * @param array a list of string to substitute.
 159     * @param string get the translation from a particular message
 160     * @param string charset, the input AND output charset catalogue.
 161     * @return string translated string.
 162     */
 163    public function format($string, $args = array(), $catalogue = null, $charset = null)
 164    {
 165      if (empty($charset))
 166      {
 167        $charset = $this->getCharset();
 168      }
 169  
 170      $s = $this->formatString(I18N_toUTF8($string, $charset), $args, $catalogue);
 171  
 172      return I18N_toEncoding($s, $charset);
 173    }
 174  
 175    public function formatExists($string, $args = array(), $catalogue = null, $charset = null)
 176    {
 177      if (empty($charset))
 178      {
 179        $charset = $this->getCharset();
 180      }
 181  
 182      $s = $this->getFormattedString(I18N_toUTF8($string, $charset), $args, $catalogue);
 183  
 184      return I18N_toEncoding($s, $charset);
 185    }
 186  
 187    /**
 188     * Do string translation.
 189     *
 190     * @param string the string to translate.
 191     * @param array a list of string to substitute.
 192     * @param string get the translation from a particular message catalogue.
 193     * @return string translated string.
 194     */
 195    protected function formatString($string, $args = array(), $catalogue = null)
 196    {
 197      if (empty($args))
 198      {
 199        $args = array();
 200      }
 201  
 202      $target = $this->getFormattedString($string, $args, $catalogue);
 203  
 204      // well we did not find the translation string.
 205      if (!$target)
 206      {
 207        $this->source->append($string);
 208        $target = $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1];
 209      }
 210  
 211      return $target;
 212    }
 213  
 214    protected function getFormattedString($string, $args = array(), $catalogue = null)
 215    {
 216      if (empty($catalogue))
 217      {
 218        $catalogue = empty($this->Catalogue) ? 'messages' : $this->Catalogue;
 219      }
 220  
 221      if (empty($args))
 222      {
 223        $args = array();
 224      }
 225  
 226      $this->loadCatalogue($catalogue);
 227  
 228      foreach ($this->messages[$catalogue] as $variant)
 229      {
 230        // foreach of the translation units
 231        foreach ($variant as $source => $result)
 232        {
 233          // we found it, so return the target translation
 234          if ($source == $string)
 235          {
 236            // check if it contains only strings.
 237            if (is_string($result))
 238            {
 239              $target = $result;
 240            }
 241            else
 242            {
 243              $target = $result[0];
 244            }
 245  
 246            // found, but untranslated
 247            if (empty($target))
 248            {
 249              return $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1];
 250            }
 251            else
 252            {
 253              return $this->replaceArgs($target, $args);
 254            }
 255          }
 256        }
 257      }
 258  
 259      return null;
 260    }
 261  
 262    protected function replaceArgs($string, $args)
 263    {
 264      // replace object with strings
 265      foreach ($args as $key => $value)
 266      {
 267        if (is_object($value) && method_exists($value, '__toString'))
 268        {
 269          $args[$key] = $value->__toString();
 270        }
 271      }
 272  
 273      return strtr($string, $args);
 274    }
 275  
 276    /**
 277     * Get the message source.
 278     *
 279     * @return MessageSource 
 280     */
 281    function getSource()
 282    {
 283      return $this->source;
 284    }
 285    
 286    /**
 287     * Set the prefix and suffix to append to untranslated messages.
 288     * e.g. $postscript=array('[T]','[/T]'); will output 
 289     * "[T]Hello[/T]" if the translation for "Hello" can not be determined.
 290     *
 291     * @param array first element is the prefix, second element the suffix.
 292     */
 293    function setUntranslatedPS($postscript)
 294    {
 295      if (is_array($postscript) && count($postscript) >= 2)
 296      {
 297        $this->postscript[0] = $postscript[0];
 298        $this->postscript[1] = $postscript[1];
 299      }
 300    }
 301  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7