Skip to content

Commit f5815bc

Browse files
levu42joedixon
andauthored
Database: cache group translations (#244)
Co-authored-by: Joe Dixon <[email protected]>
1 parent 943068a commit f5815bc

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Drivers/Database.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Database extends Translation implements DriverInterface
1414

1515
protected $scanner;
1616

17+
protected array $groupTranslationCache = [];
18+
1719
protected array $languageCache = [];
1820

1921
public function __construct($sourceLanguage, $scanner)
@@ -184,18 +186,32 @@ public function getSingleTranslationsFor($language)
184186
*/
185187
public function getGroupTranslationsFor($language)
186188
{
187-
$translations = $this->getLanguage($language)
189+
if (isset($this->groupTranslationCache[$language])) {
190+
return $this->groupTranslationCache[$language];
191+
}
192+
193+
$languageModel = $this->getLanguage($language);
194+
195+
if (is_null($languageModel)) {
196+
return collect();
197+
}
198+
199+
$translations = $languageModel
188200
->translations()
189201
->whereNotNull('group')
190202
->where('group', 'not like', '%single')
191203
->get()
192204
->groupBy('group');
193205

194-
return $translations->map(function ($translations) {
206+
$result = $translations->map(function ($translations) {
195207
return $translations->mapWithKeys(function ($translation) {
196208
return [$translation->key => $translation->value];
197209
});
198210
});
211+
212+
$this->groupTranslationCache[$language] = $result;
213+
214+
return $result;
199215
}
200216

201217
/**

0 commit comments

Comments
 (0)