-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathroutes.php
168 lines (148 loc) · 5.68 KB
/
routes.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
<?php
/**
* Created by PhpStorm.
* User: r
* Date: 13.04.16
* Time: 16:12
*/
use Excodus\TranslateExtended\Models\Settings;
use RainLab\Translate\Classes\Translator;
use RainLab\Translate\Models\Locale;
App::before(function($request) {
if (App::runningInBackend()) {
return;
}
$translator = Translator::instance();
if (!$translator->isConfigured())
return;
$locale = Request::segment(1);
$localeSession = Session::get($translator::SESSION_LOCALE);
/*
* Behavior when changing locale from the locale picker; post('locale') has priority over $locale,
* because Request still have old locale in the URL, hence $locale is outdated and User sends new locale in the POST
* TODO: hook the translate plugin's onSwitchLocale ajax handler instead of checking on post
*/
if (post('locale') && $locale != post('locale')) {
$translator->setLocale(post('locale'));
}
/*
* Behavior when there is no locale in the Request URL, first check in session and then try to match with default browser language
*/
if (!$locale || !Locale::isValid($locale)) {
if (Settings::get('prefer_user_session',true) && $localeSession) {
$translator->setLocale($localeSession);
} else {
if(Settings::get('browser_language_detection',true) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// get the list of browser languages
$accepted = parseLanguageList($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$available = Locale::listEnabled();
// match against languages enabled in Translate plugin
// TODO: allow october backend users to create their own mappings to the locale short codes
$matches = findMatches($accepted, $available);
// get the first match and save if not empty
if (!empty($matches)) {
$match = array_keys($matches)[0];
$translator->setLocale($match);
}
}
}
}
/*
* If it was unable to retrieve locale from session, route url or browser matching, just roll back to default locale
*/
$locale = $translator->getLocale();
if (!Locale::isValid($locale)) {
$translator->setLocale($translator->getDefaultLocale());
}
if(Settings::get('route_prefixing', true)) {
Route::group(['prefix' => $locale], function() {
Route::any('{slug}', 'Cms\Classes\CmsController@run')->where('slug', '(.*)?');
});
Route::any($locale, 'Cms\Classes\CmsController@run');
Event::listen('cms.route', function() use ($locale) {
Route::group(['prefix' => $locale], function() {
Route::any('{slug}', 'Cms\Classes\CmsController@run')->where('slug', '(.*)?');
});
});
if(Settings::get('homepage_redirect', true)) {
Route::get('/', function() use ($locale) {
return redirect($locale);
});
}
}
});
// browser language parser based on Gumbo's answer
// http://stackoverflow.com/a/3771447/3704886
// parse list of comma separated language tags and sort it by the quality value
function parseLanguageList($languageList) {
if (is_null($languageList)) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return array();
}
$languageList = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
$languages = array();
$languageRanges = explode(',', trim($languageList));
foreach ($languageRanges as $languageRange) {
if (preg_match('/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/', trim($languageRange), $match)) {
if (!isset($match[2])) {
$match[2] = '1.0';
} else {
$match[2] = (string) floatval($match[2]);
}
if (!isset($languages[$match[2]])) {
$languages[$match[2]] = strtolower($match[1]);
}
}
}
krsort($languages);
return $languages;
}
// compare two parsed arrays of language tags and find the matches
function findMatches($accepted, $available) {
$matches = array();
$any = false;
foreach ($available as $availableLocale => $availableName) {
foreach ($accepted as $acceptedQuality => $acceptedLocale) {
$acceptedQuality = floatval($acceptedQuality);
if ($acceptedQuality === 0.0) continue;
if ($acceptedLocale === '*') {
$any = true;
}
$matchingGrade = matchLanguage($acceptedLocale, $availableLocale);
if ($matchingGrade > 0) {
$q = ($acceptedQuality * $matchingGrade);
if (!array_key_exists($availableLocale, $matches) || $matches[$availableLocale] < $q) {
$matches[$availableLocale] = $q;
}
}
}
}
if (count($matches) === 0 && $any) {
$matches = $available;
}
arsort($matches);
return $matches;
}
/**
* compare two language tags and distinguish the degree of matching
* edit: actually matching "en-us" with "en" will always return "1"
* @param $a [] user-accepted
* @param $b [] backend-available
* @return float|int
*/
function matchLanguage($a, $b) {
// convert 'en-US' to 'en-us'
$b = strtolower($b);
$a = explode('-', $a);
$b = explode('-', $b);
$perfect_match = false;
for ($i=0, $n=min(count($a), count($b)); $i<$n; $i++) {
if ($a[$i] !== $b[$i]) break;
if (count($a) == count($b) && $i == $n-1) {
$perfect_match = true;
}
}
$val = $i === 0 ? 0 : (float) $i / count($a);
return $perfect_match ? 2 : $val;
}