1 | <?php |
||
2 | |||
3 | namespace LaravelPolyglot\Providers; |
||
4 | |||
5 | use Illuminate\Translation\TranslationServiceProvider as IlluminateTranslationServiceProvider; |
||
6 | use LaravelPolyglot\Translation\Translator; |
||
7 | use Polyglot\Polyglot; |
||
8 | use Illuminate\Support\Str; |
||
9 | |||
10 | class TranslationServiceProvider extends IlluminateTranslationServiceProvider |
||
11 | { |
||
12 | /** |
||
13 | * @inheritDoc |
||
14 | */ |
||
15 | public function register() |
||
16 | { |
||
17 | parent::register(); |
||
18 | |||
19 | $this->registerPolyglot(); |
||
20 | $this->registerTranslator(); |
||
21 | |||
22 | $this->mergeConfigFrom(__DIR__.'/../config/laravel-polyglot.php', 'laravel-polyglot'); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Bootstrap the application services. |
||
27 | */ |
||
28 | public function boot() |
||
29 | { |
||
30 | if ($this->app->runningInConsole() && ! Str::contains($this->app->version(), 'Lumen')) { |
||
31 | $this->publishes([ |
||
32 | __DIR__.'/../config/laravel-polyglot.php' => config_path('laravel-polyglot.php'), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
33 | ], 'config'); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get the services provided by the provider. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function provides() |
||
43 | { |
||
44 | return array_merge(parent::provides(), ['translation.polyglot']); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Register the polyglot instance. |
||
49 | */ |
||
50 | protected function registerPolyglot() |
||
51 | { |
||
52 | $this->app->singleton('translation.polyglot', static function ($app) { |
||
53 | $defaultLocale = $app['config']['app.locale']; |
||
54 | $polyglotConfig = $app['config']['laravel-polyglot']['polyglot'] ?? []; |
||
55 | |||
56 | $options = [ |
||
57 | 'locale' => $defaultLocale, |
||
58 | 'allowMissing' => $polyglotConfig['allowMissing'], |
||
59 | 'delimiter' => $polyglotConfig['delimiter'], |
||
60 | 'interpolation' => $polyglotConfig['interpolation'], |
||
61 | 'pluralRules' => $polyglotConfig['pluralRules'], |
||
62 | ]; |
||
63 | |||
64 | if (isset($polyglotConfig['onMissingKey']) && |
||
65 | is_callable($polyglotConfig['onMissingKey']) && |
||
66 | in_array(env('APP_ENV', 'production'), $polyglotConfig['onMissingKeyEnvs']) |
||
67 | ) { |
||
68 | $options['onMissingKey'] = $polyglotConfig['onMissingKey']; |
||
69 | } |
||
70 | |||
71 | return new Polyglot($options); |
||
72 | }); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Register the translator. |
||
77 | */ |
||
78 | protected function registerTranslator() |
||
79 | { |
||
80 | $this->app->singleton('translator', static function ($app) { |
||
81 | $loader = $app['translation.loader']; |
||
82 | $polyglot = $app['translation.polyglot']; |
||
83 | |||
84 | // When registering the translator component, we'll need to set the default |
||
85 | // locale as well as the fallback locale. So, we'll grab the application |
||
86 | // configuration so we can easily get both of these values from there. |
||
87 | $locale = $app['config']['app.locale']; |
||
88 | |||
89 | $trans = new Translator($polyglot, $loader, $locale); |
||
90 | |||
91 | $trans->setFallback($app['config']['app.fallback_locale']); |
||
92 | |||
93 | return $trans; |
||
94 | }); |
||
95 | } |
||
96 | } |