|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vluzrmos\LanguageDetector\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\ServiceProvider; |
|
8
|
|
|
use Symfony\Component\Translation\TranslatorInterface as Translator; |
|
9
|
|
|
use Vluzrmos\LanguageDetector\Drivers\AbstractDetector; |
|
10
|
|
|
use Vluzrmos\LanguageDetector\LanguageDetector; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ServiceProvider. |
|
14
|
|
|
*/ |
|
15
|
|
|
class LanguageDetectorServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Symfony translator. |
|
19
|
|
|
* @var Translator |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $translator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Illuminate Request. |
|
25
|
|
|
* @var Request |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $request; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Configurations repository. |
|
31
|
|
|
* @var Config |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Detector Drivers available and its shortcuts. |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $drivers = [ |
|
40
|
|
|
'browser' => 'Vluzrmos\LanguageDetector\Drivers\BrowserDetectorDriver', |
|
41
|
|
|
'subdomain' => 'Vluzrmos\LanguageDetector\Drivers\SubdomainDetectorDriver', |
|
42
|
|
|
'uri' => 'Vluzrmos\LanguageDetector\Drivers\UriDetectorDriver', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Bootstrap the application. |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function boot() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->translator = $this->app['translator']; |
|
53
|
|
|
$this->config = $this->app['config']; |
|
54
|
|
|
$this->request = $this->app['request']; |
|
55
|
|
|
|
|
56
|
|
|
$this->registerAndPublishConfigurations(); |
|
57
|
|
|
$this->registerAllDrivers(); |
|
58
|
|
|
$this->registerLanguageDetector(); |
|
59
|
|
|
$this->detectAndApplyLanguage(); |
|
60
|
|
|
$this->registerRoutePrefix(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Register the package. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function register() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->registerEncryptCookies(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The services that package provides. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public function provides() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
'language.detector', |
|
80
|
|
|
'language.driver.browser', |
|
81
|
|
|
'language.driver.subdomain', |
|
82
|
|
|
'language.driver.uri', |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Disable cookie encryption for language cookie name. |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function registerEncryptCookies() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->app->resolving('Illuminate\Cookie\Middleware\EncryptCookies', function ($middleware) { |
|
92
|
|
|
if ($this->config('cookie', true) && ! $this->config('cookie_encrypt', false)) { |
|
93
|
|
|
$middleware->disableFor($this->config('cookie_name', 'locale')); |
|
94
|
|
|
} |
|
95
|
|
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Register and publish configuration files. |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function registerAndPublishConfigurations() |
|
104
|
|
|
{ |
|
105
|
|
|
$configFile = __DIR__.'/../../config/lang-detector.php'; |
|
106
|
|
|
|
|
107
|
|
|
$this->publishes([$configFile => base_path('config/lang-detector.php')]); |
|
108
|
|
|
|
|
109
|
|
|
$this->mergeConfigFrom($configFile, 'lang-detector'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Register All drivers available. |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function registerAllDrivers() |
|
118
|
|
|
{ |
|
119
|
|
|
$languages = $this->config('languages', []); |
|
120
|
|
|
|
|
121
|
|
|
if (empty($languages) || in_array('auto', $languages)) { |
|
122
|
|
|
$languages = $this->getSupportedLanguages(); |
|
123
|
|
|
|
|
124
|
|
|
$this->app['config']->set('lang-detector.languages', $languages); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$segment = $this->config('segment', 0); |
|
128
|
|
|
|
|
129
|
|
|
foreach ($this->drivers as $short => $driver) { |
|
130
|
|
|
$this->app->singleton( |
|
131
|
|
|
'language.driver.'.$short, |
|
132
|
|
|
function () use ($driver, $languages, $segment) { |
|
133
|
|
|
/** @var AbstractDetector $instance */ |
|
134
|
|
|
$instance = new $driver($this->request, $languages); |
|
135
|
|
|
$instance->setDefaultSegment($segment); |
|
136
|
|
|
|
|
137
|
|
|
return $instance; |
|
138
|
|
|
} |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get a config value. |
|
145
|
|
|
* @param string $key |
|
146
|
|
|
* @param mixed $default |
|
147
|
|
|
* @return mixed |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function config($key, $default = null) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->config->get('lang-detector.'.$key, $default); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Register the detector instance. |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function registerLanguageDetector() |
|
160
|
|
|
{ |
|
161
|
|
|
$contract = 'Vluzrmos\LanguageDetector\Contracts\LanguageDetectorInterface'; |
|
162
|
|
|
|
|
163
|
|
|
$cookie = $this->config('cookie', true) ? $this->config('cookie_name', 'locale') : null; |
|
164
|
|
|
|
|
165
|
|
|
$driver = $this->config('driver', 'browser'); |
|
166
|
|
|
|
|
167
|
|
|
$this->app->singleton( |
|
168
|
|
|
$contract, |
|
169
|
|
|
function () use ($driver, $cookie) { |
|
170
|
|
|
$detector = new LanguageDetector( |
|
171
|
|
|
$this->translator, |
|
172
|
|
|
$this->app['language.driver.'.$driver] |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
if ($cookie) { |
|
176
|
|
|
$detector->useCookies($cookie); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (method_exists($this->app, 'setLocale')) { |
|
180
|
|
|
$detector->addCallback(function ($locale) { |
|
181
|
|
|
$this->app->setLocale($locale); |
|
182
|
|
|
}); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $detector; |
|
186
|
|
|
} |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
$this->app->alias($contract, 'language.detector'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Detect and apply language for the application. |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function detectAndApplyLanguage() |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->config('autodetect', true)) { |
|
198
|
|
|
$this->getLanguageDetector()->detectAndApply(); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get language.detector from container. |
|
204
|
|
|
* |
|
205
|
|
|
* @return LanguageDetector |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function getLanguageDetector() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->app['language.detector']; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Regiter in container the routePrefix. |
|
214
|
|
|
* |
|
215
|
|
|
* @return void |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function registerRoutePrefix() |
|
218
|
|
|
{ |
|
219
|
|
|
$this->app->bind( |
|
220
|
|
|
'language.routePrefix', |
|
221
|
|
|
function () { |
|
222
|
|
|
return $this->getLanguageDetector()->routePrefix(); |
|
223
|
|
|
} |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get a list of supported locales. |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function getSupportedLanguages() |
|
231
|
|
|
{ |
|
232
|
|
|
/** @var \Illuminate\Cache\Repository $cache */ |
|
233
|
|
|
$cache = $this->app['cache']; |
|
234
|
|
|
|
|
235
|
|
|
return $cache->rememberForever('lang-detector.supported-languages', function () { |
|
236
|
|
|
$iterator = \Symfony\Component\Finder\Finder::create() |
|
237
|
|
|
->directories() |
|
238
|
|
|
->in($this->app->langPath()) |
|
239
|
|
|
->depth(0); |
|
240
|
|
|
|
|
241
|
|
|
$langs = []; |
|
242
|
|
|
|
|
243
|
|
|
foreach ($iterator as $dir) { |
|
244
|
|
|
$langs[] = $dir->getBasename(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return parse_langs_to_array($langs); |
|
248
|
|
|
}); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|