Skip to content

Commit 53a9e7c

Browse files
committed
bugfix: clear cache after switching the locale
1 parent c39028e commit 53a9e7c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Traits/TranslatableTrait.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ private function isTranslatableAttribute(string $key): bool
9797
*/
9898
private function getCachedTranslations(): Collection
9999
{
100-
return $this->cachedTranslationModels = $this->cachedTranslationModels ?: $this->translation()->locale(app()->getLocale())->get();
100+
// Check if translations are already cached for the current locale
101+
if ($this->cachedTranslationModels instanceof Collection && $this->hasTranslationsForCurrentLocale()) {
102+
return $this->cachedTranslationModels;
103+
}
104+
105+
// If not cached or not found for current locale, fetch and cache translations
106+
return $this->cachedTranslationModels = $this->fetchAndCacheTranslations(app()->getLocale());
107+
}
108+
109+
/**
110+
* Determines whether there are any translations cached for the current locale.
111+
*
112+
* @return bool `true` if there are cached translations for the current locale, `false` otherwise
113+
*/
114+
private function hasTranslationsForCurrentLocale(): bool
115+
{
116+
$locale = app()->getLocale();
117+
118+
return null !== $this->cachedTranslationModels->firstWhere('locale', $locale);
119+
}
120+
121+
/**
122+
* Fetches and caches the translation models for the given locale.
123+
*
124+
* @param string $locale the locale to fetch translations for
125+
*
126+
* @return Collection the collection of translation models
127+
*/
128+
private function fetchAndCacheTranslations(string $locale): Collection
129+
{
130+
return $this->translation()->locale($locale)->get();
101131
}
102132
}

tests/TranslationModelTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ public function testItTranslatesOnlyTranslatableAttributes()
101101
$this->assertEquals('some text goes here', $post->body);
102102
}
103103

104+
public function testItTranslatesAttributesImmediatelyAfterSwitchingLocale()
105+
{
106+
$post = new Post();
107+
$post->id = 1;
108+
$post->name = 'original text goes here';
109+
110+
$post->translation()->saveMany([
111+
new Translation([
112+
'locale' => 'fr_FR',
113+
'key' => 'name',
114+
'value' => 'un texte va ici',
115+
]),
116+
new Translation([
117+
'locale' => 'es_ES',
118+
'key' => 'name',
119+
'value' => 'aqui va un texto',
120+
]),
121+
]);
122+
123+
app()->setLocale('fr_FR');
124+
$this->assertEquals('un texte va ici', $post->name);
125+
126+
app()->setLocale('es_ES');
127+
$this->assertEquals('aqui va un texto', $post->name);
128+
}
129+
104130
public function testItTranslatesAllAttributesIfNoTranslatableAttributesAreDefined()
105131
{
106132
$post = new class() extends Post {

0 commit comments

Comments
 (0)