File: gettext.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (232 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
 * FusionForge localisation
 *
 * Copyright 1999-2001, VA Linux Systems, Inc.
 * Copyright 2003-2004, Guillaume Smet
 * Copyright 2007-2009, Roland Mas
 * Copyright 2012, Franck Villaume - TrivialDev
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * choose_language_from_context - find the most appropriate language
 *
 * @return string the language class name.
 */
function choose_language_from_context () {
	/*
		Determine which language to use

		It depends on whether the user has set a cookie or not using
		the account page or the left-hand nav or how their browser is
		set or whether they are logged in or not

		if logged in, use language from users table
		else check for cookie and use that value if valid
		if no cookie check browser preference and use that language if valid
		else just use default language as configured for the installation
	*/

	if (!isset($_SERVER['SERVER_SOFTWARE'])) {
		// In command-line scripts
		if (forge_get_config('default_language')) {
			return forge_get_config('default_language') ;
		}
		return "English";
	}

	// Logged in -> use preferences
	if (session_loggedin()) {
		$user = session_get_user () ;
		return lang_id_to_language_name ($user->getLanguage()) ;
	}

	// Cookie present -> use that
	$cookie_language_id = getIntFromCookie ('cookie_language_id') ;
	if ($cookie_language_id) {
		return lang_id_to_language_name ($cookie_language_id) ;
	}

	// Try with the browser's preferred language
	$ranges = explode (',', getStringFromServer ('HTTP_ACCEPT_LANGUAGE')) ;
	$languages = array() ; $lcount = count ($ranges) ;
	if ($lcount > 0) {
		$delta = 0.009/$lcount ;
		$i = 0 ;

		foreach ($ranges as $p) {
			if (preg_match ('/(.*);q=(.*)/', $p, $matches)) {
				$l = $matches[1] ;
				$w = $matches[2] ;
				$languages[$l] = $w + $delta * ($lcount - $i) ;
			} else {
				$languages[$p] = 1 + $delta * ($lcount - $i) ;
			}
			$i++ ;
		}
		arsort($languages, SORT_NUMERIC);
		$languages = array_keys($languages);

		for( $i=0, $max = sizeof($languages); $i < $max; $i++){
			$languageCode = $languages[$i];
			$res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
			if (db_numrows($res) > 0) {
				return db_result($res,0,'classname');
			}
			// If that didn't work, check if we have sublanguage specifier
			// If so, try to strip it and look for for main language only
			if (strstr($languageCode, '-')) {
				$languageCode = substr($languageCode, 0, 2);
				$res = db_query_params ('select classname from supported_languages where language_code=$1', array ($languageCode)) ;
				if (db_numrows($res) > 0) {
					return db_result($res,0,'classname');
				}
			}
		}
	}

	// Okay, let's use the site-wide default language
	if (forge_get_config('default_language')) {
		return forge_get_config('default_language') ;
	}

	// Still no match?  Really?
	return "English";
}

function language_name_to_locale_code ($lang) {
	$langmap = array (
		'Basque'              => 'eu_ES',
		'Bulgarian'           => 'bg_BG',
		'Catalan'             => 'ca_ES',
		'Chinese'             => 'zh_TW',
		'Dutch'               => 'nl_NL',
		'English'             => 'en_US',
		'Esperanto'           => 'eo',
		'French'              => 'fr_FR',
		'German'              => 'de_DE',
		'Greek'               => 'el_GR',
		'Hebrew'              => 'he_IL',
		'Indonesian'          => 'id_ID',
		'Italian'             => 'it_IT',
		'Japanese'            => 'ja_JP',
		'Korean'              => 'ko_KR',
		'Norwegian'           => 'nb_NO',
		'Polish'              => 'pl_PL',
		'PortugueseBrazilian' => 'pt_BR',
		'Portuguese'          => 'pt_PT',
		'Russian'             => 'ru_RU',
		'SimplifiedChinese'   => 'zh_CN',
		'Spanish'             => 'es_ES',
		'Swedish'             => 'sv_SE',
		'Thai'                => 'th_TH',
		) ;
	return $langmap[$lang] ;
}

function locale_code_to_language_name ($loc) {
	$localemap = array (
		'eu_ES' => 'Basque',
		'bg_BG' => 'Bulgarian',
		'ca_ES' => 'Catalan',
		'zh_TW' => 'Chinese',
		'nl_NL' => 'Dutch',
		'en_US' => 'English',
		'eo' 	=> 'Esperanto',
		'fr_FR' => 'French',
		'de_DE' => 'German',
		'el_GR' => 'Greek',
		'he_IL' => 'Hebrew',
		'id_ID' => 'Indonesian',
		'it_IT' => 'Italian',
		'ja_JP' => 'Japanese',
		'ko_KR' => 'Korean',
		'nb_NO' => 'Norwegian',
		'pl_PL' => 'Polish',
		'pt_BR' => 'PortugueseBrazilian',
		'pt_PT' => 'Portuguese',
		'ru_RU' => 'Russian',
		'zh_CN' => 'SimplifiedChinese',
		'es_ES' => 'Spanish',
		'sv_SE' => 'Swedish',
		'th_TH' => 'Thai',
		) ;
	return $localemap[$loc] ;
}

function lang_id_to_language_name ($lang_id) {
	$res = db_query_params ('SELECT classname FROM supported_languages WHERE language_id=$1', array ($lang_id));
	return db_result($res, 0, 'classname');
}

function language_name_to_lang_id ($language) {
	$res = db_query_params ('SELECT language_id FROM supported_languages WHERE classname=$1', array ($language)) ;
	return db_result($res, 0, 'language_id');
}

function setup_gettext_from_context() {
	setup_gettext_from_langname (choose_language_from_context ());
}

function setup_gettext_for_user ($user) {
	setup_gettext_from_lang_id ($user->getLanguage());
}

function setup_gettext_from_lang_id ($lang_id) {
	$lang = lang_id_to_language_name ($lang_id) ;
	setup_gettext_from_langname($lang) ;
}

function setup_gettext_from_langname ($lang) {
	$locale[] = language_name_to_locale_code($lang).'.utf8';
	$locale[] = language_name_to_locale_code($lang).'.UTF-8';
	setup_gettext_from_locale ($locale) ;
}

function setup_gettext_from_sys_lang () {

	$lang = "English";
	if (forge_get_config('default_language')) {
		$lang = forge_get_config('default_language') ;
	}

	$locale[] = language_name_to_locale_code($lang).'.utf8';
	$locale[] = language_name_to_locale_code($lang).'.UTF-8';
	setup_gettext_from_locale ($locale) ;
}

/*
 * setup_gettext_from_locale() - call setlocales to set up language used by gettext
 *
 * @param	array	locales (.utf8 + .UTF-8)
 */
function setup_gettext_from_locale ($locale) {
	setlocale(LC_ALL, $locale);

	if (forge_get_config('gettext_path')) {
		bindtextdomain('fusionforge', forge_get_config('gettext_path'));
	} else {
		bindtextdomain('fusionforge', '/usr/share/locale/');
	}
	textdomain('fusionforge');
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: