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