forked from OpenStackweb/openstack-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserLanguage.php
More file actions
94 lines (87 loc) · 3.09 KB
/
Copy pathUserLanguage.php
File metadata and controls
94 lines (87 loc) · 3.09 KB
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
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class UserLanguage {
static private function _parse_accept_language_header(){
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
else// ??
$langs = array("en" => 1);
return $langs;
}
static function getCurrentUserLang(){
$accept_languages = self::_parse_accept_language_header();
reset($accept_languages);
$current_lang = key($accept_languages);
$current_lang = substr($current_lang, 0, 2);
$languages = array(
'af'=>'Afrikaans',
'ar'=>'Arabic',
'bg'=>'Bulgarian',
'ca'=>'Catalan',
'cs'=>'Czech',
'da'=>'Danish',
'de'=>'German',
'el'=>'Greek',
'en'=>'English',
'es'=>'Spanish',
'et'=>'Estonian',
'fi'=>'Finnish',
'fr'=>'French',
'gl'=>'Galician',
'he'=>'Hebrew',
'hi'=>'Hindi',
'hr'=>'Croatian',
'hu'=>'Hungarian',
'id'=>'Indonesian',
'it'=>'Italian',
'ja'=>'Japanese',
'ko'=>'Korean',
'ka'=>'Georgian',
'lt'=>'Lithuanian',
'lv'=>'Latvian',
'ms'=>'Malay',
'nl'=>'Dutch',
'no'=>'Norwegian',
'pl'=>'Polish',
'pt'=>'Portuguese',
'ro'=>'Romanian',
'ru'=>'Russian',
'sk'=>'Slovak',
'sl'=>'Slovenian',
'sq'=>'Albanian',
'sr'=>'Serbian',
'sv'=>'Swedis',
'th'=>'Thai',
'tr'=>'Turkish',
'uk'=>'Ukrainian',
'zh' =>'Chinese',
);
if(is_string($current_lang) && array_key_exists($current_lang,$languages))
return $languages[$current_lang];
return 'English';
}
}