Skip to content

Commit e18e43d

Browse files
Add: fallback support
1 parent c2a7d66 commit e18e43d

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ $post->title_nl
5656
$post->getTranslation('title', 'nl')
5757
```
5858

59+
### Using a fallback
60+
This package allows you to return the value of an attribute's `fallback_locale` defined in the `config/app.php` of your application.
61+
62+
The third `useFallbackLocale` parameter of the `getTranslation` method may be used to control this behaviour:
63+
```php
64+
$post->title_en = 'Your first translation';
65+
$post->title_nl = null;
66+
$post->getTranslation('title', 'nl', true); // returns 'Your first translation'
67+
$post->getTranslation('title', 'nl', false); // returns null
68+
```
69+
70+
Or you may use dedicated methods for this:
71+
```php
72+
$post->title_en = 'Your first translation';
73+
$post->title_nl = null;
74+
$post->getTranslationWithFallback('title', 'nl'); // returns 'Your first translation'
75+
$post->getTranslationWithoutFallback('title', 'nl'); // returns null
76+
```
77+
5978
### Setting translations
6079

6180
To set the translation for the current locale you may use the attribute you have defined in the `translatable` property. Or you could pass it immediately when creating a model:

src/UnderscoreTranslatable.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,31 @@ public function getTranslatableAttributeName(string $key, ?string $locale = null
1717
return $key . '_' . ($locale ?? App::getLocale());
1818
}
1919

20-
public function getTranslation(string $key, ?string $locale = null): mixed
20+
public function getTranslation(string $key, ?string $locale = null, bool $useFallbackLocale = false): mixed
2121
{
2222
$value = $this->{$this->getTranslatableAttributeName($key, $locale)};
2323

2424
if ($this->hasGetMutator($key)) {
25-
return $this->mutateAttribute($key, $value);
25+
$value = $this->mutateAttribute($key, $value);
26+
}
27+
28+
if (empty($value) && $useFallbackLocale) {
29+
$value = $this->getTranslation($key, config('app.fallback_locale', $locale), false);
2630
}
2731

2832
return $value;
2933
}
3034

35+
public function getTranslationWithFallback(string $key, ?string $locale = null): mixed
36+
{
37+
return $this->getTranslation($key, $locale, true);
38+
}
39+
40+
public function getTranslationWithoutFallback(string $key, ?string $locale = null): mixed
41+
{
42+
return $this->getTranslation($key, $locale, false);
43+
}
44+
3145
public function setTranslation(string $key, string $locale, mixed $value): self
3246
{
3347
if ($this->hasSetMutator($key)) {

tests/UnderscoreTranslatableTest.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function it_wont_interfere_when_setting_non_translatable_attributes()
4444
}
4545

4646
/** @test */
47-
public function it_can_get_a_translatable_attribute_using_a_method()
47+
public function it_can_get_a_translation()
4848
{
4949
$post = new Post();
5050
$post->title_en = 'Test en';
@@ -56,7 +56,67 @@ public function it_can_get_a_translatable_attribute_using_a_method()
5656
}
5757

5858
/** @test */
59-
public function it_can_get_a_translatable_attribute_using_a_property()
59+
public function it_can_get_a_translation_using_a_fallback()
60+
{
61+
$post = new Post();
62+
$post->title_en = 'Test en';
63+
$post->title_nl = 'Test nl';
64+
65+
$this->assertEquals('Test nl', $post->getTranslation('title', 'nl', true));
66+
$this->assertEquals('Test en', $post->getTranslation('title', 'en', true));
67+
$this->assertEquals('Test en', $post->getTranslation('title', 'fr', true));
68+
}
69+
70+
/** @test */
71+
public function it_can_get_a_translation_without_using_a_fallback()
72+
{
73+
$post = new Post();
74+
$post->title_en = 'Test en';
75+
$post->title_nl = 'Test nl';
76+
77+
$this->assertEquals('Test nl', $post->getTranslation('title', 'nl', false));
78+
$this->assertEquals('Test en', $post->getTranslation('title', 'en', false));
79+
$this->assertNull($post->getTranslation('title', 'fr', false));
80+
}
81+
82+
/** @test */
83+
public function it_can_get_a_translation_with_a_fallback()
84+
{
85+
$post = new Post();
86+
$post->title_en = 'Test en';
87+
$post->title_nl = 'Test nl';
88+
89+
$this->assertEquals('Test nl', $post->getTranslationWithFallback('title', 'nl'));
90+
$this->assertEquals('Test en', $post->getTranslationWithFallback('title', 'en'));
91+
$this->assertEquals('Test en', $post->getTranslationWithFallback('title', 'fr'));
92+
}
93+
94+
/** @test */
95+
public function it_can_get_a_translation_without_a_fallback()
96+
{
97+
$post = new Post();
98+
$post->title_en = 'Test en';
99+
$post->title_nl = 'Test nl';
100+
101+
$this->assertEquals('Test nl', $post->getTranslationWithoutFallback('title', 'nl'));
102+
$this->assertEquals('Test en', $post->getTranslationWithoutFallback('title', 'en'));
103+
$this->assertNull($post->getTranslationWithoutFallback('title', 'fr'));
104+
}
105+
106+
/** @test */
107+
public function it_can_get_a_translatable_attribute_using_a_method_with_a_fallback()
108+
{
109+
$post = new Post();
110+
$post->title_en = 'Test en';
111+
$post->title_nl = null;
112+
113+
$this->assertEquals('Test en', $post->getTranslation('title', 'nl', true));
114+
$this->assertEquals('Test en', $post->getTranslation('title', 'en', true));
115+
$this->assertEquals('Test en', $post->getTranslation('title', 'fr', true));
116+
}
117+
118+
/** @test */
119+
public function it_can_get_a_translation_using_a_property()
60120
{
61121
$post = new Post();
62122
$post->title_en = 'Test en';
@@ -73,7 +133,7 @@ public function it_can_get_a_translatable_attribute_using_a_property()
73133
}
74134

75135
/** @test */
76-
public function it_can_get_a_translatable_attribute_using_an_accessor()
136+
public function it_can_get_a_translation_using_an_accessor()
77137
{
78138
$post = new Post();
79139
$post->field_with_accessor_en = 'Test en';

0 commit comments

Comments
 (0)