File tree Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,36 @@ private function isTranslatableAttribute(string $key): bool
97
97
*/
98
98
private function getCachedTranslations (): Collection
99
99
{
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 ();
101
131
}
102
132
}
Original file line number Diff line number Diff line change @@ -101,6 +101,32 @@ public function testItTranslatesOnlyTranslatableAttributes()
101
101
$ this ->assertEquals ('some text goes here ' , $ post ->body );
102
102
}
103
103
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
+
104
130
public function testItTranslatesAllAttributesIfNoTranslatableAttributesAreDefined ()
105
131
{
106
132
$ post = new class () extends Post {
You can’t perform that action at this time.
0 commit comments