]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/I18n.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2007-2018 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2007-07-06
38 namespace Galette\Core
;
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2007-2020 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
62 const DEFAULT_LANG
= 'fr_FR';
64 private $dir = 'lang/';
67 private $rtl_langs = [
76 * Default constructor.
77 * Initialize default language and set environment variables
79 * @param bool $lang true if there were a language change
83 public function __construct($lang = false)
85 $this->path
= GALETTE_ROOT
. $this->dir
;
89 //try to determine user language
90 $dlang = self
::DEFAULT_LANG
;
91 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
92 $blang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
93 if (substr($blang, 0, 2) == 'fr') {
95 } elseif (substr($blang, 0, 2) == 'en') {
98 $dlang = self
::DEFAULT_LANG
;
101 $this->changeLanguage($dlang);
108 * Load language parameters
110 * @param string $id Identifier for requested language
114 public function changeLanguage($id)
116 Analog
::log('Trying to set locale to ' . $id, Analog
::DEBUG
);
122 * Update environment according to locale.
123 * Mainly used at app initialization or at login
127 public function updateEnv()
129 setlocale(LC_ALL
, $this->getLongID());
132 putenv("LANG=" . $this->getLongID())
133 or putenv("LANGUAGE=" . $this->getLongID())
134 or putenv("LC_ALL=" . $this->getLongID())
136 $textdomain = realpath(GALETTE_ROOT
. 'lang');
137 //main translation domain
139 bindtextdomain($domain, $textdomain);
140 //set default translation domain and encoding
142 bind_textdomain_codeset($domain, 'UTF-8');
149 * @param string $id identifier for the language to load
153 private function load($id)
155 if (!isset($this->langs
[$id])) {
156 $msg = 'Lang ' . $id . ' does not exist, switching to default.';
157 Analog
::log($msg, Analog
::WARNING
);
158 $id = self
::DEFAULT_LANG
;
160 $lang = $this->langs
[$id];
162 $this->longid
= $lang['long'];
163 $this->name
= $lang['longname'];
164 $this->abbrev
= $lang['shortname'];
170 * @return array list of all active languages
172 public function getList()
175 foreach (array_keys($this->langs
) as $id) {
176 $result[] = new I18n((string)$id);
183 * List languages as simple array
187 public function getArrayList()
189 $list = $this->getList();
191 foreach ($list as $l) {
192 //FIXME: shoudl use mb with sthing like:
193 //$strlen = mb_strlen($string, $encoding);
194 //$firstChar = mb_substr($string, 0, 1, $encoding);
195 //$then = mb_substr($string, 1, $strlen - 1, $encoding);
196 //return mb_strtoupper($firstChar, $encoding) . $then;
197 $al[$l->getID()] = $l->getName();
203 * Gets language full name from its ID
205 * @param string $id the language identifier
207 * @return string name for specified identifier
209 public function getNameFromId($id)
211 if (isset($this->langs
[$id])) {
212 return $this->langs
[$id]['longname'];
217 _T('Unknown lang (%lang)')
225 * @return string current language identifier
227 public function getID()
233 * Get long identifier
235 * @return string current language long identifier
237 public function getLongID()
239 return $this->longid
;
245 * @return string current language name
247 public function getName()
253 * Get current abreviation
255 * @return string current language abreviation
257 public function getAbbrev()
259 return $this->abbrev
;
263 * Is a string seem to be UTF-8 one ?
265 * @param string $str string to analyze
269 public static function seemUtf8($str)
271 return mb_check_encoding($str, 'UTF-8');
275 * Guess available languages from directories
276 * that are present in the lang directory.
278 * Will store foud langs in class langs variable and return it.
282 public function guessLangs()
284 $dir = new \
DirectoryIterator($this->path
);
286 foreach ($dir as $fileinfo) {
287 if ($fileinfo->isDir() && !$fileinfo->isDot()) {
288 $lang = $fileinfo->getFilename();
289 $real_lang = str_replace('.utf8', '', $lang);
290 $parsed_lang = \Locale
::parseLocale($lang);
292 $langs[$real_lang] = [
294 'shortname' => $parsed_lang['language'],
295 'longname' => ucfirst(
296 \Locale
::getDisplayLanguage(
305 $this->langs
= $langs;
310 * Is current language RTL?
314 public function isRTL()