1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
6
|
|
|
use Thinktomorrow\Squanto\Services\CachedTranslationFile; |
7
|
|
|
use Thinktomorrow\Squanto\Import\ImportTranslationsCommand; |
8
|
|
|
use Illuminate\Translation\TranslationServiceProvider as BaseServiceProvider; |
9
|
|
|
use League\Flysystem\Adapter\Local; |
10
|
|
|
use Thinktomorrow\Squanto\Services\CacheTranslationsCommand; |
11
|
|
|
use Thinktomorrow\Squanto\Services\LaravelTranslationsReader; |
12
|
|
|
use Thinktomorrow\Squanto\Translators\SquantoTranslator; |
13
|
|
|
|
14
|
|
|
class SquantoServiceProvider extends BaseServiceProvider |
15
|
|
|
{ |
16
|
|
|
protected $defer = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
1 |
|
public function provides() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
1 |
|
'translator', |
25
|
|
|
'translation.loader', |
26
|
|
|
'Thinktomorrow\\Squanto\\Handlers\\ClearCacheTranslations', |
27
|
|
|
'Thinktomorrow\\Squanto\\Handlers\\WriteTranslationLineToDisk', |
28
|
|
|
'Thinktomorrow\\Squanto\\Services\\LaravelTranslationsReader', |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Bootstrap any application services. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
55 |
|
public function boot() |
38
|
|
|
{ |
39
|
55 |
|
$this->loadTranslationsFrom($this->getSquantoCachePath(), 'squanto'); |
40
|
|
|
|
41
|
55 |
|
if ($this->app->runningInConsole()) { |
42
|
55 |
|
$this->publishes([ |
43
|
55 |
|
__DIR__.'/../config/squanto.php' => config_path('squanto.php') |
44
|
55 |
|
], 'config'); |
45
|
|
|
|
46
|
55 |
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); |
47
|
|
|
|
48
|
55 |
|
$this->commands([ |
49
|
55 |
|
ImportTranslationsCommand::class, |
50
|
|
|
CacheTranslationsCommand::class, |
51
|
|
|
]); |
52
|
|
|
} |
53
|
55 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register our translator |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
55 |
|
public function register() |
61
|
|
|
{ |
62
|
55 |
|
$this->app['squanto.cache_path'] = $this->getSquantoCachePath(); |
63
|
55 |
|
$this->app['squanto.lang_path'] = $this->getSquantoLangPath(); |
64
|
|
|
|
65
|
55 |
|
$this->registerTranslator(); |
66
|
|
|
|
67
|
|
|
$this->app->bind(CachedTranslationFile::class, function ($app) { |
|
|
|
|
68
|
|
|
return new CachedTranslationFile( |
69
|
|
|
new Filesystem(new Local($this->getSquantoCachePath())) |
70
|
|
|
); |
71
|
55 |
|
}); |
72
|
|
|
|
73
|
|
|
$this->app->bind(LaravelTranslationsReader::class, function ($app) { |
|
|
|
|
74
|
5 |
|
return new LaravelTranslationsReader( |
75
|
5 |
|
new Filesystem(new Local($this->getSquantoLangPath())) |
76
|
|
|
); |
77
|
55 |
|
}); |
78
|
|
|
|
79
|
55 |
|
$this->mergeConfigFrom(__DIR__.'/../config/squanto.php', 'squanto'); |
80
|
55 |
|
} |
81
|
|
|
|
82
|
55 |
|
private function registerTranslator() |
83
|
|
|
{ |
84
|
55 |
|
$this->registerLoader(); |
85
|
|
|
|
86
|
|
|
$this->app->singleton('translator', function ($app) { |
87
|
|
|
|
88
|
55 |
|
$loader = $app['translation.loader']; |
89
|
55 |
|
$locale = $app['config']['app.locale']; |
90
|
|
|
|
91
|
55 |
|
$trans = new SquantoTranslator($loader, $locale); |
92
|
|
|
|
93
|
55 |
|
$trans->setFallback($app['config']['app.fallback_locale']); |
94
|
|
|
|
95
|
|
|
// Custom Squanto option to display key or null when translation is not found |
96
|
55 |
|
$trans->setKeyAsDefault($app['config']['squanto.key_as_default']); |
97
|
|
|
|
98
|
55 |
|
return $trans; |
99
|
55 |
|
}); |
100
|
55 |
|
} |
101
|
|
|
|
102
|
55 |
|
private function getSquantoCachePath() |
103
|
|
|
{ |
104
|
55 |
|
$path = config('squanto.cache_path'); |
105
|
55 |
|
return is_null($path) ? storage_path('app/trans') : $path; |
106
|
|
|
} |
107
|
|
|
|
108
|
55 |
|
private function getSquantoLangPath() |
109
|
|
|
{ |
110
|
55 |
|
$path = config('squanto.lang_path'); |
111
|
55 |
|
return is_null($path) ? app('path.lang') : $path; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.