diff --git a/CHANGELOG.md b/CHANGELOG.md index 79f32b9..c8a1bdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,12 @@ All notable changes to `laravel-translatable` will be documented in this file -## 2.1.3 - 2018-02-08 +## 2.1.4 - 2018-02-08 - add support for L5.6 +## 2.1.3 - 2018-01-24 +- make locale handling more flexible + ## 2.1.2 - 2017-12-24 - fix for using translations within translations diff --git a/src/HasTranslations.php b/src/HasTranslations.php index 43e919a..05d655b 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -19,7 +19,7 @@ public function getAttributeValue($key) return parent::getAttributeValue($key); } - return $this->getTranslation($key, config('app.locale')); + return $this->getTranslation($key, $this->getLocale()); } /** @@ -38,7 +38,7 @@ public function setAttribute($key, $value) } // if the attribute is translatable and not already translated (=array), // set a translation for the current app locale - return $this->setTranslation($key, config('app.locale'), $value); + return $this->setTranslation($key, $this->getLocale(), $value); } /** @@ -196,6 +196,11 @@ protected function normalizeLocale(string $key, string $locale, bool $useFallbac return $locale; } + protected function getLocale() : string + { + return config('app.locale'); + } + public function getTranslatableAttributes() : array { return is_array($this->translatable) diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index c7e7ced..60fba34 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -307,7 +307,7 @@ public function setNameAttribute($value) $translations = [ 'nl' => 'hallo', 'en' => 'hello', - 'kh' => 'សួរស្តី' + 'kh' => 'សួរស្តី', ]; $testModel->setTranslations('name', $translations); @@ -317,7 +317,7 @@ public function setNameAttribute($value) $expected = [ 'nl' => 'I just mutated hallo', 'en' => 'I just mutated hello', - 'kh' => 'I just mutated សួរស្តី' + 'kh' => 'I just mutated សួរស្តី', ]; $this->assertEquals($expected, $testModel->getTranslations('name')); @@ -335,19 +335,19 @@ public function setOtherFieldAttribute($value, $locale = 'en') $testModel->setTranslations('name', [ 'nl' => 'wereld', - 'en' => 'world' + 'en' => 'world', ]); $testModel->setTranslations('other_field', [ 'nl' => 'hallo', - 'en' => 'hello' + 'en' => 'hello', ]); $testModel->save(); $expected = [ 'nl' => 'hallo wereld', - 'en' => 'hello world' + 'en' => 'hello world', ]; $this->assertEquals($expected, $testModel->getTranslations('other_field'));