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