]>
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-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
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-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
62 const DEFAULT_LANG
= 'fr_FR';
64 private $dir = 'lang/';
68 * Default constructor.
69 * Initialize default language and set environment variables
71 * @param bool $lang true if there were a language change
75 public function __construct($lang = false)
77 $this->path
= GALETTE_ROOT
. $this->dir
;
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') {
87 } elseif (substr($blang, 0, 2) == 'en') {
90 $dlang = self
::DEFAULT_LANG
;
93 $this->changeLanguage($dlang);
100 * Load language parameters
102 * @param string $id Identifier for requested language
106 public function changeLanguage($id)
108 Analog
::log('Trying to set locale to ' . $id, Analog
::DEBUG
);
114 * Update environment according to locale.
115 * Mainly used at app initialization or at login
119 public function updateEnv()
121 setlocale(LC_ALL
, $this->getLongID());
123 if (putenv("LANG=" . $this->getLongID())
124 or putenv("LANGUAGE=" . $this->getLongID())
125 or putenv("LC_ALL=" . $this->getLongID())
127 $textdomain = realpath(GALETTE_ROOT
. 'lang');
128 //main translation domain
130 bindtextdomain($domain, $textdomain);
131 //set default translation domain and encoding
133 bind_textdomain_codeset($domain, 'UTF-8');
140 * @param string $id identifier for the language to load
144 private function load($id)
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
;
151 $lang = $this->langs
[$id];
153 $this->longid
= $lang['long'];
154 $this->name
= $lang['longname'];
155 $this->abbrev
= $lang['shortname'];
161 * @return array list of all active languages
163 public function getList()
166 foreach (array_keys($this->langs
) as $id) {
167 $result[] = new I18n((string)$id);
174 * List languages as simple array
178 public function getArrayList()
180 $list = $this->getList();
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();
194 * Gets language full name from its ID
196 * @param string $id the language identifier
198 * @return string name for specified identifier
200 public function getNameFromId($id)
202 if (isset($this->langs
[$id])) {
203 return $this->langs
[$id]['longname'];
208 _T('Unknown lang (%lang)')
216 * @return string current language identifier
218 public function getID()
224 * Get long identifier
226 * @return string current language long identifier
228 public function getLongID()
230 return $this->longid
;
236 * @return string current language name
238 public function getName()
244 * Get current abreviation
246 * @return string current language abreviation
248 public function getAbbrev()
250 return $this->abbrev
;
254 * Is a string seem to be UTF-8 one ?
256 * @param string $str string to analyze
260 public static function seemUtf8($str)
262 return mb_check_encoding($str, 'UTF-8');
266 * Guess available languages from directories
267 * that are present in the lang directory.
269 * Will store foud langs in class langs variable and return it.
273 public function guessLangs()
275 $dir = new \
DirectoryIterator($this->path
);
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);
283 $langs[$real_lang] = [
285 'shortname' => $parsed_lang['language'],
286 'longname' => ucfirst(
287 \Locale
::getDisplayLanguage(
296 $this->langs
= $langs;