|
1
|
|
|
<?php namespace Modules\Translation\Services; |
|
2
|
|
|
|
|
3
|
|
|
use Modules\Translation\Repositories\FileTranslationRepository; |
|
4
|
|
|
use Modules\Translation\Repositories\TranslationRepository; |
|
5
|
|
|
use Modules\Translation\ValueObjects\TranslationGroup; |
|
6
|
|
|
|
|
7
|
|
|
class TranslationsService |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var FileTranslationRepository |
|
11
|
|
|
*/ |
|
12
|
|
|
private $fileTranslations; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var TranslationRepository |
|
15
|
|
|
*/ |
|
16
|
|
|
private $databaseTranslations; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->fileTranslations = app(FileTranslationRepository::class); |
|
21
|
|
|
$this->databaseTranslations = app(TranslationRepository::class); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get the file translations & the database translations, overwrite the file translations by db translations |
|
26
|
|
|
* @return TranslationGroup |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getFileAndDatabaseMergedTranslations() |
|
29
|
|
|
{ |
|
30
|
|
|
$allFileTranslations = $this->fileTranslations->all(); |
|
31
|
|
|
$allDatabaseTranslations = $this->databaseTranslations->allFormatted(); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($allFileTranslations as $locale => $fileTranslation) { |
|
34
|
|
|
foreach ($fileTranslation as $key => $translation) { |
|
35
|
|
|
if (is_string($translation) === false) { |
|
36
|
|
|
unset($allFileTranslations[$locale][$key]); |
|
37
|
|
|
} |
|
38
|
|
|
if (isset($allDatabaseTranslations[$locale][$key])) { |
|
39
|
|
|
$allFileTranslations[$locale][$key] = $allDatabaseTranslations[$locale][$key]; |
|
40
|
|
|
unset($allDatabaseTranslations[$locale][$key]); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->addDatabaseOnlyTranslations($allFileTranslations, $allDatabaseTranslations); |
|
46
|
|
|
$this->filterOnlyActiveLocales($allFileTranslations); |
|
47
|
|
|
|
|
48
|
|
|
return new TranslationGroup($allFileTranslations); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Filter out the non-active locales |
|
53
|
|
|
* @param array $allFileTranslations |
|
54
|
|
|
*/ |
|
55
|
|
|
private function filterOnlyActiveLocales(array &$allFileTranslations) |
|
56
|
|
|
{ |
|
57
|
|
|
$activeLocales = $this->getActiveLocales(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($allFileTranslations as $locale => $value) { |
|
60
|
|
|
if (! in_array($locale, $activeLocales)) { |
|
61
|
|
|
unset($allFileTranslations[$locale]); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the currently active locales |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
private function getActiveLocales() |
|
71
|
|
|
{ |
|
72
|
|
|
$locales = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach (config('laravellocalization.supportedLocales') as $locale => $translation) { |
|
75
|
|
|
$locales[] = $locale; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $locales; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $allFileTranslations |
|
83
|
|
|
* @param array $allDatabaseTranslations |
|
84
|
|
|
*/ |
|
85
|
|
|
private function addDatabaseOnlyTranslations(array &$allFileTranslations, array $allDatabaseTranslations) |
|
86
|
|
|
{ |
|
87
|
|
|
foreach ($allDatabaseTranslations as $locale => $group) { |
|
88
|
|
|
foreach ($group as $key => $value) { |
|
89
|
|
|
$allFileTranslations[$locale][$key] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|