]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/I18n.php
8264aa55c7a4ec41cd5c4490e63f48869522ddae
[galette.git] / galette / lib / Galette / Core / I18n.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * i18n handling
7 *
8 * PHP version 5
9 *
10 * Copyright © 2007-2018 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2007-2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2007-07-06
35 */
36
37 namespace Galette\Core;
38
39 use Analog\Analog;
40
41 /**
42 * i18n handling
43 *
44 * @category Core
45 * @name i18n
46 * @package Galette
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2007-2020 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
51 * @since Available since 0.7dev - 2007-07-06
52 */
53
54 class I18n
55 {
56 private $id;
57 private $longid;
58 private $name;
59 private $abbrev;
60
61 const DEFAULT_LANG = 'fr_FR';
62
63 private $dir = 'lang/';
64 private $path;
65
66 private $rtl_langs = [
67 'ar',
68 'az',
69 'fa',
70 'he',
71 'ur'
72 ];
73
74 /**
75 * Default constructor.
76 * Initialize default language and set environment variables
77 *
78 * @param bool $lang true if there were a language change
79 *
80 * @return void
81 */
82 public function __construct($lang = false)
83 {
84 $this->path = GALETTE_ROOT . $this->dir;
85 $this->guessLangs();
86
87 if (!$lang) {
88 //try to determine user language
89 $dlang = self::DEFAULT_LANG;
90 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
91 $blang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
92 if (substr($blang, 0, 2) == 'fr') {
93 $dlang = 'fr_FR';
94 } elseif (substr($blang, 0, 2) == 'en') {
95 $dlang = 'en_US';
96 } else {
97 $dlang = self::DEFAULT_LANG;
98 }
99 }
100 $this->changeLanguage($dlang);
101 } else {
102 $this->load($lang);
103 }
104 }
105
106 /**
107 * Load language parameters
108 *
109 * @param string $id Identifier for requested language
110 *
111 * @return void
112 */
113 public function changeLanguage($id)
114 {
115 Analog::log('Trying to set locale to ' . $id, Analog::DEBUG);
116 $this->load($id);
117 $this->updateEnv();
118 }
119
120 /**
121 * Update environment according to locale.
122 * Mainly used at app initialization or at login
123 *
124 * @return void
125 */
126 public function updateEnv()
127 {
128 setlocale(LC_ALL, $this->getLongID());
129
130 if (
131 putenv("LANG=" . $this->getLongID())
132 or putenv("LANGUAGE=" . $this->getLongID())
133 or putenv("LC_ALL=" . $this->getLongID())
134 ) {
135 $textdomain = realpath(GALETTE_ROOT . 'lang');
136 //main translation domain
137 $domain = 'galette';
138 bindtextdomain($domain, $textdomain);
139 //set default translation domain and encoding
140 textdomain($domain);
141 bind_textdomain_codeset($domain, 'UTF-8');
142 }
143 }
144
145 /**
146 * Load a language
147 *
148 * @param string $id identifier for the language to load
149 *
150 * @return void
151 */
152 private function load($id)
153 {
154 if (!isset($this->langs[$id])) {
155 $msg = 'Lang ' . $id . ' does not exist, switching to default.';
156 Analog::log($msg, Analog::WARNING);
157 $id = self::DEFAULT_LANG;
158 }
159 $lang = $this->langs[$id];
160 $this->id = $id;
161 $this->longid = $lang['long'];
162 $this->name = $lang['longname'];
163 $this->abbrev = $lang['shortname'];
164 }
165
166 /**
167 * List languages
168 *
169 * @return array list of all active languages
170 */
171 public function getList()
172 {
173 $result = array();
174 foreach (array_keys($this->langs) as $id) {
175 $result[] = new I18n((string)$id);
176 }
177
178 return $result;
179 }
180
181 /**
182 * List languages as simple array
183 *
184 * @return array
185 */
186 public function getArrayList()
187 {
188 $list = $this->getList();
189 $al = array();
190 foreach ($list as $l) {
191 //FIXME: shoudl use mb with sthing like:
192 //$strlen = mb_strlen($string, $encoding);
193 //$firstChar = mb_substr($string, 0, 1, $encoding);
194 //$then = mb_substr($string, 1, $strlen - 1, $encoding);
195 //return mb_strtoupper($firstChar, $encoding) . $then;
196 $al[$l->getID()] = $l->getName();
197 }
198 return $al;
199 }
200
201 /**
202 * Gets language full name from its ID
203 *
204 * @param string $id the language identifier
205 *
206 * @return string name for specified identifier
207 */
208 public function getNameFromId($id)
209 {
210 if (isset($this->langs[$id])) {
211 return $this->langs[$id]['longname'];
212 } else {
213 return str_replace(
214 '%lang',
215 $id,
216 _T('Unknown lang (%lang)')
217 );
218 }
219 }
220
221 /**
222 * Get current id
223 *
224 * @return string current language identifier
225 */
226 public function getID()
227 {
228 return $this->id;
229 }
230
231 /**
232 * Get long identifier
233 *
234 * @return string current language long identifier
235 */
236 public function getLongID()
237 {
238 return $this->longid;
239 }
240
241 /**
242 * Get current name
243 *
244 * @return string current language name
245 */
246 public function getName()
247 {
248 return $this->name;
249 }
250
251 /**
252 * Get current abreviation
253 *
254 * @return string current language abreviation
255 */
256 public function getAbbrev()
257 {
258 return $this->abbrev;
259 }
260
261 /**
262 * Is a string seem to be UTF-8 one ?
263 *
264 * @param string $str string to analyze
265 *
266 * @return boolean
267 */
268 public static function seemUtf8($str)
269 {
270 return mb_check_encoding($str, 'UTF-8');
271 }
272
273 /**
274 * Guess available languages from directories
275 * that are present in the lang directory.
276 *
277 * Will store foud langs in class langs variable and return it.
278 *
279 * @return array
280 */
281 public function guessLangs()
282 {
283 $dir = new \DirectoryIterator($this->path);
284 $langs = [];
285 foreach ($dir as $fileinfo) {
286 if ($fileinfo->isDir() && !$fileinfo->isDot()) {
287 $lang = $fileinfo->getFilename();
288 $real_lang = str_replace('.utf8', '', $lang);
289 $parsed_lang = \Locale::parseLocale($lang);
290
291 $langs[$real_lang] = [
292 'long' => $lang,
293 'shortname' => $parsed_lang['language'],
294 'longname' => ucfirst(
295 \Locale::getDisplayLanguage(
296 $lang,
297 $real_lang
298 )
299 )
300 ];
301 }
302 }
303 ksort($langs);
304 $this->langs = $langs;
305 return $this->langs;
306 }
307
308 /**
309 * Is current language RTL?
310 *
311 * @return boolean
312 */
313 public function isRTL()
314 {
315 return in_array(
316 $this->getAbbrev(),
317 $this->rtl_langs
318 );
319 }
320 }