[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/i18n/src/toolkit/ -> lmbI18NTools.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/toolkit/src/lmbAbstractTools.class.php');
  10  lmb_require('limb/i18n/src/locale/lmbLocale.class.php');
  11  lmb_require('limb/i18n/src/translation/lmbQtDictionaryBackend.class.php');
  12  
  13  @define('LIMB_LOCALE_INCLUDE_PATH', 'i18n/locale;limb/i18n/i18n/locale');
  14  
  15  /**

  16   * class lmbI18NTools.

  17   *

  18   * @package i18n

  19   * @version $Id: lmbI18NTools.class.php 5945 2007-06-06 08:31:43Z pachanga $

  20   */
  21  class lmbI18NTools extends lmbAbstractTools
  22  {
  23    protected $current_locale;
  24    protected $locale_objects = array();
  25    protected $dictionaries = array();
  26    protected $dict_backend;
  27  
  28    function getDictionaryBackend()
  29    {
  30      if(!is_object($this->dict_backend))
  31      {
  32        $this->dict_backend = new lmbQtDictionaryBackend();
  33        if(defined('LIMB_VAR_DIR'))
  34        {
  35          $this->dict_backend->setCacheDir(LIMB_VAR_DIR);
  36          $this->dict_backend->useCache();
  37        }
  38      }
  39  
  40      return $this->dict_backend;
  41    }
  42  
  43    function setDictionaryBackend($backend)
  44    {
  45      $this->dict_backend = $backend;
  46    }
  47  
  48    function getLocale()
  49    {
  50      if(!$this->current_locale)
  51        $this->current_locale = 'en_US';
  52  
  53      return $this->current_locale;
  54    }
  55  
  56    function setLocale($locale)
  57    {
  58      $this->current_locale = $locale;
  59    }
  60  
  61    function getLocaleObject($locale = null)
  62    {
  63      if(!$locale)
  64        $locale = $this->getLocale();
  65  
  66      if(!isset($this->locale_objects[$locale]))
  67        $this->locale_objects[$locale] = $this->createLocaleObject($locale);
  68  
  69      return $this->locale_objects[$locale];
  70    }
  71  
  72    function addLocaleObject($obj, $locale = null)
  73    {
  74      if(!$locale)
  75        $locale = $obj->getLocaleString();
  76  
  77      $this->locale_objects[$locale] = $obj;
  78    }
  79  
  80    function createLocaleObject($locale)
  81    {
  82      $file = $this->toolkit->findFileAlias($locale . '.ini', LIMB_LOCALE_INCLUDE_PATH, 'i18n');
  83  
  84      if(defined('LIMB_VAR_DIR'))
  85        return new lmbLocale($locale, new lmbCachedIni($file, LIMB_VAR_DIR . '/locale/'));
  86      else
  87        return new lmbLocale($locale, new lmbIni($file));
  88    }
  89  
  90    function getDictionary($locale, $domain)
  91    {
  92      if(!isset($this->dictionaries[$locale . '@' . $domain]))
  93      {
  94        $backend = $this->getDictionaryBackend();
  95        $this->dictionaries[$locale . '@' . $domain] = $backend->load($locale, $domain);
  96      }
  97  
  98      return $this->dictionaries[$locale . '@' . $domain];
  99    }
 100  
 101    function setDictionary($locale, $domain, $dict)
 102    {
 103      $this->dictionaries[$locale . '@' . $domain] = $dict;
 104    }
 105  
 106    function translate($text, $arg1 = null, $arg2 = null)
 107    {
 108      $locale = $this->getLocale();
 109  
 110      $domain = 'default';
 111      $attributes = null;
 112  
 113      if(is_array($arg1))
 114      {
 115        $attributes = $arg1;
 116        if(is_string($arg2))
 117          $domain = $arg2;
 118      }
 119      elseif(is_string($arg1))
 120        $domain = $arg1;
 121  
 122      if($dict = $this->getDictionary($locale, $domain))
 123        return $dict->translate($text, $attributes);
 124      else
 125        return $text;
 126    }
 127  }
 128  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7