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