1
0
Fork 0
MySQLDumper/library/Msd/Language.php

190 Zeilen
4,4 KiB
PHP

2011-06-10 21:55:32 +00:00
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage Language
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Language class implemented as singleton
*
* Handles translation of language variables.
*
* @package MySQLDumper
* @subpackage Language
*/
class Msd_Language
{
/**
* Instance
*
* @var Msd_Language
2011-06-10 21:55:32 +00:00
*/
private static $_instance = NULL;
/**
* Translator
*
* @var Zend_Translate
*/
private $_translate = NULL;
/**
* Base directory for language files
*
* @var string
*/
private $_baseLanguageDir = null;
/**
* Constructor gets the configuration params
*
* @return Msd_Language
2011-06-10 21:55:32 +00:00
*/
private function __construct()
2011-06-10 21:55:32 +00:00
{
$config = Msd_Registry::getConfig();
$language = $config->getParam('interface.language', 'en');
2011-06-10 21:55:32 +00:00
$this->loadLanguage($language);
}
/**
* Load new language.
*
* @param string $language New language
*
* @return void
*/
public function loadLanguage($language)
{
2012-08-04 10:40:48 +00:00
$this->_baseLanguageDir = APPLICATION_PATH . '/language/';
$file = $this->_baseLanguageDir . $language . '/lang.php';
$translator = $this->getTranslator();
2011-06-10 21:55:32 +00:00
if ($translator === null) {
$translator = new Zend_Translate('array', $file, $language);
} else {
$translator->setAdapter(
array(
'adapter' => 'array',
'content' => $file,
'locale' => $language
2011-06-10 21:55:32 +00:00
)
);
}
$this->setTranslator($translator);
Zend_Registry::set('Zend_Translate', $translator);
}
2011-06-10 21:55:32 +00:00
/**
* No cloning for singleton
*
* @throws Msd_Exception
*
2011-06-10 21:55:32 +00:00
* @return void
*/
public function __clone()
{
throw new Msd_Exception('Cloning of Msd_Language is not allowed!');
}
/**
* Magic getter to keep syntax in rest of script short
*
* @param mixed $name Name of language var to translate
2011-06-10 21:55:32 +00:00
*
* @return mixed
*/
public function __get($name)
2011-06-10 21:55:32 +00:00
{
$translated = $this->getTranslator()->_($name);
if ($translated == $name && substr($name, 0, 2) == 'L_') {
2011-06-10 21:55:32 +00:00
// no translation found -> remove prefix L_
return substr($name, 2);
2011-06-10 21:55:32 +00:00
}
return $translated;
}
2011-06-10 21:55:32 +00:00
/**
* Returns the single instance
*
* @return Msd_Language
*/
public static function getInstance()
2011-06-10 21:55:32 +00:00
{
if (NULL == self::$_instance) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Translate a Message from Zend_Validate.
*
* @param string $zendMessageId Message ID from Zend_Validate
* @param string $messageText Pre-translatet message
*
* @return string
*/
public function translateZendId($zendMessageId, $messageText = '')
{
if (substr($zendMessageId, 0, 6) == 'access' && $messageText > '') {
2011-06-10 21:55:32 +00:00
// message is already translated by validator access
return $messageText;
}
return $this->_translate->_(
$this->_transformMessageId($zendMessageId)
);
}
/**
* Transforms a message ID in Zend_Validate format into Msd_Language format.
*
* @param string $zendMessageId Message ID from Zend_Validate
*
* @return string
*/
private function _transformMessageId($zendMessageId)
{
$result = preg_replace('/([A-Z])/', '_${1}', $zendMessageId);
$result = strtoupper($result);
return 'L_ZEND_ID_' . $result;
}
/**
* Get Translator
*
* @return Zend_Translate_Adapter
2011-06-10 21:55:32 +00:00
*/
public function getTranslator()
{
return $this->_translate;
}
/**
* Set Translator
*
* @param Zend_Translate $translate
*
* @return void
*/
public function setTranslator(Zend_Translate $translate)
{
$this->_translate = $translate;
}
/**
* Retrieve a list of available languages.
*
* @return array
*/
public function getAvailableLanguages()
{
$lang = array();
include $this->_baseLanguageDir . 'lang_list.php';
return $lang;
}
}