-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMultiLanguageServiceProvider.php
83 lines (71 loc) · 2.12 KB
/
MultiLanguageServiceProvider.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
<?php
/**
* @author Rytis Grincevicius <[email protected]>
* @link http://www.github.com/kiberzauras/laravel.multilanguage
* @version 2.0.2
* @license MIT
*/
namespace Kiberzauras\MultiLanguage;
use Illuminate\Support\ServiceProvider;
use App;
class MultiLanguageServiceProvider extends ServiceProvider
{
/**
* Set our created language constant to laravel locale
*/
public function boot()
{
defined('Language') || define('Language', config('app.locale'));
App::setLocale(Language);
}
public function register()
{
$this->registerUrlGenerator();
}
/**
* @return array
*/
public function provides()
{
return [
'url',
];
}
/**
* Overwriting laravel urlGenerator
*/
protected function registerUrlGenerator()
{
$this->app->singleton('url', function ($app) {
$routes = $app['router']->getRoutes();
// The URL generator needs the route collection that exists on the router.
// Keep in mind this is an object, so we're passing by references here
// and all the registered routes will be available to the generator.
$app->instance('routes', $routes);
$url = new UrlGenerator(
$routes, $app->rebinding(
'request', $this->requestRebinder()
)
);
$url->setSessionResolver(function () {
return $this->app['session'];
});
// If the route collection is "rebound", for example, when the routes stay
// cached for the application, we will need to rebind the routes on the
// URL generator instance so it has the latest version of the routes.
$app->rebinding('routes', function ($app, $routes) {
$app['url']->setRoutes($routes);
});
return $url;
});
}
/**
* @return \Closure
*/
protected function requestRebinder()
{
return function ($app, $request) {
$app['url']->setRequest($request);
};
}
}