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

Source for file gettext.php

Documentation is available at gettext.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Container_gettext 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    Michael Wallner <mike@php.net>
  34.  * @copyright 2004-2008 Lorenzo Alberton, Michael Wallner
  35.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  36.  * @version   CVS: $Id: gettext.php,v 1.32 2008/02/02 18:05:06 quipo Exp $
  37.  * @link      http://pear.php.net/package/Translation2
  38.  */
  39.  
  40. /**
  41.  * require Translation2_Container class
  42.  */
  43. require_once 'Translation2/Container.php';
  44.  
  45. /**
  46.  * require I18Nv2 for locale handling
  47.  */
  48. require_once 'I18Nv2.php';
  49.  
  50. /**
  51.  * Storage driver for fetching data with gettext
  52.  *
  53.  * This storage driver requires the gettext extension
  54.  * and the PEAR::I18Nv2 class for locale handling
  55.  *
  56.  * @category  Internationalization
  57.  * @package   Translation2
  58.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  59.  * @author    Michael Wallner <mike@php.net>
  60.  * @copyright 2004-2008 Lorenzo Alberton, Michael Wallner
  61.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  62.  * @version   CVS: $Id: gettext.php,v 1.32 2008/02/02 18:05:06 quipo Exp $
  63.  * @link      http://pear.php.net/package/Translation2
  64.  * @see       /docs/gettext_readme.txt for an usage example
  65.  */
  66. {
  67.     // {{{ class vars
  68.  
  69.     /**
  70.      * domain bindings
  71.      * @var array 
  72.      * @access private
  73.      */
  74.     var $_domains = array();
  75.  
  76.     /**
  77.      * @var array 
  78.      * @access private
  79.      */
  80.     var $cachedDomains = array();
  81.  
  82.     /**
  83.      * @var boolean 
  84.      * @access private
  85.      */
  86.     var $_native = false;
  87.  
  88.     // }}}
  89.     // {{{ init
  90.  
  91.     /**
  92.      * Initialize the container
  93.      *
  94.      * @param array $options gettext parameters
  95.      *
  96.      * @return boolean|PEAR_Errorobject if domains INI file doesn't exist
  97.      */
  98.     function init($options)
  99.     {
  100.         $this->_setDefaultOptions();
  101.         $this->_parseOptions($options);
  102.         $this->_native (
  103.             function_exists('gettext'&&
  104.             (strtolower($this->options['file_type']== 'mo'&&
  105.             !$this->options['blank_on_missing']
  106.         );
  107.         
  108.         $this->_domains @parse_ini_file($this->options['domains_path_file']);
  109.         
  110.         if (!$this->_domains{
  111.             return $this->raiseError(sprintf(
  112.                     'Cannot find domains INI file "%s" [%s on line %d]',
  113.                     $this->options['domains_path_file']__FILE____LINE__
  114.                 ),
  115.                 TRANSLATION2_ERROR_CANNOT_FIND_FILE
  116.             );
  117.         }
  118.  
  119.         if ($this->_native{
  120.             foreach ((array) $this->_domains as $domain => $path{
  121.                 bindtextdomain($domain$path);
  122.             }
  123.             textdomain($this->options['default_domain']);
  124.         }
  125.         $this->setLang($this->options['default_lang']);
  126.         
  127.         return true;
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ _setDefaultOptions()
  132.  
  133.     /**
  134.      * Set some default options
  135.      *
  136.      * @access private
  137.      * @return void 
  138.      */
  139.     function _setDefaultOptions()
  140.     {
  141.         $this->options['langs_avail_file']  'langs.ini';
  142.         $this->options['domains_path_file''domains.ini';
  143.         $this->options['default_domain']    'messages';
  144.         $this->options['carriage_return']   "\n";
  145.         $this->options['file_type']         'mo';
  146.         $this->options['default_lang']      'en';
  147.         $this->options['default_encoding']  'iso-8859-1';
  148.         $this->options['blank_on_missing']  = false;
  149.     }
  150.  
  151.     // }}}
  152.     // {{{ _switchLang()
  153.  
  154.     /**
  155.      * Set the given langID
  156.      *
  157.      * @param string $langID new langID
  158.      *
  159.      * @return string previous langID
  160.      * @access private
  161.      */
  162.     function _switchLang($langID)
  163.     {
  164.         $langID  $this->_getLangID($langID);
  165.         $oldLang $this->currentLang['id'];
  166.         $this->setLang($langID);
  167.         return $oldLang;
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ fetchLangs()
  172.  
  173.     /**
  174.      * Fetch the available langs if they're not cached yet.
  175.      *
  176.      * @return void 
  177.      */
  178.     function fetchLangs()
  179.     {
  180.         $this->langs @parse_ini_file($this->options['langs_avail_file']true);
  181.         foreach ((array) $this->langs as $id => $lang{
  182.             $this->langs[$id]['id'$id;
  183.         }
  184.     }
  185.  
  186.     // }}}
  187.     // {{{ setLang()
  188.  
  189.     /**
  190.      * Sets the current lang
  191.      *
  192.      * @param string $langID language ID
  193.      *
  194.      * @return array language data
  195.      */
  196.     function setLang($langID)
  197.     {
  198.         if (!PEAR::isError($langData = parent::setLang($langID))) {
  199.             I18Nv2::setLocale($langID);
  200.         }
  201.         return $langData;
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ getPage()
  206.  
  207.     /**
  208.      * Get all the strings from a domain (parsing the .mo file)
  209.      *
  210.      * @param string $pageID page/group ID
  211.      * @param string $langID language ID
  212.      *
  213.      * @return array|PEAR_Error
  214.      */
  215.     function getPage($pageID = null$langID = null)
  216.     {
  217.         $oldLang $this->_switchLang($langID);
  218.         $curLang $this->currentLang['id'];
  219.         
  220.         if (empty($pageID|| $pageID == TRANSLATION2_DEFAULT_PAGEID{
  221.             $pageID $this->options['default_domain'];
  222.         }
  223.         
  224.         if (isset($this->cachedDomains[$curLang][$pageID])) {
  225.             $this->_switchLang($oldLang);
  226.             return $this->cachedDomains[$curLang][$pageID];
  227.         }
  228.         
  229.         if (!isset($this->_domains[$pageID])) {
  230.             $this->_switchLang($oldLang);
  231.             return $this->raiseError(sprintf(
  232.                     'The domain "%s" was not specified in the domains INI '.
  233.                     'file "%s" [%s on line %d]'$pageID,
  234.                     $this->options['domains_path_file']__FILE____LINE__
  235.                 ),
  236.                 TRANSLATION2_ERROR_DOMAIN_NOT_SET
  237.             );
  238.         }
  239.         
  240.         include_once 'File/Gettext.php';
  241.         $gtFile &File_Gettext::factory($this->options['file_type']);
  242.         
  243.         $path $this->_domains[$pageID.'/'$curLang .'/LC_MESSAGES/';
  244.         $file $path $pageID .'.'$this->options['file_type'];
  245.  
  246.         if (PEAR::isError($e $gtFile->load($file))) {
  247.             if (is_file($file)) {
  248.                 $this->_switchLang($oldLang);
  249.                 return $this->raiseError(sprintf(
  250.                         '%s [%s on line %d]'$e->getMessage()__FILE____LINE__
  251.                     ),
  252.                     TRANSLATION2_ERRORPEAR_ERROR_RETURN
  253.                 );
  254.             }
  255.             $this->_switchLang($oldLang);
  256.             return $this->raiseError(sprintf(
  257.                     'Cannot find file "%s" [%s on line %d]',
  258.                     $file__FILE____LINE__
  259.                 ),
  260.                 TRANSLATION2_ERROR_CANNOT_FIND_FILEPEAR_ERROR_RETURN
  261.             );
  262.         }
  263.         
  264.         $this->cachedDomains[$curLang][$pageID$gtFile->strings;
  265.         $this->_switchLang($oldLang);
  266.         return $gtFile->strings;
  267.     }
  268.  
  269.     // }}}
  270.     // {{{ getOne()
  271.  
  272.     /**
  273.      * Get a single item from the container, without caching the whole page
  274.      *
  275.      * @param string $stringID string ID
  276.      * @param string $pageID   page/group ID
  277.      * @param string $langID   language ID
  278.      *
  279.      * @return string 
  280.      */
  281.     function getOne($stringID$pageID = null$langID = null)
  282.     {
  283.         // native mode
  284.         if ($this->_native{
  285.             $oldLang $this->_switchLang($langID);
  286.             $curLang $this->currentLang['id'];
  287.  
  288.             if (empty($pageID|| $pageID == TRANSLATION2_DEFAULT_PAGEID{
  289.                 $pageID $this->options['default_domain'];
  290.             }
  291.  
  292.             $string dgettext($pageID$stringID);
  293.  
  294.             $this->_switchLang($oldLang);
  295.             return $string;
  296.         }
  297.  
  298.         // use File_Gettext
  299.         $page $this->getPage($pageID$langID);
  300.         if (PEAR::isError($page $this->getPage($pageID$langID))) {
  301.             if ($page->getCode(== TRANSLATION2_ERROR_CANNOT_FIND_FILE{
  302.                 $page = array();
  303.             else {
  304.                 return $this->raiseError($page->getMessage()$page->getCode());
  305.             }
  306.         }
  307.         
  308.         // return original string if there's no translation available
  309.         if (isset($page[$stringID]&& strlen($page[$stringID])) {
  310.             return $page[$stringID];
  311.         else if (false == $this->options['blank_on_missing']{
  312.             return $stringID;
  313.         else {
  314.             return '';
  315.         }
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ getStringID()
  320.  
  321.     /**
  322.      * Get the stringID for the given string
  323.      *
  324.      * @param string $string string
  325.      * @param string $pageID page/group ID
  326.      *
  327.      * @return string|PEAR_Error
  328.      */
  329.     function getStringID($string$pageID = null)
  330.     {
  331.         if (empty($pageID|| $pageID == TRANSLATION2_DEFAULT_PAGEID{
  332.             $pageID $this->options['default_domain'];
  333.         }
  334.         
  335.         if (!array_key_exists($pageID$this->_domains)) {
  336.             return $this->raiseError(sprintf(
  337.                     'The domain "%s" was not specified in the domains '.
  338.                     'INI file "%s" [%s on line %d]'$pageID
  339.                     $this->options['domains_path_file']__FILE____LINE__
  340.                 ),
  341.                 TRANSLATION2_ERROR_DOMAIN_NOT_SET
  342.             );
  343.         }
  344.  
  345.         return array_search($string$this->getPage($pageID));
  346.     }
  347.  
  348.     // }}}
  349. }
  350. ?>

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