This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate.php
154 lines (136 loc) · 4.82 KB
/
Translate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Copyright (C) 2010 Fabian Becker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace MongoUI\Core;
class Translate {
const DEFAULT_LANG = "en_US";
private static $instance = null;
private $translations = array();
private function __construct() {
$this->loadDefaultLanguage();
}
/**
* @return MongoUI_Translate
*/
static public function getInstance() {
if (self::$instance == null) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
public function loadLanguage($language) {
// Default language is already loaded.
if(self::DEFAULT_LANG == $language)
return;
$translation = $this->loadLanguageFromXML($language);
$this->mergeTranslationArray($translation);
$this->setLocale();
}
protected function loadDefaultLanguage() {
$this->translations = $this->loadLanguageFromXML(self::DEFAULT_LANG);
}
/**
* Returns translated string or given message if translation is not found.
*
* @param string Translation string index
* @param array $args sprintf arguments
* @return string
*/
public function getTranslation($string, $args = array()) {
if (!is_array($args)) {
$args = array($args);
}
if (isset($this->translations[$string])) {
$string = $this->translations[$string];
}
if (count($args) == 0) {
return $string;
}
return vsprintf($string, $args);
}
/**
* @return string the language filename prefix, eg 'en_US' for english
* @throws exception if the language set is not a valid filename
*/
public function getLanguageToLoad() {
$language = \MongoUI\Core\Common::getRequestVar('lang', self::DEFAULT_LANG, 'string');
if ($this->isValidLanguage($language)) {
return $language;
} else {
throw new \Exception("The language selected ('$language') is not a valid language file ");
}
}
/**
* Returns a list of all available languages.
*
* @staticvar string $languages Stores the array of languages.
* @return array [Locale,Name] pairs.
*/
public function getAvailableLanguages() {
static $languages = null;
if(!is_null($languages)) {
return $languages;
}
foreach(glob(MONGOUI_ROOT.'/lang/*.xml') as $file) {
$data = new \SimpleXMLElement($file, NULL, TRUE);
$name = $data->xpath("//label[@index='General.Language.OriginalName']");
$code = $data->xpath("/language/@code");
$languages[] = array((string)$code[0], (string)$name[0]);
}
return $languages;
}
/**
* Returns whether a language exists.
*
* @return boolean if the language exists or not
* @param string $code
*/
public function isValidLanguage($code) {
$file = MONGOUI_ROOT . '/lang/' . $code . '.xml';
return is_readable($file);
}
private function loadLanguageFromXML($language) {
$path = MONGOUI_ROOT . '/lang/' . $language . '.xml';
if (!file_exists($path)) {
throw new \InvalidArgumentException("Language file for language {$language} does not exists!");
}
$xml = new \SimpleXMLElement($path, NULL, TRUE);
$translations = array();
foreach ($xml->children() as $label) {
$index = (string) $label['index'];
$translations[$index] = (string) $label;
}
return $translations;
}
private function mergeTranslationArray($translation) {
// Merges a new translation into the existing translation array.
// Using array_filter to filter out empty elements.
$this->translations = array_merge($this->translations, array_filter($translation, 'strlen'));
}
/**
* Set locale
*
* @see http://php.net/setlocale
*/
private function setLocale() {
$locale = $this->translations['General.Language.Locale'];
$locale_variant = str_replace('UTF-8', 'UTF8', $locale);
setlocale(LC_ALL, $locale, $locale_variant);
setlocale(LC_CTYPE, '');
}
}