3 * FusionForge localisation
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2003-2004, Guillaume Smet
7 * Copyright 2007-2009, Roland Mas
8 * Copyright 2012, Franck Villaume - TrivialDev
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * choose_language_from_context - find the most appropriate language
29 * @return string the language class name.
31 function choose_language_from_context () {
33 Determine which language to use
35 It depends on whether the user has set a cookie or not using
36 the account page or the left-hand nav or how their browser is
37 set or whether they are logged in or not
39 if logged in, use language from users table
40 else check for cookie and use that value if valid
41 if no cookie check browser preference and use that language if valid
42 else just use default language as configured for the installation
45 if (!isset($_SERVER['SERVER_SOFTWARE'])) {
46 // In command-line scripts
47 if (forge_get_config('default_language')) {
48 return forge_get_config('default_language') ;
53 // Logged in -> use preferences
54 if (session_loggedin()) {
55 $user = session_get_user () ;
56 return lang_id_to_language_name ($user->getLanguage()) ;
59 // Cookie present -> use that
60 $cookie_language_id = getIntFromCookie ('cookie_language_id') ;
61 if ($cookie_language_id) {
62 return lang_id_to_language_name ($cookie_language_id) ;
65 // Try with the browser's preferred language
66 $ranges = explode (',', getStringFromServer ('HTTP_ACCEPT_LANGUAGE')) ;
67 $languages = array() ; $lcount = count ($ranges) ;
69 $delta = 0.009/$lcount ;
72 foreach ($ranges as $p) {
73 if (preg_match ('/(.*);q=(.*)/', $p, $matches)) {
76 $languages[$l] = $w + $delta * ($lcount - $i) ;
78 $languages[$p] = 1 + $delta * ($lcount - $i) ;
82 arsort($languages, SORT_NUMERIC);
83 $languages = array_keys($languages);
85 for( $i=0, $max = sizeof($languages); $i < $max; $i++){
86 $languageCode = $languages[$i];
87 $res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
88 if (db_numrows($res) > 0) {
89 return db_result($res,0,'classname');
91 // If that didn't work, check if we have sublanguage specifier
92 // If so, try to strip it and look for for main language only
93 if (strstr($languageCode, '-')) {
94 $languageCode = substr($languageCode, 0, 2);
95 $res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
96 if (db_numrows($res) > 0) {
97 return db_result($res,0,'classname');
103 // Okay, let's use the site-wide default language
104 if (forge_get_config('default_language')) {
105 return forge_get_config('default_language') ;
108 // Still no match? Really?
112 function language_name_to_locale_code ($lang) {
115 'Bulgarian' => 'bg_BG',
116 'Catalan' => 'ca_ES',
117 'Chinese' => 'zh_TW',
119 'English' => 'en_US',
125 'Indonesian' => 'id_ID',
126 'Italian' => 'it_IT',
127 'Japanese' => 'ja_JP',
129 'Norwegian' => 'nb_NO',
131 'PortugueseBrazilian' => 'pt_BR',
132 'Portuguese' => 'pt_PT',
133 'Russian' => 'ru_RU',
134 'SimplifiedChinese' => 'zh_CN',
135 'Spanish' => 'es_ES',
136 'Swedish' => 'sv_SE',
139 return $langmap[$lang] ;
142 function locale_code_to_language_name ($loc) {
145 'bg_BG' => 'Bulgarian',
146 'ca_ES' => 'Catalan',
147 'zh_TW' => 'Chinese',
149 'en_US' => 'English',
155 'id_ID' => 'Indonesian',
156 'it_IT' => 'Italian',
157 'ja_JP' => 'Japanese',
159 'nb_NO' => 'Norwegian',
161 'pt_BR' => 'PortugueseBrazilian',
162 'pt_PT' => 'Portuguese',
163 'ru_RU' => 'Russian',
164 'zh_CN' => 'SimplifiedChinese',
165 'es_ES' => 'Spanish',
166 'sv_SE' => 'Swedish',
169 return $localemap[$loc] ;
172 function lang_id_to_language_name ($lang_id) {
173 $res = db_query_params ('SELECT classname FROM supported_languages WHERE language_id=$1', array ($lang_id));
174 return db_result($res, 0, 'classname');
177 function language_name_to_lang_id ($language) {
178 $res = db_query_params ('SELECT language_id FROM supported_languages WHERE classname=$1', array ($language)) ;
179 return db_result($res, 0, 'language_id');
182 function setup_gettext_from_context() {
183 setup_gettext_from_langname (choose_language_from_context ());
186 function setup_gettext_for_user ($user) {
187 setup_gettext_from_lang_id ($user->getLanguage());
190 function setup_gettext_from_lang_id ($lang_id) {
191 $lang = lang_id_to_language_name ($lang_id) ;
192 setup_gettext_from_langname($lang) ;
195 function setup_gettext_from_langname ($lang) {
196 $locale[] = language_name_to_locale_code($lang).'.utf8';
197 $locale[] = language_name_to_locale_code($lang).'.UTF-8';
198 setup_gettext_from_locale ($locale) ;
201 function setup_gettext_from_sys_lang () {
204 if (forge_get_config('default_language')) {
205 $lang = forge_get_config('default_language') ;
208 $locale[] = language_name_to_locale_code($lang).'.utf8';
209 $locale[] = language_name_to_locale_code($lang).'.UTF-8';
210 setup_gettext_from_locale ($locale) ;
214 * setup_gettext_from_locale() - call setlocales to set up language used by gettext
216 * @param array locales (.utf8 + .UTF-8)
218 function setup_gettext_from_locale ($locale) {
219 setlocale(LC_ALL, $locale);
221 if (forge_get_config('gettext_path')) {
222 bindtextdomain('fusionforge', forge_get_config('gettext_path'));
224 bindtextdomain('fusionforge', '/usr/share/locale/');
226 textdomain('fusionforge');
231 // c-file-style: "bsd"