3 * FusionForge localisation
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2003-2004, Guillaume Smet
7 * Copyright 2007-2009, Roland Mas
9 * This file is part of FusionForge.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published
13 * by the Free Software Foundation; either version 2 of the License,
14 * or (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 * choose_language_from_context - find the most appropriate language
30 * @return string the language class name.
32 function choose_language_from_context () {
34 Determine which language to use
36 It depends on whether the user has set a cookie or not using
37 the account page or the left-hand nav or how their browser is
38 set or whether they are logged in or not
40 if logged in, use language from users table
41 else check for cookie and use that value if valid
42 if no cookie check browser preference and use that language if valid
43 else just use default language as configured for the installation
46 // Logged in -> use preferences
47 if (session_loggedin()) {
48 $user = session_get_user () ;
49 return lang_id_to_language_name ($user->getLanguage()) ;
52 // Cookie present -> use that
53 $cookie_language_id = getIntFromCookie ('cookie_language_id') ;
54 if ($cookie_language_id) {
55 return lang_id_to_language_name ($cookie_language_id) ;
58 // Try with the browser's preferred language
59 $ranges = explode (',', getStringFromServer ('HTTP_ACCEPT_LANGUAGE')) ;
60 $languages = array() ; $lcount = count ($ranges) ;
62 $delta = 0.009/$lcount ;
65 foreach ($ranges as $p) {
66 if (preg_match ('/(.*);q=(.*)/', $p, $matches)) {
69 $languages[$l] = $w + $delta * ($lcount - $i) ;
71 $languages[$p] = 1 + $delta * ($lcount - $i) ;
75 arsort($languages, SORT_NUMERIC);
76 $languages = array_keys($languages);
78 for( $i=0, $max = sizeof($languages); $i < $max; $i++){
79 $languageCode = $languages[$i];
80 $res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
81 if (db_numrows($res) > 0) {
82 return db_result($res,0,'classname');
84 // If that didn't work, check if we have sublanguage specifier
85 // If so, try to strip it and look for for main language only
86 if (strstr($languageCode, '-')) {
87 $languageCode = substr($languageCode, 0, 2);
88 $res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
89 if (db_numrows($res) > 0) {
90 return db_result($res,0,'classname');
96 // Okay, let's use the site-wide default language
97 if (forge_get_config('default_language')) {
98 return forge_get_config('default_language') ;
101 // Still no match? Really?
105 function language_name_to_locale_code ($lang) {
108 'Bulgarian' => 'bg_BG',
109 'Catalan' => 'ca_ES',
110 'Chinese' => 'zh_TW',
112 'English' => 'en_US',
118 'Indonesian' => 'id_ID',
119 'Italian' => 'it_IT',
120 'Japanese' => 'ja_JP',
122 'Norwegian' => 'nb_NO',
124 'PortugueseBrazilian' => 'pt_BR',
125 'Portuguese' => 'pt_PT',
126 'Russian' => 'ru_RU',
127 'SimplifiedChinese' => 'zh_CN',
128 'Spanish' => 'es_ES',
129 'Swedish' => 'sv_SE',
132 return $langmap[$lang] ;
135 function locale_code_to_language_name ($loc) {
138 'bg_BG' => 'Bulgarian',
139 'ca_ES' => 'Catalan',
140 'zh_TW' => 'Chinese',
142 'en_US' => 'English',
148 'id_ID' => 'Indonesian',
149 'it_IT' => 'Italian',
150 'ja_JP' => 'Japanese',
152 'nb_NO' => 'Norwegian',
154 'pt_BR' => 'PortugueseBrazilian',
155 'pt_PT' => 'Portuguese',
156 'ru_RU' => 'Russian',
157 'zh_CN' => 'SimplifiedChinese',
158 'es_ES' => 'Spanish',
159 'sv_SE' => 'Swedish',
162 return $localemap[$loc] ;
165 function lang_id_to_language_name ($lang_id) {
166 $res = db_query_params ('SELECT classname FROM supported_languages WHERE language_id=$1', array ($lang_id));
167 return db_result($res, 0, 'classname');
170 function language_name_to_lang_id ($language) {
171 $res = db_query_params ('SELECT language_id FROM supported_languages WHERE classname=$1', array ($language)) ;
172 return db_result($res, 0, 'language_id');
175 function setup_gettext_from_context() {
176 setup_gettext_from_langname (choose_language_from_context ());
179 function setup_gettext_for_user ($user) {
180 setup_gettext_from_lang_id ($user->getLanguage());
183 function setup_gettext_from_lang_id ($lang_id) {
184 $lang = lang_id_to_language_name ($lang_id) ;
185 setup_gettext_from_langname($lang) ;
188 function setup_gettext_from_langname ($lang) {
189 $locale = language_name_to_locale_code($lang).'.utf8';
190 setup_gettext_from_locale ($locale) ;
193 function setup_gettext_from_sys_lang () {
196 if (forge_get_config('default_language')) {
197 $lang = forge_get_config('default_language') ;
200 $locale = language_name_to_locale_code($lang).'.utf8';
201 setup_gettext_from_locale ($locale) ;
204 function setup_gettext_from_locale ($locale) {
205 setlocale(LC_ALL, $locale);
207 if (isset($GLOBALS['sys_gettext_path'])) {
208 bindtextdomain('gforge', $GLOBALS['sys_gettext_path']);
210 bindtextdomain('gforge', '/usr/share/locale/');
212 textdomain('gforge');
217 // c-file-style: "bsd"