1
|
|
|
<?php namespace Propaganistas\LaravelIntl; |
2
|
|
|
|
3
|
|
|
use Illuminate\Foundation\Events\LocaleUpdated; |
4
|
|
|
use Illuminate\Support\ServiceProvider; |
5
|
|
|
use Jenssegers\Date\Date; |
6
|
|
|
use Jenssegers\Date\DateServiceProvider; |
7
|
|
|
use Punic\Data as Punic; |
8
|
|
|
|
9
|
|
|
class IntlServiceProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Register the service provider. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
147 |
|
public function register() |
17
|
|
|
{ |
18
|
147 |
|
$this->registerCountry(); |
19
|
147 |
|
$this->registerCurrency(); |
20
|
147 |
|
$this->registerLanguage(); |
21
|
147 |
|
$this->registerNumber(); |
22
|
147 |
|
$this->registerDate(); |
23
|
147 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Bootstrap the application services. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
147 |
|
public function boot() |
31
|
|
|
{ |
32
|
|
|
$this->app['events']->listen(LocaleUpdated::class, function ($locale) { |
|
|
|
|
33
|
69 |
|
$this->setLocale(); |
34
|
147 |
|
}); |
35
|
|
|
|
36
|
147 |
|
$this->setLocale(); |
37
|
147 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register the country repository. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
147 |
View Code Duplication |
protected function registerCountry() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->app->singleton(Country::class, function ($app) { |
47
|
21 |
|
$repository = new Country; |
48
|
|
|
|
49
|
21 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
50
|
147 |
|
}); |
51
|
|
|
|
52
|
147 |
|
$this->app->alias(Country::class, 'intl.country'); |
53
|
147 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register the currency repository. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
147 |
View Code Duplication |
protected function registerCurrency() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->app->singleton(Currency::class, function ($app) { |
63
|
36 |
|
$repository = new Currency; |
64
|
|
|
|
65
|
36 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
66
|
147 |
|
}); |
67
|
|
|
|
68
|
147 |
|
$this->app->alias(Currency::class, 'intl.currency'); |
69
|
147 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Register the language repository. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
147 |
View Code Duplication |
protected function registerLanguage() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->app->singleton(Language::class, function ($app) { |
79
|
24 |
|
$repository = new Language; |
80
|
|
|
|
81
|
24 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
82
|
147 |
|
}); |
83
|
|
|
|
84
|
147 |
|
$this->app->alias(Language::class, 'intl.language'); |
85
|
147 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Register the number repository. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
147 |
View Code Duplication |
protected function registerNumber() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$this->app->singleton(Number::class, function ($app) { |
95
|
24 |
|
$repository = new Number; |
96
|
|
|
|
97
|
24 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
98
|
147 |
|
}); |
99
|
|
|
|
100
|
147 |
|
$this->app->alias(Number::class, 'intl.number'); |
101
|
147 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Register the date handler. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
147 |
|
protected function registerDate() |
109
|
|
|
{ |
110
|
147 |
|
$this->app->register(DateServiceProvider::class); |
111
|
|
|
|
112
|
147 |
|
require __DIR__.'/Macros/Carbon.php'; |
113
|
147 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set locales on sub-libraries. |
117
|
|
|
* |
118
|
|
|
* @throws \Punic\Exception\InvalidLocale |
119
|
|
|
*/ |
120
|
147 |
|
protected function setLocale() |
121
|
|
|
{ |
122
|
147 |
|
$locale = $this->app['config']['app.locale']; |
123
|
147 |
|
$fallbackLocale = $this->app['config']['app.fallback_locale']; |
124
|
|
|
|
125
|
147 |
|
Punic::setDefaultLocale($locale); |
126
|
147 |
|
Punic::setFallbackLocale($fallbackLocale); |
127
|
|
|
|
128
|
147 |
|
Date::setLocale($locale); |
129
|
147 |
|
Date::setFallbackLocale($fallbackLocale); |
130
|
147 |
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.