-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageHandler.php
More file actions
73 lines (59 loc) · 2.24 KB
/
LanguageHandler.php
File metadata and controls
73 lines (59 loc) · 2.24 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
<?php
namespace App\Modules\Locale;
use Illuminate\Http\Request;
use Locale;
class LanguageHandler
{
protected $config;
protected $app;
private $defaultLocale;
private $supportedLanguages;
private $request;
private $use_intl = false;
public function __construct($defaultLocale, $supportedLanguages, Request $request)
{
$this->app = app();
$this->config = $this->app['config'];
$this->defaultLocale = $defaultLocale;
$this->request = $request;
if (class_exists('Locale')) {
$this->use_intl = true;
foreach ($supportedLanguages as $key => $supportedLanguage) {
if (!isset($supportedLanguage['lang'])) {
$supportedLanguage['lang'] = Locale::canonicalize($key);
} else {
$supportedLanguage['lang'] = Locale::canonicalize($supportedLanguage['lang']);
}
if (isset($supportedLanguage['regional'])) {
$supportedLanguage['regional'] = Locale::canonicalize($supportedLanguage['regional']);
}
$this->supportedLanguages[$key] = $supportedLanguage;
}
} else {
$this->supportedLanguages = $supportedLanguages;
}
}
public function handle()
{
if ($this->request->user() !== null) {
$userLocale = $this->request->user()->language->locale;
if (!empty($this->supportedLanguages[$userLocale])) {
return $userLocale;
}
}
if ($this->use_intl && !empty($this->request->server('HTTP_ACCEPT_LANGUAGE'))) {
$http_accept_language = $this->request->getPreferredLanguage(array_keys($this->config->get('locale.supported_locales')));
if (!empty($this->supportedLanguages[$http_accept_language])) {
return $http_accept_language;
}
}
if ($this->request->server('REMOTE_HOST')) {
$remote_host = explode('.', $this->request->server('REMOTE_HOST'));
$lang = strtolower(end($remote_host));
if (!empty($this->supportedLanguages[$lang])) {
return $lang;
}
}
return $this->defaultLocale;
}
}