-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhooks.php
44 lines (36 loc) · 1.53 KB
/
hooks.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
<?
$app->hook('slim.before', function () use ($app, $availableLangs) {
$env = $app->environment();
// setup default lang based on first in the list
$lang = $availableLangs[0];
// if they are accessing the root, you could try and direct them to the correct language
if ($env['PATH_INFO'] == '/') {
if (isset($env['ACCEPT_LANGUAGE'])) {
// try and auto-detect, find the language with the lowest offset as they are in order of priority
$priority_offset = strlen($env['ACCEPT_LANGUAGE']);
foreach($availableLangs as $availableLang) {
$i = strpos($env['ACCEPT_LANGUAGE'], $availableLang);
if ($i !== false && $i < $priority_offset) {
$priority_offset = $i;
$lang = $availableLang;
}
}
}
} else {
$pathInfo = $env['PATH_INFO'] . (substr($env['PATH_INFO'], -1) !== '/' ? '/' : '');
// extract lang from PATH_INFO
foreach($availableLangs as $availableLang) {
$match = '/'.$availableLang;
if (strpos($pathInfo, $match.'/') === 0) {
$lang = $availableLang;
$env['PATH_INFO'] = substr($env['PATH_INFO'], strlen($match));
if (strlen($env['PATH_INFO']) == 0) {
$env['PATH_INFO'] = '/';
}
}
}
}
$app->view()->setLang($lang);
$app->view()->setAvailableLangs($availableLangs);
$app->view()->setPathInfo($env['PATH_INFO']);
});