Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] elegant syntax update #229

Merged
merged 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}