Source for file Translation.php
Documentation is available at Translation.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* This source file is subject to version 2.1 of the GNU Lesser General Public
* License, that is bundled with this package in the file COPYING, available
* through the world wide web at the following URI:
* http://www.gnu.org/copyleft/lesser.html.
* @subpackage Presentation
* @author Alessandro Rossini <http://www.alessandrorossini.org>
* @author Graziano Liberati <http://www.liberati.org>
* @copyright 2004-2007 The ZNF Development Team
* @license LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
* @version SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
* @link http://www.zeronotice.org
require_once('ZNF/Config/AppConfig.php');
* <i>ZNF_Presentation_Translation</i> class handles the translations. Returns translated strings by a numeric index.
* You can specify if the \texttt{ZNF_Presentation_Translation} should load
* translations of a specific package or the global ones, typically used in the
* whole application. The first parameter of the constructor of
* \texttt{ZNF_Presentation_Translation} let you to specify the package to use.
* If you pass a string to this parameter the
* \texttt{ZNF_Presentation_Translation} object will load translations files
* from the ``lang'' directory of the specified package. If no string is passed
* to this parameter the \texttt{ZNF_Presentation_Translation} object will load
* translations files from the ``lang'' directory of the root of the application.
* @subpackage Presentation
* @author Alessandro Rossini <http://www.alessandrorossini.org>
* @author Graziano Liberati <http://www.liberati.org>
* @copyright 2004-2007 The ZNF Development Team
* @license LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
* @version SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
* @link http://www.zeronotice.org
* The <i>ZNF_Config_AppConfig</i> instance.
* @var ZNF_Config_AppConfig $_appConfig
* The array of the translation.
* @var array $_translation
* Constructs a new <i>ZNF_Presentation_Translation</i> object.
* @param string $package Name of the package to which the lang refers, leave
* null this parameter to refer to the application lang
* @param string $lang Name of the language to get (en, it, etc.), leave null
* this parameter to refer to the default application lang
public function __construct($package = null, $lang = null)
* Parses and validates the translation files.
* For performance reasons implements a caching mechanism that serializes the
* translations file in an internal structure.
* @param string $package Name of the package to which the lang refers, leave
* null this parameter to refer to the application lang
* @param string $lang Name of the language to get (en, it, etc.), leave null
* this parameter to refer to the default application lang
protected function _parseLang($package = null, $lang = null)
$serializedTimestamp = @filemtime(ZNF::CACHE_DIR . "/{$package}_lang_{$lang}.xml.cache");
if ($xmlTimestamp <= $serializedTimestamp) {
// get and validate the default language xml document
$translationDefaultXml = DOMDocument::load($defaultPath);
// quick hack to make ZNF work as a PEAR package, to be improved
$schemaPath = substr($dirName, 0, strpos($dirName, 'Presentation')) . "xsd/znf-translation.xsd";
if (!$translationDefaultXml->schemaValidate($schemaPath)) {
$translationDefaultXpath = new DOMXPath($translationDefaultXml);
$translationDefaultXpath->registerNamespace('tra', 'http://www.zeronotice.org/ZNF/znf-translation');
// get the default language items
$defaultItems = $translationDefaultXpath->query('/tra:znf-translation/tra:item');
// get and validate the language xml document
$translationXml = DOMDocument::load($path);
if (!$translationXml->schemaValidate($schemaPath)) {
$translationXpath = new DOMXPath($translationXml);
$translationXpath->registerNamespace('tra', 'http://www.zeronotice.org/ZNF/znf-translation');
// build the translation array completing with default translation
foreach ($defaultItems as $current) {
$code = $current->getAttribute('code');
$item = $translationXpath->query('/tra:znf-translation/tra:item[@code="' . $code . '"]');
$translation[$code] = $item->item(0)->getAttribute('text');
$translation[$code] = $current->getAttribute('text');
* Returns the path of a language file.
* @param string $package Name of the package to which the lang refers, leave
* null this parameter to refer to the application lang
* @param string $lang Name of the language to get (en, it, etc.), leave null
* this parameter to refer to the default application lang
protected function _getLangPath($package = null, $lang = null)
// quick hack to make ZNF work as a PEAR package, to be improved
$path = substr($dirName, 0, strpos($dirName, 'Presentation'));
$path = ZNF::MODULES_DIR . "/$package/";
$pathLang = $path . ZNF::LANG_DIR . "/{$lang}.xml";
// not translated to avoid code looping
* Returns the translated message.
* @param string $code Id of the string to translate
public function get($code)
* Destroys the <i>ZNF_Presentation_Translation</i> object.</>
* <i>ZNF_Presentation_TranslationException</i> is the exception type for the <i>ZNF_Presentation_Translation</i> class.
* <i>ZNF_Presentation_TranslationException</i> extends the <i>Exception</i> class of PHP5.
* @subpackage Presentation
* @author Alessandro Rossini <http://www.alessandrorossini.org>
* @author Graziano Liberati <http://www.liberati.org>
* @copyright 2004-2007 The ZNF Development Team
* @license LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
* @version SVN $Id: Translation.php 44 2007-07-22 23:55:30Z evolic $
* @link http://www.zeronotice.org
|