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

Source for file Decorator.php

Documentation is available at Decorator.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator base 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.  * @copyright 2004-2005 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: Decorator.php,v 1.25 2008/11/14 16:18:50 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * Translation2_Decorator. Base Decorator class for Translation2
  41.  *
  42.  * Extend this class to provide custom decorators.
  43.  * Some decorators are already bundled with the package.
  44.  *
  45.  * @category  Internationalization
  46.  * @package   Translation2
  47.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  48.  * @copyright 2004-2005 Lorenzo Alberton
  49.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  50.  * @link      http://pear.php.net/package/Translation2
  51.  * @abstract
  52.  */
  53. {
  54.     // {{{ class vars
  55.  
  56.     /**
  57.      * Translation2 object being decorated
  58.      * @var object 
  59.      * @access protected
  60.      */
  61.     var $translation2;
  62.  
  63.     /**
  64.      * @var object 
  65.      * @access protected
  66.      */
  67.     var $storage;
  68.  
  69.     /**
  70.      * @var array 
  71.      * @access protected
  72.      */
  73.     var $lang;
  74.  
  75.     /**
  76.      * @var string 
  77.      * @access protected
  78.      */
  79.     var $currentPageID;
  80.  
  81.     // }}}
  82.     // {{{ Constructor
  83.  
  84.     /**
  85.      * Constructor
  86.      * Constructs the Translation2_Decorator
  87.      *
  88.      * @param object &$translation2 Translation2 object to decorate
  89.      */
  90.     function Translation2_Decorator($translation2)
  91.     {
  92.         $this->translation2 = $translation2;
  93.         //used for debug only
  94.         $this->storage       = $translation2->storage;
  95.         $this->currentPageID = $translation2->currentPageID;
  96.         $this->lang          = $translation2->lang;
  97.     }
  98.  
  99.     // }}}
  100.     // {{{ setOptions()
  101.  
  102.     /**
  103.      * set Decorator options
  104.      *
  105.      * @param array $options decorator options
  106.      *
  107.      * @return self 
  108.      */
  109.     function setOptions($options=array())
  110.     {
  111.         if (is_array($options)) {
  112.             foreach ($options as $option => $value{
  113.                 $this->setOption($option$value);
  114.             }
  115.         }
  116.         return $this;
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ setOption()
  121.  
  122.     /**
  123.      * set Decorator option
  124.      *
  125.      * @param string $option option name
  126.      * @param mixed  $value  option value
  127.      *
  128.      * @return self 
  129.      */
  130.     function setOption($option$value=null)
  131.     {
  132.         if (isset($this->$option)) {
  133.             $this->$option $value;
  134.         elseif (is_a($this->translation2'Translation2_Decorator')) {
  135.             $this->translation2->setOption($option$value);
  136.         }
  137.         return $this;
  138.     }
  139.  
  140.     // }}}
  141.     // {{{ setContainerOptions()
  142.  
  143.     /**
  144.      * Set some storage driver options
  145.      *
  146.      * @param array $options storage driver options
  147.      *
  148.      * @return void 
  149.      * @access protected
  150.      */
  151.     function setContainerOptions($options)
  152.     {
  153.         $this->storage->_parseOptions($options);
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ getDecorator()
  158.  
  159.     /**
  160.      * Return an instance of a decorator
  161.      *
  162.      * @param string $decorator Name of the decorator
  163.      * @param object $object    instance [optional]
  164.      *
  165.      * @return object Decorator object reference
  166.      * @access public
  167.      */
  168.     function getDecorator($decorator)
  169.     {
  170.         if (func_num_args(> 1{
  171.             $obj func_get_arg(1);
  172.             $new_decorator =$this->translation2->getDecorator($decorator$obj);
  173.         else {
  174.             $new_decorator =$this->translation2->getDecorator($decorator$this);
  175.         }
  176.         return $new_decorator;
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ setCharset()
  181.  
  182.     /**
  183.      * Set charset used to read/store the translations
  184.      *
  185.      * @param string $charset character set (encoding)
  186.      *
  187.      * @return PEAR_Error on failure
  188.      */
  189.     function setCharset($charset)
  190.     {
  191.         return $this->translation2->setCharset($charset);
  192.     }
  193.  
  194.     // }}}
  195.     // {{{ setLang()
  196.  
  197.     /**
  198.      * Set default language
  199.      *
  200.      * @param string $langID language ID
  201.      *
  202.      * @return true|PEAR_Error
  203.      */
  204.     function setLang($langID)
  205.     {
  206.         return $this->translation2->setLang($langID);
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ setPageID($pageID)
  211.  
  212.     /**
  213.      * Set default page
  214.      *
  215.      * @param string $pageID page/group ID
  216.      *
  217.      * @return void 
  218.      */
  219.     function setPageID($pageID = null)
  220.     {
  221.         $this->translation2->setPageID($pageID);
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ getLang()
  226.  
  227.     /**
  228.      * Get language info
  229.      *
  230.      * @param string $langID language ID
  231.      * @param string $format ['name', 'meta', 'error_text', 'array']
  232.      *
  233.      * @return mixed [string | array], depending on $format
  234.      */
  235.     function getLang($langID=null$format='name')
  236.     {
  237.         return $this->translation2->getLang($langID$format);
  238.     }
  239.  
  240.     // }}}
  241.     // {{{ getLangs()
  242.  
  243.     /**
  244.      * Get languages
  245.      *
  246.      * @param string $format ['ids', 'names', 'array']
  247.      *
  248.      * @return array 
  249.      */
  250.     function getLangs($format='name')
  251.     {
  252.         return $this->translation2->getLangs($format);
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ setParams()
  257.  
  258.     /**
  259.      * Set parameters for next string
  260.      *
  261.      * @param array $params array of replacement parameters
  262.      *
  263.      * @return void 
  264.      */
  265.     function setParams($params = null)
  266.     {
  267.         $this->translation2->setParams($params);
  268.     }
  269.  
  270.     // }}}
  271.     // {{{ getRaw()
  272.  
  273.     /**
  274.      * Get translated string
  275.      *
  276.      * No filter is applied.
  277.      *
  278.      * @param string $stringID    string ID
  279.      * @param string $pageID      page/group ID
  280.      * @param string $langID      language ID
  281.      * @param string $defaultText Text to display when the strings in both
  282.      *                             the default and the fallback lang are empty
  283.      *
  284.      * @return string 
  285.      */
  286.     function getRaw($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  287.     {
  288.         return $this->translation2->getRaw($stringID$pageID$langID$defaultText);
  289.     }
  290.  
  291.     // }}}
  292.     // {{{ get()
  293.  
  294.     /**
  295.      * Get translated string
  296.      *
  297.      * All the filters are applied.
  298.      *
  299.      * @param string $stringID    string ID
  300.      * @param string $pageID      page/group ID
  301.      * @param string $langID      language ID
  302.      * @param string $defaultText Text to display when the string is empty
  303.      *                NB: This parameter is only used in the DefaultText decorator
  304.      *
  305.      * @return string 
  306.      */
  307.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  308.     {
  309.         return $this->translation2->get($stringID$pageID$langID$defaultText);
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ getRawPage()
  314.  
  315.     /**
  316.      * Get the array of strings in a page
  317.      *
  318.      * Fetch the strings from the container, without any replacing
  319.      *
  320.      * @param string $pageID page/group ID
  321.      * @param string $langID language ID
  322.      *
  323.      * @return array 
  324.      */
  325.     function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  326.     {
  327.         return $this->translation2->getRawPage($pageID$langID);
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ getPage()
  332.  
  333.     /**
  334.      * Same as getRawPage, but resort to fallback language and
  335.      * replace parameters when needed
  336.      *
  337.      * @param string $pageID page/group ID
  338.      * @param string $langID language ID
  339.      *
  340.      * @return array 
  341.      */
  342.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  343.     {
  344.         $this->translation2->getPage($pageID$langID);
  345.     }
  346.  
  347.     // }}}
  348.     // {{{ _replaceParams()
  349.  
  350.     /**
  351.      * Replace parameters in strings
  352.      *
  353.      * @param mixed $strings strings where the replacements must occur
  354.      *
  355.      * @return mixed 
  356.      * @access protected
  357.      */
  358.     function _replaceParams($strings)
  359.     {
  360.         return $this->translation2->_replaceParams($strings);
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ replaceEmptyStringsWithKeys()
  365.  
  366.     /**
  367.      * Replace empty strings with their stringID
  368.      *
  369.      * @param array $strings array of strings to be replaced if empty
  370.      *
  371.      * @return array 
  372.      * @access public
  373.      */
  374.     function replaceEmptyStringsWithKeys($strings)
  375.     {
  376.         return $this->translation2->replaceEmptyStringsWithKeys($strings);
  377.     }
  378.  
  379.     // }}}
  380.     // {{{ getStringID()
  381.  
  382.     /**
  383.      * Get the stringID for the given string. This method is the reverse of get().
  384.      *
  385.      * @param string $string This is NOT the stringID, this is a real string.
  386.      *                The method will search for its matching stringID, and then
  387.      *                it will return the associate string in the selected language.
  388.      * @param string $pageID page/group ID
  389.      *
  390.      * @return string 
  391.      */
  392.     function getStringID($string$pageID = TRANSLATION2_DEFAULT_PAGEID)
  393.     {
  394.         return $this->translation2->getStringID($string$pageID);
  395.     }
  396.  
  397.     // }}}
  398.     // {{{ __clone()
  399.  
  400.     /**
  401.      * Clone internal object references
  402.      *
  403.      * This method is called automatically by PHP5
  404.      *
  405.      * @return void 
  406.      * @access protected
  407.      */
  408.     function __clone()
  409.     {
  410.         $this->translation2 = clone($this->translation2);
  411.     }
  412.  
  413.     // }}}
  414. }
  415. ?>

Documentation generated on Fri, 14 Nov 2008 11:30:18 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.