phpDocumentor ZNF
Presentation
[ class tree: ZNF ] [ index: ZNF ] [ all elements ]

Source for file Translation.php

Documentation is available at Translation.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * PHP version 5
  5.  *
  6.  * This source file is subject to version 2.1 of the GNU Lesser General Public
  7.  * License, that is bundled with this package in the file COPYING, available
  8.  * through the world wide web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html.
  10.  *
  11.  * @package    ZNF
  12.  * @subpackage Presentation
  13.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  14.  * @author     Graziano Liberati <http://www.liberati.org>
  15.  * @copyright  2004-2007 The ZNF Development Team
  16.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  17.  * @version    SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
  18.  * @since      Release 0.5.0
  19.  * @link       http://www.zeronotice.org
  20.  */
  21.  
  22. require_once('ZNF/Config/AppConfig.php');
  23.  
  24. /**
  25.  * <i>ZNF_Presentation_Translation</i> class handles the translations. Returns translated strings by a numeric index.
  26.  *
  27.  * You can specify if the \texttt{ZNF_Presentation_Translation} should load
  28.  * translations of a specific package or the global ones, typically used in the
  29.  * whole application. The first parameter of the constructor of
  30.  * \texttt{ZNF_Presentation_Translation} let you to specify the package to use.
  31.  * If you pass a string to this parameter the
  32.  * \texttt{ZNF_Presentation_Translation} object will load translations files
  33.  * from the ``lang'' directory of the specified package. If no string is passed
  34.  * to this parameter the \texttt{ZNF_Presentation_Translation} object will load
  35.  * translations files from the ``lang'' directory of the root of the application.
  36.  *
  37.  * @access     public
  38.  * @package    ZNF
  39.  * @subpackage Presentation
  40.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  41.  * @author     Graziano Liberati <http://www.liberati.org>
  42.  * @copyright  2004-2007 The ZNF Development Team
  43.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  44.  * @version    SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
  45.  * @since      Release 0.5.0
  46.  * @link       http://www.zeronotice.org
  47.  */
  48. {
  49.  
  50.     /**
  51.      * The <i>ZNF_Config_AppConfig</i> instance.
  52.      *
  53.      * @access protected
  54.      * @var ZNF_Config_AppConfig $_appConfig 
  55.      */
  56.     protected $_appConfig;
  57.  
  58.     /**
  59.      * The array of the translation.
  60.      *
  61.      * @access protected
  62.      * @var array $_translation 
  63.      */
  64.     protected $_translation;
  65.  
  66.     /**
  67.      * Constructs a new <i>ZNF_Presentation_Translation</i> object.
  68.      *
  69.      * @access public
  70.      * @param string $package Name of the package to which the lang refers, leave
  71.      *                         null this parameter to refer to the application lang
  72.      *                         path
  73.      * @param string $lang Name of the language to get (en, it, etc.), leave null
  74.      *                      this parameter to refer to the default application lang
  75.      */
  76.     public function __construct($package null$lang null)
  77.     {
  78.         $this->_appConfig = ZNF_Config_AppConfig::getInstance();
  79.         $this->_parseLang($package$lang);
  80.     }
  81.  
  82.     /**
  83.      * Parses and validates the translation files.
  84.      *
  85.      * For performance reasons implements a caching mechanism that serializes the
  86.      * translations file in an internal structure.
  87.      *
  88.      * @access protected
  89.      * @param string $package Name of the package to which the lang refers, leave
  90.      *                         null this parameter to refer to the application lang
  91.      *                         path
  92.      * @param string $lang Name of the language to get (en, it, etc.), leave null
  93.      *                      this parameter to refer to the default application lang
  94.      */
  95.     protected function _parseLang($package null$lang null)
  96.     {
  97.         $path $this->_getLangPath($package$lang);
  98.         $xmlTimestamp filemtime($path);
  99.         $serializedTimestamp @filemtime(ZNF::CACHE_DIR "/{$package}_lang_{$lang}.xml.cache");
  100.  
  101.         // check the timestamp
  102.         if ($xmlTimestamp <= $serializedTimestamp{
  103.             $this->_translation = unserialize(file_get_contents(ZNF::CACHE_DIR "/{$package}_lang_{$lang}.xml.cache"));
  104.         else {
  105.             // get and validate the default language xml document
  106.             $defaultPath $this->_getLangPath($package$this->_appConfig->getLang());
  107.             $translationDefaultXml DOMDocument::load($defaultPath);
  108.  
  109.             // quick hack to make ZNF work as a PEAR package, to be improved
  110.             $dirName dirname(__FILE__);
  111.             $schemaPath substr($dirName0strpos($dirName'Presentation')) "xsd/znf-translation.xsd";
  112.             if (!$translationDefaultXml->schemaValidate($schemaPath)) {
  113.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  114.                 throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadTranslation'));
  115.             }
  116.  
  117.             $translationDefaultXpath new DOMXPath($translationDefaultXml);
  118.             $translationDefaultXpath->registerNamespace('tra''http://www.zeronotice.org/ZNF/znf-translation');
  119.  
  120.             // get the default language items
  121.             $defaultItems $translationDefaultXpath->query('/tra:znf-translation/tra:item');
  122.  
  123.             // get and validate the language xml document
  124.             $translationXml DOMDocument::load($path);
  125.  
  126.             if (!$translationXml->schemaValidate($schemaPath)) {
  127.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  128.                 throw new ZNF_Action_RequestProcessorException($translation->get('errorLoadTranslation'));
  129.             }
  130.  
  131.             $translationXpath new DOMXPath($translationXml);
  132.             $translationXpath->registerNamespace('tra''http://www.zeronotice.org/ZNF/znf-translation');
  133.             $translation array();
  134.  
  135.             // build the translation array completing with default translation
  136.             foreach ($defaultItems as $current{
  137.                 $code $current->getAttribute('code');
  138.                 $item $translationXpath->query('/tra:znf-translation/tra:item[@code="' $code '"]');
  139.  
  140.                 if ($item->length{
  141.                     $translation[$code$item->item(0)->getAttribute('text');
  142.                 else {
  143.                     $translation[$code$current->getAttribute('text');
  144.                 }
  145.  
  146.             }
  147.  
  148.             file_put_contents(ZNF::CACHE_DIR "/{$package}_lang_{$lang}.xml.cache"serialize($translation));
  149.             $this->_translation = $translation;
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Returns the path of a language file.
  155.      *
  156.      * @access protected
  157.      * @param string $package Name of the package to which the lang refers, leave
  158.      *                         null this parameter to refer to the application lang
  159.      *                         path
  160.      * @param string $lang Name of the language to get (en, it, etc.), leave null
  161.      *                      this parameter to refer to the default application lang
  162.      * @return string 
  163.      */
  164.     protected function _getLangPath($package null$lang null)
  165.     {
  166.         $path null;
  167.  
  168.         // quick hack to make ZNF work as a PEAR package, to be improved
  169.         if ($package == 'ZNF'{
  170.             $dirName dirname(__FILE__);
  171.             $path substr($dirName0strpos($dirName'Presentation'));
  172.         elseif ($package{
  173.             $path =  ZNF::MODULES_DIR "/$package/";
  174.         }
  175.  
  176.         if ($lang{
  177.             $pathLang $path ZNF::LANG_DIR "/{$lang}.xml";
  178.  
  179.             if (is_file($pathLang)) {
  180.                 return $pathLang;
  181.             }
  182.  
  183.         }
  184.  
  185.         $pathDefault $path ZNF::LANG_DIR "/{$this->_appConfig->getLang()}.xml";
  186.  
  187.         if (!is_file($pathDefault)) {
  188.             // not translated to avoid code looping
  189.             throw new ZNF_Presentation_TranslationException('Error loading default language.');
  190.         }
  191.  
  192.         return $pathDefault;
  193.     }
  194.  
  195.     /**
  196.      * Returns the translated message.
  197.      *
  198.      * @access public
  199.      * @param string $code Id of the string to translate
  200.      * @return string 
  201.      */
  202.     public function get($code)
  203.     {
  204.         if (isset($this->_translation[$code])) {
  205.             return $this->_translation[$code];
  206.         else {
  207.             return null;
  208.         }
  209.     }
  210.  
  211.     /**
  212.      * Destroys the <i>ZNF_Presentation_Translation</i> object.</>
  213.      *
  214.      * @access public
  215.      */
  216.     public function __destruct()
  217.     {
  218.     }
  219.  
  220. }
  221.  
  222. /**
  223.  * <i>ZNF_Presentation_TranslationException</i> is the exception type for the <i>ZNF_Presentation_Translation</i> class.
  224.  *
  225.  * <i>ZNF_Presentation_TranslationException</i> extends the <i>Exception</i> class of PHP5.
  226.  *
  227.  * @access     public
  228.  * @package    ZNF
  229.  * @subpackage Presentation
  230.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  231.  * @author     Graziano Liberati <http://www.liberati.org>
  232.  * @copyright  2004-2007 The ZNF Development Team
  233.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  234.  * @version    SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
  235.  * @since      Release 0.5.0
  236.  * @link       http://www.zeronotice.org
  237.  */
  238. class ZNF_Presentation_TranslationException extends Exception
  239. {
  240. }
  241.  
  242. ?>

Documentation generated on Wed, 14 Nov 2007 23:47:41 +0100 by phpDocumentor 1.4.0