forked from Gnucash/gnucash-htdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.php
111 lines (98 loc) · 3.56 KB
/
lang.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
<?php
$top_dir = ".";
$locale_dir = "locale";
# key: locale, value: lang_dir
$supported_languages = array(
'ca_ES' => 'ca',
'de_DE' => 'de',
'en_US' => 'en',
'es_ES' => 'es',
'fr_FR' => 'fr',
'he_IL' => 'he',
'hr_HR' => 'hr',
'hu_HU' => 'hu',
'id_ID' => 'id',
'it_IT' => 'it',
'ja_JP' => 'ja',
'nb_NO' => 'nb',
'nl_NL' => 'nl',
'pl_PL' => 'pl',
'pt_PT' => 'pt',
'zh_CN' => 'zh_CN',
'zh_TW' => 'zh_TW'
);
if (!array_key_exists('HTTP_HOST', $_SERVER) || ($_SERVER["HTTP_HOST"] == "lists.gnucash.org"))
{
$home = "https://www.gnucash.org";
}else{
$home = $top_dir;
}
if ( !isset($locale) ) { $locale = ""; }
if ( !isset($lang_dir) ) { $lang_dir = $locale; }
# get the cookie setting
if (array_key_exists('lang_cookie', $_COOKIE)) {
$locale = filter_input(INPUT_COOKIE, 'lang_cookie', FILTER_SANITIZE_STRING);
$lang_cookie = $locale;
}else{
$lang_cookie = "";
}
# allow user override.
$get_lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);
if ($get_lang) { $locale = $get_lang; }
# Find the full locale name for short language name.
if (strlen($locale) == 2) {
foreach($supported_languages as $loc_lang => $loc_dir)
{
if ( (strtolower($locale) == strtolower($loc_dir))
|| (strtolower($locale) == substr($loc_lang, 0, 2 )) )
{
$locale = $loc_lang;
break;
}
}
}
# Find the locale from Client Accept language
if ($locale == "") {
# Get user preferred languages, and match against supported language
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
{
# tolower() => remove space => '-' -> '_'
# "fr-ch;q=0.3, en, zh-cn;q=0.7" => "fr_ch;q=0.3,en,zh_cn;q=0.7"
$accept_language = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING);
$languages = str_replace('-','_', str_replace(' ', '', strtolower($accept_language)));
$languages = explode(",", $languages);
foreach ($languages as $item)
{
# "zh_cn;q=0.7" => "zh_cn"
$lang = substr($item, 0, strcspn($item, ';'));
$lang_short = substr($lang, 0, 2);
# full match is prefer, but short match is acceptable.
foreach ($supported_languages as $loc_lang => $loc_dir)
{
if ($lang == strtolower($loc_lang)) { $locale = $loc_lang; break; }
if ($lang_short == substr($loc_lang, 0, 2 )) { $locale = $loc_lang; }
}
if ($locale != "") { break; }
}
}
# nothing matched, use default language
if ($locale == "") { $locale = "en_US"; }
}
$lang_dir = array_key_exists($locale, $supported_languages) ?
$supported_languages[$locale] : "en";
setcookie("lang_cookie", $locale);
# We should have a locale now, let's set up the required bits and pieces to show
# the website in that language.
require_once("externals/gettext.inc");
//putenv("LANG=$locale");
//putenv("LANGUAGE=$locale");
$locale_res = T_setlocale(LC_ALL, $locale);
$domain = "gnucash-htdocs";
if (! isset($text_dir)) { $text_dir = $locale_dir; }
$dir_res = T_bindtextdomain($domain, $text_dir);
# Also specify that we want to receive the translated strings as UTF-8
T_bind_textdomain_codeset($domain, 'UTF-8');
T_textdomain($domain);
# Next line was disabled in order to have the definition of doctype on the first line of the html resulting page. In this way the fixed attribute of the branding GnuCash sign works in ie8
/*echo ("<!-- $locale , locale_res [$locale_res] , dir_res $dir_res, lang_cookie [$lang_cookie] -->\n");*/
?>