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

Source for file DefaultText.php

Documentation is available at DefaultText.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator_DefaultText 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    Rolf 'Red' Ochsenbein <red@raven.ch>
  34.  * @copyright 2004-2007 Lorenzo Alberton, Rolf 'Red' Ochsenbein
  35.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  36.  * @version   CVS: $Id: DefaultText.php,v 1.13 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.  * Decorator to provide a fallback text for empty strings.
  47.  *
  48.  * If the string is empty, return the <parameter>defaultText</parameter> parameter.
  49.  * If the <parameter>defaultText</parameter> parameter is empty too, then return
  50.  * &quot;$emptyPostfix.$outputString.$emptyPrefix&quot;, the three variables
  51.  * being class properties you can set to a custom string.
  52.  *
  53.  * @category  Internationalization
  54.  * @package   Translation2
  55.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  56.  * @author    Rolf 'Red' Ochsenbein <red@raven.ch>
  57.  * @copyright 2004-2007 Lorenzo Alberton, Rolf 'Red' Ochsenbein
  58.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  59.  * @version   CVS: $Id: DefaultText.php,v 1.13 2007/10/28 23:42:35 quipo Exp $
  60.  * @link      http://pear.php.net/package/Translation2
  61.  */
  62. {
  63.     // {{{ class vars
  64.  
  65.     /**
  66.      * String appended to the returned string when the string is empty
  67.      * and it's replaced by its $stringID. It can be used to mark unreplaced
  68.      * strings.
  69.      * @var string 
  70.      * @access protected
  71.      */
  72.     var $emptyPostfix = '';
  73.  
  74.     /**
  75.      * String prepended to the returned string when the string is empty
  76.      * and it's replaced by its $stringID. It can be used to mark unreplaced
  77.      * strings.
  78.      * @var string 
  79.      * @access protected
  80.      */
  81.     var $emptyPrefix = '';
  82.  
  83.     /**
  84.      * String to output when there was no translation
  85.      * %stringID% will be replaced with the stringID
  86.      * %stringID_url% will replaced with a urlencoded stringID
  87.      * %url% will be replaced with the targeted url
  88.      * @var string 
  89.      * @access protected
  90.      */
  91.     //var $outputString = '%stringID%<a href="%url%">(T)</a>';
  92.     var $outputString = '%stringID%';
  93.  
  94.     /**
  95.      * Targeted URL of strings without translations
  96.      * @var string 
  97.      * @access protected
  98.      */
  99.     var $url = '#';
  100.  
  101.     // }}}
  102.     // {{{ get()
  103.  
  104.     /**
  105.      * Get translated string
  106.      *
  107.      * If the string is empty, return the $defaultText if not empty,
  108.      * the $stringID otherwise.
  109.      *
  110.      * @param string $stringID    string ID
  111.      * @param string $pageID      page/group ID
  112.      * @param string $langID      language ID
  113.      * @param string $defaultText Text to display when the string is empty
  114.      *
  115.      * @return string 
  116.      */
  117.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  118.     {
  119.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  120.             $pageID $this->translation2->currentPageID;
  121.         }
  122.         $str $this->translation2->get($stringID$pageID$langID);
  123.         if (!empty($str)) {
  124.             return $str;
  125.         }
  126.         if (!empty($defaultText)) {
  127.             return $this->_replaceParams($defaultText);
  128.         }
  129.  
  130.         $search  = array(
  131.             '%stringID%',
  132.             '%stringID_url%',
  133.             '%pageID_url%',
  134.             '%url%'
  135.         );
  136.         $replace = array(
  137.             $stringID,
  138.             urlencode($stringID),
  139.             urlencode($pageID),
  140.             $this->url
  141.         );
  142.         return $this->_replaceParams(
  143.             $this->emptyPrefix
  144.             .str_replace($search$replace$this->outputString)
  145.             .$this->emptyPostfix
  146.         );
  147.         //$str = (empty($defaultText) ? $this->emptyPrefix.$stringID.$this->emptyPostfix : $defaultText);
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ getPage()
  152.  
  153.     /**
  154.      * Replace empty strings with their $stringID
  155.      *
  156.      * @param string $pageID page/group ID
  157.      * @param string $langID language ID
  158.      *
  159.      * @return array 
  160.      */
  161.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  162.     {
  163.         $data $this->translation2->getPage($pageID$langID);
  164.         return $this->replaceEmptyStringsWithKeys($data);
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ getStringID
  169.  
  170.     /**
  171.      * Get the stringID for the given string. This method is the reverse of get().
  172.      * If the requested string is unknown to the system,
  173.      * the requested string will be returned.
  174.      *
  175.      * @param string $string This is NOT the stringID, this is a real string.
  176.      *                        The method will search for its matching stringID,
  177.      *                        and then it will return the associate string in the
  178.      *                        selected language.
  179.      * @param string $pageID page/group ID
  180.      *
  181.      * @return string 
  182.      */
  183.     function &getStringID($string$pageID = TRANSLATION2_DEFAULT_PAGEID)
  184.     {
  185.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  186.             $pageID $this->translation2->currentPageID;
  187.         }
  188.         $stringID $this->storage->getStringID($string$pageID);
  189.         if (empty($stringID)) {
  190.             $stringID $string;
  191.         }
  192.         return $stringID;
  193.     }
  194. }
  195. ?>

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