TranslationServiceProvider::provides()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                __DIR__.'/../config/laravel-polyglot.php' => /** @scrutinizer ignore-call */ config_path('laravel-polyglot.php'),
Loading history...
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
}