Skip to content

Commit

Permalink
Database: cache group translations (#244)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Dixon <[email protected]>
  • Loading branch information
levu42 and joedixon authored Aug 30, 2022
1 parent 943068a commit f5815bc
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Database extends Translation implements DriverInterface

protected $scanner;

protected array $groupTranslationCache = [];

protected array $languageCache = [];

public function __construct($sourceLanguage, $scanner)
Expand Down Expand Up @@ -184,18 +186,32 @@ public function getSingleTranslationsFor($language)
*/
public function getGroupTranslationsFor($language)
{
$translations = $this->getLanguage($language)
if (isset($this->groupTranslationCache[$language])) {
return $this->groupTranslationCache[$language];
}

$languageModel = $this->getLanguage($language);

if (is_null($languageModel)) {
return collect();
}

$translations = $languageModel
->translations()
->whereNotNull('group')
->where('group', 'not like', '%single')
->get()
->groupBy('group');

return $translations->map(function ($translations) {
$result = $translations->map(function ($translations) {
return $translations->mapWithKeys(function ($translation) {
return [$translation->key => $translation->value];
});
});

$this->groupTranslationCache[$language] = $result;

return $result;
}

/**
Expand Down

0 comments on commit f5815bc

Please sign in to comment.