1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable; |
4
|
|
|
|
5
|
|
|
use Spatie\Translatable\HasTranslations; |
|
|
|
|
6
|
|
|
|
7
|
|
|
trait HasTranslations |
8
|
|
|
{ |
9
|
|
|
use HasTranslations; |
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
|
|
|
return is_array($translation) ? array_first($translation) : $translation; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/* |
40
|
|
|
|-------------------------------------------------------------------------- |
41
|
|
|
| ELOQUENT OVERWRITES |
42
|
|
|
|-------------------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create translated items as json. |
47
|
|
|
* |
48
|
|
|
* @param array $attributes |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public static function create(array $attributes = []) |
52
|
|
|
{ |
53
|
|
|
$locale = $attributes['locale'] ?? \App::getLocale(); |
54
|
|
|
$attributes = array_except($attributes, ['locale']); |
55
|
|
|
|
56
|
|
|
$model = new static(); |
57
|
|
|
|
58
|
|
|
// do the actual saving |
59
|
|
|
foreach ($attributes as $attribute => $value) { |
60
|
|
|
if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
61
|
|
|
$model->setTranslation($attribute, $locale, $value); |
62
|
|
|
} else { // the attribute is NOT translatable |
63
|
|
|
$model->{$attribute} = $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$model->save(); |
67
|
|
|
|
68
|
|
|
return $model; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Update translated items as json. |
73
|
|
|
* |
74
|
|
|
* @param array $attributes |
75
|
|
|
* @param array $options |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function update(array $attributes = [], array $options = []) |
79
|
|
|
{ |
80
|
|
|
if (! $this->exists) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$locale = $attributes['locale'] ?? \App::getLocale(); |
85
|
|
|
$attributes = array_except($attributes, ['locale']); |
86
|
|
|
|
87
|
|
|
// do the actual saving |
88
|
|
|
foreach ($attributes as $attribute => $value) { |
89
|
|
|
if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
90
|
|
|
$this->setTranslation($attribute, $locale, $value); |
91
|
|
|
} else { // the attribute is NOT translatable |
92
|
|
|
$this->{$attribute} = $value; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->save($options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
|-------------------------------------------------------------------------- |
101
|
|
|
| CUSTOM METHODS |
102
|
|
|
|-------------------------------------------------------------------------- |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if a model is translatable, by the adapter's standards. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function translationEnabledForModel() |
111
|
|
|
{ |
112
|
|
|
return property_exists($this, 'translatable'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get all locales the admin is allowed to use. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getAvailableLocales() |
121
|
|
|
{ |
122
|
|
|
return config('backpack.crud.locales'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the locale property. Used in normalizeLocale() to force the translation |
127
|
|
|
* to a different language that the one set in app()->getLocale();. |
128
|
|
|
* |
129
|
|
|
* @param string |
130
|
|
|
*/ |
131
|
|
|
public function setLocale($locale) |
132
|
|
|
{ |
133
|
|
|
$this->locale = $locale; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the locale property. Used in SpatieTranslatableSluggableService |
138
|
|
|
* to save the slug for the appropriate language. |
139
|
|
|
* |
140
|
|
|
* @param string |
141
|
|
|
*/ |
142
|
|
|
public function getLocale() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
if ($this->locale) { |
145
|
|
|
return $this->locale; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return \Request::input('locale', \App::getLocale()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Magic method to get the db entries already translated in the wanted locale. |
153
|
|
|
* |
154
|
|
|
* @param string $method |
155
|
|
|
* @param array $parameters |
156
|
|
|
* @return |
157
|
|
|
*/ |
158
|
|
|
public function __call($method, $parameters) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
switch ($method) { |
161
|
|
|
// translate all find methods |
162
|
|
|
case 'find': |
|
|
|
|
163
|
|
|
case 'findOrFail': |
|
|
|
|
164
|
|
|
case 'findMany': |
|
|
|
|
165
|
|
|
case 'findBySlug': |
|
|
|
|
166
|
|
|
case 'findBySlugOrFail': |
|
|
|
|
167
|
|
|
|
168
|
|
|
$translation_locale = \Request::input('locale', \App::getLocale()); |
169
|
|
|
|
170
|
|
|
if ($translation_locale) { |
171
|
|
|
$item = parent::__call($method, $parameters); |
172
|
|
|
|
173
|
|
|
if ($item) { |
174
|
|
|
$item->setLocale($translation_locale); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $item; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return parent::__call($method, $parameters); |
181
|
|
|
break; |
|
|
|
|
182
|
|
|
|
183
|
|
|
// do not translate any other methods |
184
|
|
|
default: |
185
|
|
|
return parent::__call($method, $parameters); |
186
|
|
|
break; |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: