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