cacert-testmgr/manager/library/i18n/I18n.php

96 lines
2.5 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* encapsulate Zend_Translate within an singleton class
* @author markus
* $Id: I18n.php 33 2009-12-10 15:08:38Z markus $
*/
require_once('l10n/L10n.php');
class I18n {
2015-08-09 17:12:51 +00:00
/**
* static pointer to instance
* @var array(I18n)
*/
private static $instance = null;
2015-08-09 17:12:51 +00:00
/**
* config object
* @var Zend_Translate
*/
private $translate = null;
2015-08-09 17:12:51 +00:00
/**
* make new translate
*/
protected function __construct() {
$options = array(
'log' => Log::Log(),
'logUntranslated' => false
);
2015-08-09 17:12:51 +00:00
$locale = L10n::getInstance();
$supported = $locale->getBrowser();
arsort($supported, SORT_NUMERIC);
2015-08-09 17:12:51 +00:00
$file = '';
foreach ($supported as $loc => $val) {
if (file_exists(LOCALE_PATH . '/' . $loc . '/locale.php')) {
$file = LOCALE_PATH . '/' . $loc . '/locale.php';
$locale->setLocale($loc);
break;
}
}
2015-08-09 17:12:51 +00:00
if ($file == '' && file_exists(LOCALE_PATH . '/en_US/locale.php')) {
$file = LOCALE_PATH . '/en_US/locale.php';
$locale->setLocale('en_US');
}
2015-08-09 17:12:51 +00:00
if ($file != '') {
$this->translate = new Zend_Translate(Zend_Translate::AN_ARRAY, $file, $locale->getLanguage(), $options);
#Log::Log()->debug('locale ' . $locale->getLanguage() . '_' .$locale->getRegion() . ' loaded');
}
else
throw new Exception(__METHOD__ . ': no translation files available');
}
2015-08-09 17:12:51 +00:00
/**
* get already existing instance, make new instance or throw an exception
* @return I18n
*/
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new I18n();
}
2015-08-09 17:12:51 +00:00
return self::$instance;
}
2015-08-09 17:12:51 +00:00
/**
* return the Zend_Translate object
* @return Zend_Translate
*/
public static function getTranslate() {
return self::getInstance()->translate;
}
2015-08-09 17:12:51 +00:00
/**
* map _ to translate
* @param unknown_type $text
* @param unknown_type $locale
*/
public function _($text, $locale = null) {
return self::getInstance()->translate->_($text, $locale);
}
2015-08-09 17:12:51 +00:00
/**
* magic __call dispatches all unknown methods to Zend_Translate
* @param unknown_type $method
* @param unknown_type $arguments
*/
public function __call($method, $arguments) {
return call_user_func_array(array($this->translate, $method), $arguments);
}
}