Skip to content

Commit

Permalink
[feature] elegant syntax update (#229)
Browse files Browse the repository at this point in the history
* Update getLocale, Add withLocale

* Add tests

Code style

Update README

* Update README

* Update property name to avoid collisions
  • Loading branch information
angauber authored Oct 2, 2020
1 parent cc2ce05 commit d265055
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ $translations = [
$newsItem->setTranslations('name', $translations);
```

#### Setting the model locale
The default locale used to translate models is the application locale,
however it can sometimes be handy to use a custom locale.

To do so, you can use `setLocale` on a model instance.
``` php
$newsItem = NewsItem::firstOrFail()->setLocale('fr');

// Any properties on this model will automaticly be translated in French
$newsItem->name; // Will return `fr` translation
$newsItem->name = 'Actualité'; // Will set the `fr` translation
```

Alternatively, you can use `withLocale` static method:
``` php
// Will automatically set the `fr` translation
$newsItem = NewsItem::withLocale('fr')->create([
'name' => 'Actualité',
]);
```

### Events

#### TranslationHasBeenSet
Expand Down
18 changes: 16 additions & 2 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

trait HasTranslations
{
protected $translationLocale = null;

public static function withLocale(string $locale): self
{
return (new self())->setLocale($locale);
}

public function getAttributeValue($key)
{
if (! $this->isTranslatableAttribute($key)) {
Expand Down Expand Up @@ -185,9 +192,16 @@ protected function normalizeLocale(string $key, string $locale, bool $useFallbac
return $locale;
}

protected function getLocale(): string
public function setLocale(string $locale): self
{
$this->translationLocale = $locale;

return $this;
}

public function getLocale(): string
{
return config('app.locale');
return $this->translationLocale ?: config('app.locale');
}

public function getTranslatableAttributes(): array
Expand Down
32 changes: 32 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,36 @@ public function it_will_not_remove_zero_value_of_other_locale_in_database()

$this->assertSame('0', $this->testModel->getTranslation('name', 'nl'));
}

/** @test */
public function it_can_be_translated_based_on_given_locale()
{
$value = 'World';

$this->testModel = TestModel::withLocale('en')->fill([
'name' => $value,
]);
$this->testModel->save();

$this->assertSame($value, $this->testModel->getTranslation('name', 'en'));
}

/** @test */
public function it_can_set_and_fetch_attributes_based_on_set_locale()
{
$en = 'World';
$fr = 'Monde';

$this->testModel->setLocale('en');
$this->testModel->name = $en;
$this->testModel->setLocale('fr');
$this->testModel->name = $fr;

$this->testModel->save();

$this->testModel->setLocale('en');
$this->assertSame($en, $this->testModel->name);
$this->testModel->setLocale('fr');
$this->assertSame($fr, $this->testModel->name);
}
}

0 comments on commit d265055

Please sign in to comment.