initial setup of framework code
[cacert-mgr.git] / manager / library / i18n / I18n.php
1 <?php
2 /**
3 * encapsulate Zend_Translate within an singleton class
4 * @author markus
5 * $Id: I18n.php 33 2009-12-10 15:08:38Z markus $
6 */
7
8 require_once('l10n/L10n.php');
9
10 class I18n {
11 /**
12 * static pointer to instance
13 * @var array(I18n)
14 */
15 private static $instance = null;
16
17 /**
18 * config object
19 * @var Zend_Translate
20 */
21 private $translate = null;
22
23 /**
24 * make new translate
25 */
26 protected function __construct() {
27 $options = array(
28 'log' => Log::Log(),
29 'logUntranslated' => true
30 );
31
32 $locale = L10n::getInstance();
33 $supported = $locale->getBrowser();
34 arsort($supported, SORT_NUMERIC);
35
36 $file = '';
37 foreach ($supported as $loc => $val) {
38 if (file_exists(LOCALE_PATH . '/' . $loc . '/locale.php')) {
39 $file = LOCALE_PATH . '/' . $loc . '/locale.php';
40 $locale->setLocale($loc);
41 break;
42 }
43 }
44
45 if ($file == '' && file_exists(LOCALE_PATH . '/en_US/locale.php')) {
46 $file = LOCALE_PATH . '/en_US/locale.php';
47 $locale->setLocale('en_US');
48 }
49
50 if ($file != '') {
51 $this->translate = new Zend_Translate(Zend_Translate::AN_ARRAY, $file, $locale->getLanguage(), $options);
52 #Log::Log()->debug('locale ' . $locale->getLanguage() . '_' .$locale->getRegion() . ' loaded');
53 }
54 else
55 throw new Exception(__METHOD__ . ': no translation files available');
56 }
57
58 /**
59 * get already existing instance, make new instance or throw an exception
60 * @return I18n
61 */
62 public static function getInstance() {
63 if (self::$instance === null) {
64 self::$instance = new I18n();
65 }
66
67 return self::$instance;
68 }
69
70 /**
71 * return the Zend_Translate object
72 * @return Zend_Translate
73 */
74 public static function getTranslate() {
75 return self::getInstance()->translate;
76 }
77
78 /**
79 * map _ to translate
80 * @param unknown_type $text
81 * @param unknown_type $locale
82 */
83 public function _($text, $locale = null) {
84 return self::getInstance()->translate->_($text, $locale);
85 }
86
87 /**
88 * magic __call dispatches all unknown methods to Zend_Translate
89 * @param unknown_type $method
90 * @param unknown_type $arguments
91 */
92 public function __call($method, $arguments) {
93 return call_user_func_array(array($this->translate, $method), $arguments);
94 }
95 }