Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache heater #9

Merged
merged 3 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Console/DatabaseTranslationsCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace MortenDHansen\LaravelDatabaseTranslations\Console;

use MortenDHansen\LaravelDatabaseTranslations\DbTrans;
use MortenDHansen\LaravelDatabaseTranslations\Models\DatabaseLangItem;

class DatabaseTranslationsCacheCommand extends \Illuminate\Console\Command
{

protected $signature = "dbtrans:cache-rebuild";

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purge and rebuild cache for database translations';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$groups = DatabaseLangItem::all('group')->unique('group')->pluck('group');
$locales = DatabaseLangItem::all('locale')->unique('locale')->pluck('locale');

// build cache keys

foreach ($groups as $group) {
foreach ($locales as $locale) {
\Cache::forget(DbTrans::getCacheKey($group, $locale));
DbTrans::getDatabaseTranslations($group, $locale);
}
}

return self::SUCCESS;
}

public function buildCacheKeys()
{
return $cacheKeys;
}
}
5 changes: 5 additions & 0 deletions src/DatabaseTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Translation\FileLoader;
use Illuminate\Translation\TranslationServiceProvider;
use MortenDHansen\LaravelDatabaseTranslations\Console\DatabaseTranslationsCacheCommand;

class DatabaseTranslationsServiceProvider extends TranslationServiceProvider
{
Expand Down Expand Up @@ -61,6 +62,10 @@ public function boot()
$this->publishes([
__DIR__ . '/../config/translations-database.php' => config_path('translations-database.php'),
]);

$this->commands([
DatabaseTranslationsCacheCommand::class
]);
}
}
}
6 changes: 5 additions & 1 deletion src/DatabaseTranslationsTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

// The key was parsed, item place will be null if key is ungrouped
if (!isset($item)) {
$item = $group;
$item = $group == '*' ? $key : $group;
$group = '*';
}

Expand All @@ -75,6 +75,10 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

public function createMissingKey($group, $item, $locale)
{
// That's not a key
if ($item === '*') {
return;
}
DbTrans::createLanguageItem($group, $item, $locale);
$this->loaded = [];
$this->loadedFromDb = [];
Expand Down
Loading