-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacceptFromHttp.php
More file actions
172 lines (146 loc) · 4.95 KB
/
acceptFromHttp.php
File metadata and controls
172 lines (146 loc) · 4.95 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
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
<?php
/*
Reimplementation of PHP's Locale::acceptFromHttp() which uses the HTTP
Accep-Language header, an array of supported locales and a default locale to
determine the most suitable locale; returned as a IETF language tag.
Copyright © 2014, 2015 Aram Nap
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Example usage:
<?php
$supportedLocales = [
0 => 'de-DE',
1 => 'en-US',
2 => 'es-ES',
3 => 'fr-CA',
4 => 'fr-FR',
5 => 'nl_BE',
6 => 'nl-NL',
7 => 'pt-BR',
8 => 'pt-PT',
9 => 'ru-RU'
];
$defaultLocale = $supportedLocales[7];
$locale = acceptFromHttp(
$_SERVER['HTTP_ACCEPT_LANGUAGE'],
$supportedLocales,
$defaultLocale
);
echo $locale;
?>
*/
function acceptFromHttp(
$httpAcceptLanguage,
array $supportedLocales,
$defaultLocale
) {
if (empty($supportedLocales)) {
throw new LogicException(
'The array of supported locales for '
. __FUNCTION__ . ' can\'t be empty.'
);
}
if (empty($defaultLocale)) {
throw new LogicException(
'The default locale for '
. __FUNCTION__ . ' can\'t be empty.'
);
}
if (empty($httpAcceptLanguage)) {
return $defaultLocale;
}
$httpAcceptLanguage = explode(
',',
str_replace(' ', '', $httpAcceptLanguage)
);
$qualityFactors = [];
$locales = [];
for ($i = 0, $max = count($httpAcceptLanguage); $i < $max; ++$i) {
// Check if the locale contains a quality factor.
$separatorPosition = strpos($httpAcceptLanguage[$i], ';');
if (false !== $separatorPosition) {
$qualityFactors[$i] = (float) substr(
$httpAcceptLanguage[$i],
strpos($httpAcceptLanguage[$i], '=') + 1
);
$locale = substr(
$httpAcceptLanguage[$i],
0,
$separatorPosition
);
} else {
$qualityFactors[$i] = 1.0;
$locale = $httpAcceptLanguage[$i];
}
// Transform the language and the country (if available)
// to lower and upper case respectively.
$separatorPosition = strrpos($locale, '-');
if (false !== $separatorPosition) {
$locales[$i] = strtolower(substr($locale, 0, $separatorPosition))
. strtoupper(substr($locale, $separatorPosition));
} else {
$locales[$i] = strtolower($locale);
}
}
$httpAcceptLanguage = array_combine($locales, $qualityFactors);
arsort($httpAcceptLanguage, SORT_NUMERIC);
// Return the most suitable locale.
foreach ($httpAcceptLanguage as $locale => $qualityFactor) {
$separatorPosition = strrpos($locale, '-');
if (
// Check if the locale isn't just a language.
false !== $separatorPosition
&& ctype_upper(substr($locale, $separatorPosition + 1))
) {
for (
$i = 0, $max = count($supportedLocales);
$i < $max;
++$i
) {
if ($locale === $supportedLocales[$i]) {
return $supportedLocales[$i];
}
}
} else {
for (
$i = 0, $max = count($supportedLocales);
$i < $max;
++$i
) {
if (0 === strpos($supportedLocales[$i], $locale)) {
return $supportedLocales[$i];
}
}
}
}
// Return the most suitable locale based on the language of the locale.
foreach ($httpAcceptLanguage as $locale => $qualityFactor) {
$separatorPosition = strrpos($locale, '-');
if (false !== $separatorPosition) {
$language = substr($locale, 0, $separatorPosition);
} else {
$language = $locale;
}
for (
$i = 0, $max = count($supportedLocales);
$i < $max;
++$i
) {
if (0 === strpos($supportedLocales[$i], $language)) {
return $supportedLocales[$i];
}
}
}
// Fallback to the default locale.
return $defaultLocale;
}