Skip to content

Commit 0d4610c

Browse files
committed
Add active locale for the model.
1 parent 6cb23be commit 0d4610c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/Traits/HasTranslation.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ trait HasTranslation
2727
*/
2828
protected $translationModel;
2929

30+
/**
31+
* @var string
32+
*/
33+
protected $activeLocale;
34+
3035
/**
3136
* Bootstrap the trait
3237
*/
@@ -149,7 +154,7 @@ public function unsetTranslation($locale, $key = null)
149154
public function setAttribute($key, $value)
150155
{
151156
if ($this->isTranslatable($key)) {
152-
$this->setTranslation($key, $this->getDefaultLocale(), $value);
157+
$this->setTranslation($key, $this->getActiveLocale(), $value);
153158
}
154159

155160
return parent::setAttribute($key, $value);
@@ -200,6 +205,26 @@ public function getDefaultLocale()
200205
return config('app.locale');
201206
}
202207

208+
/**
209+
* @return string
210+
*/
211+
public function getActiveLocale(): string
212+
{
213+
if (! isset($this->activeLocale)) {
214+
$this->activeLocale = $this->getDefaultLocale();
215+
}
216+
217+
return $this->activeLocale;
218+
}
219+
220+
/**
221+
* @param string $activeLocale
222+
*/
223+
public function setActiveLocale(string $activeLocale)
224+
{
225+
$this->activeLocale = $activeLocale;
226+
}
227+
203228
/**
204229
* @return string
205230
*/

0 commit comments

Comments
 (0)