|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Domain; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait Translatable |
|
9
|
|
|
* @author Ben Cavens |
|
10
|
|
|
* |
|
11
|
|
|
* Allows the entity to contain multiple translations |
|
12
|
|
|
* requires the parent entity to include the Dimsav/Translatable/Translatable trait |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
trait Translatable |
|
16
|
|
|
{ |
|
17
|
|
|
public function getDefaultTranslation($attribute) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!($translation = $this->getTranslation(config('app.fallback_locale')))) { |
|
|
|
|
|
|
20
|
|
|
return null; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $translation->$attribute; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Save multiple attributes at once |
|
28
|
|
|
* |
|
29
|
|
|
* @param $locale |
|
30
|
|
|
* @param array $values |
|
31
|
|
|
*/ |
|
32
|
|
|
public function updateTranslation($locale, array $values) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($values as $attribute => $value) { |
|
35
|
|
|
$this->setTranslation($locale, $attribute, $value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->save(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Save a single attribute |
|
43
|
|
|
* |
|
44
|
|
|
* @param $locale |
|
45
|
|
|
* @param $attribute |
|
46
|
|
|
* @param $value |
|
47
|
|
|
*/ |
|
48
|
35 |
|
public function saveTranslation($locale, $attribute, $value) |
|
49
|
|
|
{ |
|
50
|
35 |
|
$this->setTranslation($locale, $attribute, $value); |
|
51
|
|
|
|
|
52
|
35 |
|
$this->save(); |
|
|
|
|
|
|
53
|
35 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $attribute |
|
57
|
|
|
* @param null $locale |
|
58
|
|
|
* @param bool $fallback | if locale not present, use fallback locale instead |
|
59
|
|
|
* @return string null |
|
60
|
|
|
*/ |
|
61
|
24 |
|
public function getTranslationFor($attribute, $locale = null, $fallback = true) |
|
62
|
|
|
{ |
|
63
|
24 |
|
return (!$trans = $this->getTranslation($locale, $fallback)) ? null : $trans->$attribute; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create or update a translation attribute. |
|
68
|
|
|
* Note: only sets to entity, does not save it. |
|
69
|
|
|
* |
|
70
|
|
|
* @param $locale |
|
71
|
|
|
* @param $attribute |
|
72
|
|
|
* @param $value |
|
73
|
|
|
*/ |
|
74
|
35 |
|
private function setTranslation($locale, $attribute, $value) |
|
75
|
|
|
{ |
|
76
|
35 |
|
$this->validateLocale($locale); |
|
77
|
|
|
|
|
78
|
35 |
|
$this->translateOrNew($locale)->$attribute = $value; |
|
|
|
|
|
|
79
|
35 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function removeTranslation($locale) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$this->hasTranslation($locale)) { |
|
|
|
|
|
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->getTranslation($locale)->delete(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get all locales where this entity |
|
92
|
|
|
* already has any translations of |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getUsedLocales() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->fetchLocales(true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get all available locales where this entity |
|
103
|
|
|
* does not have any translations of |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getNonUsedLocales() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->fetchLocales(false); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the locales from squanto config. |
|
114
|
|
|
* Fallback is the list provided via the translation package |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
35 |
|
public function getAvailableLocales() |
|
119
|
|
|
{ |
|
120
|
35 |
|
return config('squanto.locales', config('translatable.locales', [])); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get all locales associated with this entity |
|
125
|
|
|
* |
|
126
|
|
|
* @param bool $available |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
private function fetchLocales($available = true) |
|
130
|
|
|
{ |
|
131
|
|
|
$available_locales = $this->getAvailableLocales(); |
|
132
|
|
|
$current_locales = $this->translations()->lists('locale')->toArray(); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
return array_filter($available_locales, function ($v) use ($current_locales, $available) { |
|
135
|
|
|
return $available ? in_array($v, $current_locales) : !in_array($v, $current_locales); |
|
136
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Is passed locale one of the allowed ones from config? |
|
141
|
|
|
* |
|
142
|
|
|
* @param $locale |
|
143
|
|
|
*/ |
|
144
|
35 |
|
private function validateLocale($locale) |
|
145
|
|
|
{ |
|
146
|
35 |
|
if (!in_array($locale, $this->getAvailableLocales())) { |
|
147
|
|
|
throw new InvalidArgumentException('Improper locale [' . $locale . '] given or locale is not available'); |
|
148
|
|
|
} |
|
149
|
35 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Dimsav translatable trait overrides the toArray in order to |
|
153
|
|
|
* inject default translations. To ignore this behaviour and |
|
154
|
|
|
* present the actual values you should use this method. |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
public function toRawArray() |
|
159
|
|
|
{ |
|
160
|
|
|
return parent::toArray(); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.