1
|
|
|
<?php namespace Propaganistas\LaravelIntl; |
2
|
|
|
|
3
|
|
|
use Illuminate\Foundation\Events\LocaleUpdated; |
4
|
|
|
use Illuminate\Support\ServiceProvider; |
5
|
|
|
use Punic\Data as Punic; |
6
|
|
|
|
7
|
|
|
class IntlServiceProvider extends ServiceProvider |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Register the service provider. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
144 |
|
public function register() |
15
|
|
|
{ |
16
|
144 |
|
$this->registerCountry(); |
17
|
144 |
|
$this->registerCurrency(); |
18
|
144 |
|
$this->registerLanguage(); |
19
|
144 |
|
$this->registerNumber(); |
20
|
144 |
|
$this->registerDate(); |
21
|
144 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Bootstrap the application services. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
144 |
|
public function boot() |
29
|
|
|
{ |
30
|
|
|
$this->app['events']->listen(LocaleUpdated::class, function ($locale) { |
31
|
66 |
|
$this->setLocale(); |
32
|
144 |
|
}); |
33
|
|
|
|
34
|
144 |
|
$this->setLocale(); |
35
|
144 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Register the country repository. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
144 |
View Code Duplication |
protected function registerCountry() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->app->singleton(Country::class, function ($app) { |
45
|
21 |
|
$repository = new Country; |
46
|
|
|
|
47
|
21 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
48
|
144 |
|
}); |
49
|
|
|
|
50
|
144 |
|
$this->app->alias(Country::class, 'intl.country'); |
51
|
144 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register the currency repository. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
144 |
View Code Duplication |
protected function registerCurrency() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->app->singleton(Currency::class, function ($app) { |
61
|
36 |
|
$repository = new Currency; |
62
|
|
|
|
63
|
36 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
64
|
144 |
|
}); |
65
|
|
|
|
66
|
144 |
|
$this->app->alias(Currency::class, 'intl.currency'); |
67
|
144 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register the language repository. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
144 |
View Code Duplication |
protected function registerLanguage() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->app->singleton(Language::class, function ($app) { |
77
|
24 |
|
$repository = new Language; |
78
|
|
|
|
79
|
24 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
80
|
144 |
|
}); |
81
|
|
|
|
82
|
144 |
|
$this->app->alias(Language::class, 'intl.language'); |
83
|
144 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Register the number repository. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
144 |
View Code Duplication |
protected function registerNumber() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->app->singleton(Number::class, function ($app) { |
93
|
24 |
|
$repository = new Number; |
94
|
|
|
|
95
|
24 |
|
return $repository->setLocale($app['config']['app.locale'])->setFallbackLocale($app['config']['app.fallback_locale']); |
96
|
144 |
|
}); |
97
|
|
|
|
98
|
144 |
|
$this->app->alias(Number::class, 'intl.number'); |
99
|
144 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Register the date handler. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
144 |
|
protected function registerDate() |
107
|
|
|
{ |
108
|
144 |
|
require __DIR__.'/Macros/Carbon.php'; |
109
|
144 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set locales on sub-libraries. |
113
|
|
|
* |
114
|
|
|
* @throws \Punic\Exception\InvalidLocale |
115
|
|
|
*/ |
116
|
144 |
|
protected function setLocale() |
117
|
|
|
{ |
118
|
144 |
|
$locale = $this->app['config']['app.locale']; |
119
|
144 |
|
$fallbackLocale = $this->app['config']['app.fallback_locale']; |
120
|
|
|
|
121
|
144 |
|
Punic::setDefaultLocale($locale); |
122
|
144 |
|
Punic::setFallbackLocale($fallbackLocale); |
123
|
144 |
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.