1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Artisan Command to cache Translations from the Database. |
5
|
|
|
*/ |
6
|
|
|
namespace Hokan22\LaravelTranslator\Commands; |
7
|
|
|
|
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use Hokan22\LaravelTranslator\TranslatorFacade; |
11
|
|
|
use Hokan22\LaravelTranslator\Models\TranslationIdentifier; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CacheTranslationCommand |
15
|
|
|
* |
16
|
|
|
* @package Hokan22\LaravelTranslator\commands |
17
|
|
|
* @author Alexander Viertel <[email protected]> |
18
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
19
|
|
|
*/ |
20
|
|
|
class CacheTranslationCommand extends Command |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The name and signature of the console command. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $signature = 'translator:cache {locale}'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The console command description. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $description = 'This Command will cache the Translation in JSON Format'; |
36
|
|
|
|
37
|
|
|
protected $cache = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new command instance. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() { |
43
|
|
|
parent::__construct(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the command. |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function handle() { |
52
|
|
|
$locale = $this->argument('locale'); |
53
|
|
|
|
54
|
|
|
if (!is_string($locale)) |
55
|
|
|
{ |
56
|
|
|
$this->warn("Please specify only one locale"); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$file_path = TranslatorFacade::getConfigValue('cache_path') . $locale . '/'; |
61
|
|
|
|
62
|
|
|
$groups = $this->getGroups(); |
63
|
|
|
$translations = $this->loadFromDB($locale); |
64
|
|
|
|
65
|
|
|
if (!file_exists($file_path)) { |
66
|
|
|
$this->alert("The defined cache folder (" . $file_path . ") does not exists."); |
67
|
|
|
if (!$this->confirm('Do you want to create it now?')) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
mkdir($file_path, 0775, true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($groups as $key => $group) { |
74
|
|
|
$array = []; |
75
|
|
|
|
76
|
|
|
$tmp = $translations->where('group', $group); |
77
|
|
|
|
78
|
|
|
foreach ($tmp as $identifier) { |
79
|
|
|
if ($identifier->translations->count() <= 0) { |
80
|
|
|
$array[$identifier->identifier] = $identifier->identifier; |
81
|
|
|
} elseif ($identifier->translations()->first()->translation == null) { |
82
|
|
|
$array[$identifier->identifier] = $identifier->identifier; |
83
|
|
|
} else { |
84
|
|
|
$array[$identifier->identifier] = $identifier->translations()->first()->translation; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!empty($array)) { |
89
|
|
|
$file_name = $file_path . $group . '.json'; |
90
|
|
|
file_put_contents($file_name, json_encode($array)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get all used groups from translation identifiers |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function getGroups() { |
101
|
|
|
return DB::table('translation_identifiers')->select('group')->groupBy(['group'])->get()->pluck('group'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get all translation identifier with translation from the given locale |
106
|
|
|
* |
107
|
|
|
* @param string $locale The locale from which the translations to load |
108
|
|
|
* @return TranslationIdentifier|\Illuminate\Database\Eloquent\Collection|static[] |
109
|
|
|
*/ |
110
|
|
|
protected function loadFromDB($locale) { |
111
|
|
|
$trans_identifier = new TranslationIdentifier(); |
112
|
|
|
|
113
|
|
|
$trans_identifier = $trans_identifier->with('translations')->whereHas('translations', function($item) use ($locale) { |
114
|
|
|
return $item->where('locale', $locale); |
115
|
|
|
} |
116
|
|
|
)->orWhereHas('translations', null, '<=', 0) |
117
|
|
|
->get(); |
118
|
|
|
|
119
|
|
|
return $trans_identifier; |
120
|
|
|
} |
121
|
|
|
} |