]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/I18n.php
Fix language detection priority not respected; refs #1603
[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 public 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 $preferred_locales = array_reduce(
92 explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']),
93 function ($res, $el) {
94 list($l, $q) = array_merge(explode(';q=', $el), [1]);
95 $res[$l] = (float) $q;
96 return $res;
97 },
98 []
99 );
100 arsort($preferred_locales);
101
102 foreach (array_keys($preferred_locales) as $preferred_locale) {
103 $short_locale = explode('_', $preferred_locale)[0];
104 foreach (array_keys($this->langs) as $lang) {
105 $short_key = explode('_', $lang)[0];
106 if ($short_key == $short_locale) {
107 $dlang = $lang;
108 break 2;
109 }
110 }
111 }
112 }
113 $this->changeLanguage($dlang);
114 } else {
115 $this->load($lang);
116 }
117 }
118
119 /**
120 * Load language parameters
121 *
122 * @param string $id Identifier for requested language
123 *
124 * @return void
125 */
126 public function changeLanguage($id)
127 {
128 Analog::log('Trying to set locale to ' . $id, Analog::DEBUG);
129 $this->load($id);
130 $this->updateEnv();
131 }
132
133 /**
134 * Update environment according to locale.
135 * Mainly used at app initialization or at login
136 *
137 * @return void
138 */
139 public function updateEnv()
140 {
141 global $translator;
142
143 setlocale(LC_ALL, $this->getLongID());
144
145 if (
146 putenv("LANG=" . $this->getLongID())
147 or putenv("LANGUAGE=" . $this->getLongID())
148 or putenv("LC_ALL=" . $this->getLongID())
149 ) {
150 $textdomain = realpath(GALETTE_ROOT . 'lang');
151 //main translation domain
152 $domain = 'galette';
153 bindtextdomain($domain, $textdomain);
154 //set default translation domain and encoding
155 textdomain($domain);
156 bind_textdomain_codeset($domain, 'UTF-8');
157 }
158 if ($translator) {
159 $translator->setLocale($this->getLongID());
160 }
161 }
162
163 /**
164 * Load a language
165 *
166 * @param string $id identifier for the language to load
167 *
168 * @return void
169 */
170 private function load($id)
171 {
172 if (!isset($this->langs[$id])) {
173 $msg = 'Lang ' . $id . ' does not exist, switching to default.';
174 Analog::log($msg, Analog::WARNING);
175 $id = self::DEFAULT_LANG;
176 }
177 $lang = $this->langs[$id];
178 $this->id = $id;
179 $this->longid = $lang['long'];
180 $this->name = $lang['longname'];
181 $this->abbrev = $lang['shortname'];
182 }
183
184 /**
185 * List languages
186 *
187 * @return array list of all active languages
188 */
189 public function getList()
190 {
191 $result = array();
192 foreach (array_keys($this->langs) as $id) {
193 $result[] = new I18n((string)$id);
194 }
195
196 return $result;
197 }
198
199 /**
200 * List languages as simple array
201 *
202 * @return array
203 */
204 public function getArrayList()
205 {
206 $list = $this->getList();
207 $al = array();
208 foreach ($list as $l) {
209 //FIXME: should use mb with something like:
210 //$strlen = mb_strlen($string, $encoding);
211 //$firstChar = mb_substr($string, 0, 1, $encoding);
212 //$then = mb_substr($string, 1, $strlen - 1, $encoding);
213 //return mb_strtoupper($firstChar, $encoding) . $then;
214 $al[$l->getID()] = $l->getName();
215 }
216 return $al;
217 }
218
219 /**
220 * Gets language full name from its ID
221 *
222 * @param string $id the language identifier
223 *
224 * @return string name for specified identifier
225 */
226 public function getNameFromId($id)
227 {
228 if (isset($this->langs[$id])) {
229 return $this->langs[$id]['longname'];
230 } else {
231 return str_replace(
232 '%lang',
233 $id,
234 _T('Unknown lang (%lang)')
235 );
236 }
237 }
238
239 /**
240 * Get current id
241 *
242 * @return string current language identifier
243 */
244 public function getID()
245 {
246 return $this->id;
247 }
248
249 /**
250 * Get long identifier
251 *
252 * @return string current language long identifier
253 */
254 public function getLongID()
255 {
256 return $this->longid;
257 }
258
259 /**
260 * Get current name
261 *
262 * @return string current language name
263 */
264 public function getName()
265 {
266 return $this->name;
267 }
268
269 /**
270 * Get current abreviation
271 *
272 * @return string current language abreviation
273 */
274 public function getAbbrev()
275 {
276 return $this->abbrev;
277 }
278
279 /**
280 * Is a string seem to be UTF-8 one ?
281 *
282 * @param string $str string to analyze
283 *
284 * @return boolean
285 */
286 public static function seemUtf8($str)
287 {
288 return mb_check_encoding($str, 'UTF-8');
289 }
290
291 /**
292 * Guess available languages from directories
293 * that are present in the lang directory.
294 *
295 * Will store foud langs in class langs variable and return it.
296 *
297 * @return array
298 */
299 public function guessLangs()
300 {
301 $dir = new \DirectoryIterator($this->path);
302 $langs = [];
303 foreach ($dir as $fileinfo) {
304 if ($fileinfo->isDir() && !$fileinfo->isDot()) {
305 $lang = $fileinfo->getFilename();
306 $real_lang = str_replace('.utf8', '', $lang);
307 $parsed_lang = \Locale::parseLocale($lang);
308
309 $langs[$real_lang] = [
310 'long' => $lang,
311 'shortname' => $parsed_lang['language'] ?? '',
312 'longname' => ucfirst(
313 \Locale::getDisplayLanguage(
314 $lang,
315 $real_lang
316 )
317 )
318 ];
319 }
320 }
321 ksort($langs);
322 $this->langs = $langs;
323 return $this->langs;
324 }
325
326 /**
327 * Is current language RTL?
328 *
329 * @return boolean
330 */
331 public function isRTL()
332 {
333 return in_array(
334 $this->getAbbrev(),
335 $this->rtl_langs
336 );
337 }
338 }