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