Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file Iconv.php

Documentation is available at Iconv.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator_Iconv class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @author    Sergey Korotkov <sergey@pushok.com>
  34.  * @copyright 2004-2007 Lorenzo Alberton, Sergey Korotkov
  35.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  36.  * @version   CVS: $Id: Iconv.php,v 1.12 2007/10/28 23:42:35 quipo Exp $
  37.  * @link      http://pear.php.net/package/Translation2
  38.  */
  39.  
  40. /**
  41.  * Load Translation2 decorator base class
  42.  */
  43. require_once 'Translation2/Decorator.php';
  44.  
  45. /**
  46.  * Translation2 Iconv Decorator
  47.  *
  48.  * Decorator to change the encoding of the stored translation to the
  49.  * one given in the 'encoding' option.
  50.  *
  51.  * <code>
  52.  * $tr->setOptions(array('encoding' => 'UTF-8'));
  53.  * </code>
  54.  *
  55.  * @category  Internationalization
  56.  * @package   Translation2
  57.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  58.  * @author    Sergey Korotkov <sergey@pushok.com>
  59.  * @copyright 2004-2007 Lorenzo Alberton, Sergey Korotkov
  60.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  61.  * @version   CVS: $Id: Iconv.php,v 1.12 2007/10/28 23:42:35 quipo Exp $
  62.  * @link      http://pear.php.net/package/Translation2
  63.  * @see       http://www.php.net/htmlentities for a list of available encodings.
  64.  */
  65. {
  66.     // {{{ class vars
  67.  
  68.     /**
  69.      * @var string 
  70.      * @access private
  71.      */
  72.     var $encoding 'ISO-8859-1';
  73.     
  74.     /**
  75.      * @var array 
  76.      * @access private
  77.      */
  78.     var $lang_encodings;
  79.  
  80.     // }}}
  81.     // {{{ _getEncoding()
  82.  
  83.     /**
  84.      * Get the encoding for the given langID
  85.      *
  86.      * @param string $langID language ID
  87.      *
  88.      * @return string encoding
  89.      * @access private
  90.      */
  91.     function _getEncoding($langID = null)
  92.     {
  93.         if (!is_array($this->lang_encodings)) {
  94.             $this->lang_encodings = array();
  95.             foreach ($this->translation2->getLangs('encodings'as $langID => $encoding{
  96.                 $this->lang_encodings[$langID$encoding;
  97.             }
  98.         }
  99.         if (!is_null($langID&& isset($this->lang_encodings[$langID])) {
  100.             return $this->lang_encodings[$langID];
  101.         }
  102.         return $this->lang['encoding'];
  103.     }
  104.  
  105.     // }}}
  106.     // {{{ get()
  107.     
  108.     /**
  109.      * Get the translated string, in the new encoding
  110.      *
  111.      * @param string $stringID    string ID
  112.      * @param string $pageID      page/group ID
  113.      * @param string $langID      language ID
  114.      * @param string $defaultText Text to display when the string is empty
  115.      *
  116.      * @return string 
  117.      */
  118.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText = null)
  119.     {
  120.         $str $this->translation2->get($stringID$pageID$langID$defaultText);
  121.         if (PEAR::isError($str|| empty($str)) {
  122.             return $str;
  123.         }
  124.         return iconv($this->_getEncoding($langID)$this->encoding$str);
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ getPage()
  129.  
  130.     /**
  131.      * Same as getRawPage, but apply transformations when needed
  132.      *
  133.      * @param string $pageID page/group ID
  134.      * @param string $langID language ID
  135.      *
  136.      * @return array 
  137.      */
  138.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  139.     {
  140.         $data $this->translation2->getPage($pageID$langID);
  141.         if (PEAR::isError($data)) {
  142.             return $data;
  143.         }
  144.         $input_encoding $this->_getEncoding($langID);
  145.         foreach (array_keys($dataas $k{
  146.             if (!empty($data[$k])) {
  147.                 $data[$kiconv($input_encoding$this->encoding$data[$k]);
  148.             }
  149.         }
  150.         return $data;
  151.     }
  152.  
  153.     // }}}
  154. }
  155. ?>

Documentation generated on Tue, 06 May 2008 06:00:30 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.