1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\I18n; |
4
|
|
|
|
5
|
|
|
use Nip\Container\ServiceProviders\Providers\AbstractSignatureServiceProvider; |
6
|
|
|
use Nip\I18n\Translator\Backend\AbstractBackend; |
7
|
|
|
use Nip\I18n\Translator\Backend\File; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MailServiceProvider |
11
|
|
|
* @package Nip\Mail |
12
|
|
|
*/ |
13
|
|
|
class TranslatorServiceProvider extends AbstractSignatureServiceProvider |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $languages = null; |
17
|
|
|
|
18
|
|
|
protected $languageDirectory = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
1 |
|
public function register() |
24
|
|
|
{ |
25
|
1 |
|
$this->registerTranslator(); |
26
|
1 |
|
$this->registerLanguages(); |
27
|
1 |
|
$this->registerLoader(); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Register the session manager instance. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
1 |
|
protected function registerTranslator() |
36
|
|
|
{ |
37
|
1 |
|
$this->getContainer()->share('translator', Translator::class) |
38
|
1 |
|
->withArgument(AbstractBackend::class); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register the translation line loader. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function registerLoader() |
47
|
|
|
{ |
48
|
1 |
|
$this->getContainer()->share('translation.loader', function () { |
49
|
1 |
|
$backend = $this->createFileBackend(); |
50
|
1 |
|
return $backend; |
51
|
1 |
|
}); |
52
|
|
|
|
53
|
1 |
|
$this->getContainer()->alias('translation.loader', AbstractBackend::class); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
protected function registerLanguages() |
57
|
|
|
{ |
58
|
1 |
|
$this->getContainer()->share('translation.languages', function () { |
59
|
1 |
|
return $this->getLanguages(); |
60
|
1 |
|
}); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return File |
65
|
|
|
*/ |
66
|
1 |
|
protected function createFileBackend() |
67
|
|
|
{ |
68
|
|
|
/** @var File $backend */ |
69
|
1 |
|
$backend = $this->getContainer()->get(File::class); |
70
|
1 |
|
$backend->setBaseDirectory($this->getLanguageDirectory()); |
71
|
|
|
|
72
|
1 |
|
$languages = $this->getContainer()->get('translation.languages'); |
73
|
1 |
|
$backend->addLanguages($languages); |
74
|
|
|
|
75
|
1 |
|
return $backend; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function provides() |
82
|
|
|
{ |
83
|
|
|
return ['translator', 'translator.languages', 'translation.loader']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return null |
88
|
|
|
*/ |
89
|
1 |
|
public function getLanguages() |
90
|
|
|
{ |
91
|
1 |
|
if ($this->languages === null) { |
92
|
|
|
$this->initLanguages(); |
93
|
|
|
} |
94
|
1 |
|
return $this->languages; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function initLanguages() |
98
|
|
|
{ |
99
|
|
|
$languages = config('app.locale.enabled'); |
100
|
|
|
return is_array($languages) ? $languages : explode(',', $languages); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param null $languages |
105
|
|
|
*/ |
106
|
1 |
|
public function setLanguages($languages) |
107
|
|
|
{ |
108
|
1 |
|
$this->languages = $languages; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return null |
113
|
|
|
*/ |
114
|
1 |
|
public function getLanguageDirectory() |
115
|
|
|
{ |
116
|
1 |
|
if ($this->languageDirectory === null) { |
117
|
|
|
$this->initLanguageDirectory(); |
118
|
|
|
} |
119
|
1 |
|
return $this->languageDirectory; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param null $languageDirectory |
124
|
|
|
*/ |
125
|
1 |
|
public function setLanguageDirectory($languageDirectory) |
126
|
|
|
{ |
127
|
1 |
|
$this->languageDirectory = $languageDirectory; |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
protected function initLanguageDirectory() |
131
|
|
|
{ |
132
|
|
|
return app('path.lang'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
} |