1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Models\Traits\SpatieTranslatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Spatie\Translatable\HasTranslations as OriginalHasTranslations; |
7
|
|
|
use Illuminate\Support\Facades\Request; |
8
|
|
|
use Illuminate\Support\Facades\App; |
9
|
|
|
|
10
|
|
|
trait HasTranslations |
11
|
|
|
{ |
12
|
|
|
use OriginalHasTranslations; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
public $locale = false; |
18
|
|
|
|
19
|
|
|
public bool $useFallbackLocale = true; |
20
|
|
|
|
21
|
|
|
/* |
22
|
|
|
|-------------------------------------------------------------------------- |
23
|
|
|
| SPATIE/LARAVEL-TRANSLATABLE OVERWRITES |
24
|
|
|
|-------------------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Use the forced locale if present. |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function getAttributeValue($key) |
34
|
|
|
{ |
35
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
36
|
|
|
return parent::getAttributeValue($key); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$translation = $this->getTranslation($key, $this->locale ?: config('app.locale'), $this->useFallbackLocale); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// if it's a fake field, json_encode it |
42
|
|
|
if (is_array($translation)) { |
43
|
|
|
return json_encode($translation, JSON_UNESCAPED_UNICODE); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $translation; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
50
|
|
|
{ |
51
|
|
|
$locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
52
|
|
|
|
53
|
|
|
$translations = $this->getTranslations($key); |
54
|
|
|
|
55
|
|
|
$translation = $translations[$locale] ?? ''; |
56
|
|
|
|
57
|
|
|
if ($this->hasGetMutator($key)) { |
|
|
|
|
58
|
|
|
return $this->mutateAttribute($key, $translation); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $translation; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
|-------------------------------------------------------------------------- |
66
|
|
|
| ELOQUENT OVERWRITES |
67
|
|
|
|-------------------------------------------------------------------------- |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create translated items as json. |
72
|
|
|
* |
73
|
|
|
* @param array $attributes |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
|
public static function create(array $attributes = []) |
77
|
|
|
{ |
78
|
|
|
$locale = $attributes['locale'] ?? App::getLocale(); |
79
|
|
|
$attributes = Arr::except($attributes, ['locale']); |
80
|
|
|
$non_translatable = []; |
81
|
|
|
|
82
|
|
|
$model = new static(); |
83
|
|
|
|
84
|
|
|
// do the actual saving |
85
|
|
|
foreach ($attributes as $attribute => $value) { |
86
|
|
|
if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
87
|
|
|
$model->setTranslation($attribute, $locale, $value); |
88
|
|
|
} else { // the attribute is NOT translatable |
89
|
|
|
$non_translatable[$attribute] = $value; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$model->fill($non_translatable)->save(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
return $model; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update translated items as json. |
99
|
|
|
* |
100
|
|
|
* @param array $attributes |
101
|
|
|
* @param array $options |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function update(array $attributes = [], array $options = []) |
105
|
|
|
{ |
106
|
|
|
if (! $this->exists) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$locale = $attributes['_locale'] ?? App::getLocale(); |
111
|
|
|
$attributes = Arr::except($attributes, ['_locale']); |
112
|
|
|
$non_translatable = []; |
113
|
|
|
|
114
|
|
|
// do the actual saving |
115
|
|
|
foreach ($attributes as $attribute => $value) { |
116
|
|
|
if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
117
|
|
|
$this->setTranslation($attribute, $locale, $value); |
118
|
|
|
} else { // the attribute is NOT translatable |
119
|
|
|
$non_translatable[$attribute] = $value; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->fill($non_translatable)->save($options); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/* |
127
|
|
|
|-------------------------------------------------------------------------- |
128
|
|
|
| CUSTOM METHODS |
129
|
|
|
|-------------------------------------------------------------------------- |
130
|
|
|
*/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if a model is translatable, by the adapter's standards. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function translationEnabledForModel() |
138
|
|
|
{ |
139
|
|
|
return property_exists($this, 'translatable'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get all locales the admin is allowed to use. |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getAvailableLocales() |
148
|
|
|
{ |
149
|
|
|
return config('backpack.crud.locales'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the locale property. Used in normalizeLocale() to force the translation |
154
|
|
|
* to a different language that the one set in app()->getLocale();. |
155
|
|
|
* |
156
|
|
|
* @param string |
157
|
|
|
*/ |
158
|
|
|
public function setLocale($locale) |
159
|
|
|
{ |
160
|
|
|
$this->locale = $locale; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the locale property. Used in SpatieTranslatableSluggableService |
165
|
|
|
* to save the slug for the appropriate language. |
166
|
|
|
* |
167
|
|
|
* @param string |
168
|
|
|
*/ |
169
|
|
|
public function getLocale() |
170
|
|
|
{ |
171
|
|
|
if ($this->locale) { |
172
|
|
|
return $this->locale; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return Request::input('_locale', App::getLocale()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Magic method to get the db entries already translated in the wanted locale. |
180
|
|
|
* |
181
|
|
|
* @param string $method |
182
|
|
|
* @param array $parameters |
183
|
|
|
* @return |
184
|
|
|
*/ |
185
|
|
|
public function __call($method, $parameters) |
186
|
|
|
{ |
187
|
|
|
switch ($method) { |
188
|
|
|
// translate all find methods |
189
|
|
|
case 'find': |
190
|
|
|
case 'findOrFail': |
191
|
|
|
case 'findMany': |
192
|
|
|
case 'findBySlug': |
193
|
|
|
case 'findBySlugOrFail': |
194
|
|
|
$translation_locale = Request::input('_locale', App::getLocale()); |
195
|
|
|
|
196
|
|
|
if ($translation_locale) { |
197
|
|
|
$item = parent::__call($method, $parameters); |
198
|
|
|
|
199
|
|
|
if ($item instanceof \Traversable) { |
200
|
|
|
foreach ($item as $instance) { |
201
|
|
|
$instance->setLocale($translation_locale); |
202
|
|
|
} |
203
|
|
|
} elseif ($item) { |
204
|
|
|
$item->setLocale($translation_locale); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $item; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return parent::__call($method, $parameters); |
211
|
|
|
break; |
|
|
|
|
212
|
|
|
|
213
|
|
|
// do not translate any other methods |
214
|
|
|
default: |
215
|
|
|
return parent::__call($method, $parameters); |
216
|
|
|
break; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|