This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
phpLang.inc.php
294 lines (244 loc) · 11.2 KB
/
phpLang.inc.php
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
// +--------------------------------------------------------------------------+
// | phpLang modified for Fink website (fink.sourceforge.net) |
// | based on phpLang version 0.5.0 - 2002/02/23 |
// +--------------------------------------------------------------------------+
// | Copyright (c) 2000-2001 The phpHeaven-team |
// +--------------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html |
// +--------------------------------------------------------------------------+
// | It is a simple script that enables you to simply have different |
// | languages on you web site. |
// | |
// | usage: read attached documentation |
// +--------------------------------------------------------------------------+
// | Last release available on phpHeaven: |
// | http://www.phpheaven.net/projects/phpLang/ |
// | |
// | Authors: Nicolas Hoizey <nhoizey@phpheaven.net> |
// | Loïã Chapeaux <lolo@phpheaven.net> |
// +--------------------------------------------------------------------------+
// path to image files
if(!defined('phpLang_images'))
define('phpLang_images', 'flags/');
// path to translated files
if(!defined('phpLang_localDir'))
define('phpLang_localDir', './');
// name of a translation file that will always be included if it exists
if(!defined('phpLang_globalFile'))
define('phpLang_globalFile', getenv('REDIRECT_HOMEDIR').'localization/global.php');
// parameter to add in url
if(!defined('phpLang_urlParam'))
define('phpLang_urlParam', 'phpLang');
// indicates if it should put a cookie
if(!defined('phpLang_useCookie'))
define('phpLang_useCookie', true);
// how are named the xx-localized version of file 'toto.php' ?
// 1 -> current_dir/toto.php.xx
// 2 -> current_dir/toto.xx.php
// 3 -> current_dir/xx.toto.php
// 4 -> current_dir/xx/toto.php (in a directory for each language)
if(!defined('phpLang_fileNameType'))
define('phpLang_fileNameType', 2);
// list of available languages, order it as you need
$phpLang_languages = array(
"en([-_][[:alpha:]]{2})?|english" => array('en', 'english', 'English'),
"fr([-_][[:alpha:]]{2})?|french" => array('fr', 'french', 'Français'),
"cs|czech" => array('cs', 'czech', ''),
"da|danish" => array('da', 'danish', 'Dansk'),
"nl([-_][[:alpha:]]{2})?|dutch" => array('nl', 'dutch', 'Nederlands'),
"de([-_][[:alpha:]]{2})?|german" => array('de', 'german', 'Deutsch'),
"fi|finnish" => array('fi', 'finnish', ''),
"is|icelandic" => array('is', 'icelandic', ''),
"it|italian" => array('it', 'italian', 'Italiano'),
"ja|japanese" => array('ja', 'japanese', '日本語 (Nihongo)'),
"no|norwegian" => array('no', 'norwegian', ''),
"sv([-_][[:alpha:]]{2})?|swedish" => array('sv', 'swedish', 'svensk'),
"pl|polish" => array('pl', 'polish', 'Polski'),
"ru|russian" => array('ru', 'russian', 'Русский (Russkiy)'),
"sk|slovak" => array('sk', 'slovak', ''),
"es([-_][[:alpha:]]{2})?|spanish" => array('es', 'spanish', 'Español'),
"th|thai" => array('th', 'thai', ''),
"pt[-_]br" => array('pt-br', 'brazilian portuguese'),
"pt([-_][[:alpha:]]{2})?|portuguese" => array('pt', 'portuguese', 'Português'),
"uk([-_][[:alpha:]]{2})?|ukrainian" => array('ua', 'ukrainian', ''),
"ko([-_][[:alpha:]]{2})?|korean" => array('ko', 'korean', ''),
"he|hebrew" => array('he', 'hebrew', ''),
"ar([-_][[:alpha:]]{2})?|arabic" => array('ar', 'arabic', ''),
"zh[-_]tw" => array('zh-tw', 'chinese_traditional', ''),
"zh([-_][[:alpha:]]{2})?|chinese" => array('zh', 'chinese_simplified', '中文 (简) (Simplified Chinese)')
);
// Disable languages. e.g. $GLOBALS['phpLang_disabledLanguages'] = 'de ja' before calling phpLang.
if (isset($GLOBALS['phpLang_disabledLanguages'])) {
$disLangs = explode(' ', $GLOBALS['phpLang_disabledLanguages']);
foreach ($GLOBALS['phpLang_languages'] as $langKey => $langArray) {
foreach ($disLangs as $disLang) {
if ($langArray[0] == $disLang) unset ($phpLang_languages[$langKey]);
}
}
}
// finds current file name, extension and uri
if (!isset($SCRIPT_NAME)) $SCRIPT_NAME = getenv('SCRIPT_NAME');
if(preg_match("|([^/?]+)(\?.*)?$|", $SCRIPT_NAME, $regs)) {
define('phpLang_currentFile', $regs[1]);
if(preg_match("|(.*)(\.[^.]+)$|", phpLang_currentFile, $regs2)) {
define('phpLang_currentFileName', $regs2[1]);
define('phpLang_currentFileExtension', $regs2[2]);
} else {
define('phpLang_currentFileName', phpLang_currentFile);
}
$uri = preg_replace("|[?&]".phpLang_urlParam."=[^&]*|", "", $regs[0]);
$uri .= preg_match("|\?|", $uri) ? '&' : '?';
define('phpLang_currentURI', $uri);
} else {
// it should not be possible
define('phpLang_currentFile', '');
define('phpLang_currentURI', '');
define('phpLang_currentFileName', '');
define('phpLang_currentFileExtension', '');
}
$HTTP_ACCEPT_LANGUAGE = getenv('HTTP_ACCEPT_LANGUAGE');
$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT');
// checks if the language is disabled
function isDisabled($lang)
{
if (isset($GLOBALS['phpLang_disabledLanguages'])) {
if (' '.strpos($GLOBALS['phpLang_disabledLanguages'],$lang)>0) return TRUE;
}
return FALSE;
}
// function that gives the localized file name
function phpLang_localizedFileName($lang)
{
switch(phpLang_fileNameType) {
case 1 : $ret = phpLang_localDir.phpLang_currentFileName.phpLang_currentFileExtension.'.'.$lang;
break;
case 2 : $ret = phpLang_localDir.phpLang_currentFileName.'.'.$lang.phpLang_currentFileExtension;
break;
case 3 : $ret = phpLang_localDir.$lang.'.'.phpLang_currentFileName.phpLang_currentFileExtension;
break;
case 4 : $ret = phpLang_localDir.$lang.'/'.phpLang_currentFileName.phpLang_currentFileExtension;
break;
}
return $ret;
}
// language code detection
function phpLang_detectLanguage($str, $from)
{
$ext = '';
reset($GLOBALS['phpLang_languages']);
while($ext == '' && list($key, $name) = each($GLOBALS['phpLang_languages'])) {
if (($from == 1 && preg_match("!^".$key."$!i", $str)) || ($from == 2 && preg_match("!(\(|\[|;[[:space:]])".$key."(;|\]|\))!i", $str))) {
$ext = $name[0];
}
}
return $ext;
}
// finds the appropriate language file
if (isset($_GET[phpLang_urlParam]) && file_exists(phpLang_localizedFileName($_GET[phpLang_urlParam])) && !isDisabled($_GET[phpLang_urlParam])) {
// a language as been chosen by the user
define('phpLang_current', $_GET[phpLang_urlParam]);
}
if (!defined('phpLang_current') && phpLang_useCookie && isset($_COOKIE['phpLangCookie']) && file_exists(phpLang_localizedFileName($_COOKIE['phpLangCookie'])) && !isDisabled($_COOKIE['phpLangCookie'])) {
// a language as been found in a cookie previously set
define('phpLang_current', $_COOKIE['phpLangCookie']);
}
if (!defined('phpLang_current') && isset($HTTP_ACCEPT_LANGUAGE) && trim($HTTP_ACCEPT_LANGUAGE) != '') {
// looks at the languages accepted by the browser
$accepted = explode(',', $HTTP_ACCEPT_LANGUAGE);
while(!defined('phpLang_current') && list($key, $name) = each($accepted)) {
$code = explode(';', $name);
$ext = phpLang_detectLanguage($code[0], 1);
if(file_exists(phpLang_localizedFileName($ext)) && !isDisabled($ext)) {
define('phpLang_current', $ext);
}
}
}
if (!defined('phpLang_current') && isset($HTTP_USER_AGENT) && trim($HTTP_USER_AGENT) != '') {
// looks at the browser's identification
$ext = phpLang_detectLanguage($HTTP_USER_AGENT, 2);
if(file_exists(phpLang_localizedFileName($ext))) {
define('phpLang_current', $ext);
}
}
if(!defined('phpLang_current')) {
// if no language yet found, chose the first existing in site's list
reset($phpLang_languages);
while(!defined('phpLang_current') && list($key, $name) = each($phpLang_languages)) {
if(file_exists(phpLang_localizedFileName($name[0]))) {
define('phpLang_current', $name[0]);
}
}
}
// detection done, cookie update and inclusion of localized files if any available
if(defined('phpLang_current')) {
if(phpLang_useCookie) {
// set a cookie expiring in one year for current language
setcookie('phpLangCookie', phpLang_current, time() + 60*60*24*365, "/");
}
// defines a string to add at the end of each link
define('phpLang_link', phpLang_urlParam.'='.phpLang_current);
} else {
// no language found
define('phpLang_current', '');
define('phpLang_link', phpLang_urlParam.'=');
}
// ###################################################################
// only for backward compatibility with versions previous to 0.3.0 !!!
define('CUR_LANG', phpLang_current);
define('LANG_LINK', phpLang_link);
// ###################################################################
// function that adds the flags with links for existing files
// give as first parameter the HTML string to put between each flag
// function that adds the flags with links for existing files
// give as first parameter the HTML string to put between each flag
function AddFlags($between = "", $showCurrent = false, $root = '')
{
reset($GLOBALS["phpLang_languages"]);
$temp = "";
while(list($key, $name) = each($GLOBALS["phpLang_languages"])) {
if(file_exists(phpLang_localizedFileName($name[0])) && ($showCurrent || $name[0] != phpLang_current)) {
echo("\t" . $temp . '<a href="' . phpLang_currentURI . phpLang_urlParam . '=' . $name[0] . "\">\n");
echo("\t\t<img src=\"" . $root . phpLang_images . $name[0] . '.png" border="0" align="middle" width="24" height="16" alt="' . $name[1] . "\">\n");
echo("\t</a>\n");
$temp = $between;
} elseif (file_exists(phpLang_localizedFileName($name[0]))) {
// Shows selected language
echo("\t\t<img src=\"" . $root . phpLang_images . $name[0] . '.png" border="0" align="middle" width="24" height="16" alt="' . $name[1] . "\" \n");
echo("\t\t\tstyle=\"padding: 0px 0px 16px 0px;\">\n");
$temp = $between;
}
}
}
function AddLanguageNames($between = " | ", $showCurrent = false)
{
$count = 0;
reset($GLOBALS["phpLang_languages"]);
echo ($between);
while(list($key, $name) = each($GLOBALS["phpLang_languages"])) {
if(file_exists(phpLang_localizedFileName($name[0])) && ($showCurrent || $name[0] != phpLang_current)) {
echo('<a href="'.phpLang_currentURI.phpLang_urlParam.'='.$name[0].'">');
echo($name[2]);
echo('</a>' . $between);
$count ++;
} elseif ($name[0] == phpLang_current) {
echo($name[2] . $between);
$count ++;
}
}
if (0 == $count) {
echo ('English only' . $between);
}
}
if(phpLang_current != '') {
// includes global language file
if(file_exists(phpLang_localDir.phpLang_globalFile.'.'.phpLang_current)) {
include(phpLang_localDir.phpLang_globalFile.'.'.phpLang_current);
}
// includes current language file
include(phpLang_localizedFileName(phpLang_current));
}
// then, you can use two constants in your scripts :
// phpLang_current : current language code
// phpLang_link : add this in the links after a '?' or a '&'
?>