Skip to content

Commit

Permalink
Merge pull request #7 from MortenDHansen/alternate-approach
Browse files Browse the repository at this point in the history
Alternate approach
  • Loading branch information
MortenDHansen authored Mar 1, 2022
2 parents 5becb41 + 26c7335 commit 7582691
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 50 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ The key will be created in the table for the current locale. So, if you have vis
will update the table whenever the key is encountered and found missing.

If you have an en.json that sets `{"animal":"bird"}`, the result of the above will be 'bird'. You may now override that
in the database.
in the database by simply updating the record with:

```php
$record = DatabaseLangItem::where('locale', 'en')->where('group', '*')->where('key', 'animal')->first();
$record->value = 'giraffe';
$record->save();
```

now result of `__('animal')` is giraffe.

## Installation

Expand All @@ -55,5 +63,4 @@ Now you need to replace the laravel TranslationServiceProvider with the service
## Remarks

- Of course getting, and writing language keys whenever they appear is quite a heavy load of you have many of them.
Therefore the package caches the keys. When cache is cold, the pages may be quite slow for the first couple of
requests.
Therefore the package caches the keys. When cache is cold, the pages may be quite slow.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
}
},
"extra": {
"aliases": {
"DbTrans": "MortenDHansen\\LaravelDatabaseTranslations\\Facades\\DatabaseTranslationsFacade"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
Expand Down
4 changes: 3 additions & 1 deletion config/translations-database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
| Loaded translations from database are cached by the default cache selected
|
*/
'cache-prefix' => 'laravel-db-translations'
'cache-prefix' => 'laravel-db-translations',

'cache-driver' => config('cache.default')
];
1 change: 0 additions & 1 deletion src/DatabaseTranslationsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace MortenDHansen\LaravelDatabaseTranslations;

use Illuminate\Contracts\Translation\Loader;
use MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans;

class DatabaseTranslationsLoader implements Loader
{
Expand Down
1 change: 0 additions & 1 deletion src/DatabaseTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Translation\FileLoader;
use Illuminate\Translation\TranslationServiceProvider;
use MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans;

class DatabaseTranslationsServiceProvider extends TranslationServiceProvider
{
Expand Down
2 changes: 0 additions & 2 deletions src/DatabaseTranslationsTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace MortenDHansen\LaravelDatabaseTranslations;

use MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans;

class DatabaseTranslationsTranslator extends \Illuminate\Translation\Translator
{

Expand Down
29 changes: 16 additions & 13 deletions src/Facades/DbTrans.php → src/DbTrans.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?php

namespace MortenDHansen\LaravelDatabaseTranslations\Facades;
namespace MortenDHansen\LaravelDatabaseTranslations;

use Carbon\Carbon;
use MortenDHansen\LaravelDatabaseTranslations\Models\DatabaseLangItem;

use function config;

class DbTrans
{
public static function getDatabaseTranslations(string $group, string $locale): array
{
$cacheKey = app('dbtrans')->getCacheKey($group, $locale);
return cache()->remember($cacheKey, Carbon::now()->addDay(), function () use ($group, $locale) {
return DatabaseLangItem::where('locale', $locale)
->where('group', $group)
->get()
->mapWithKeys(function (DatabaseLangItem $langItem) {
$result = [$langItem->key => $langItem->value];
if (is_array(json_decode($langItem->value, true))) {
$result = [];
foreach (json_decode($langItem->value, true) as $subKey => $subValue) {
$result[$langItem->key . '.' . $subKey] = $subValue;
}
$cacheKey = DbTrans::getCacheKey($group, $locale);
return \Cache::driver(config('translations-database.cache-driver'))->remember($cacheKey,
Carbon::now()->addDay(), function () use ($group, $locale) {
return DatabaseLangItem::where('locale', $locale)
->where('group', $group)
->get()
->mapWithKeys(function (DatabaseLangItem $langItem) {
$result = [$langItem->key => $langItem->value];
if (is_array(json_decode($langItem->value, true))) {
$result = [];
foreach (json_decode($langItem->value, true) as $subKey => $subValue) {
$result[$langItem->key . '.' . $subKey] = $subValue;
}
}
return $result;
})->toArray();
Expand Down
12 changes: 0 additions & 12 deletions src/Facades/DatabaseTranslationsFacade.php

This file was deleted.

9 changes: 6 additions & 3 deletions src/Models/DatabaseLangItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace MortenDHansen\LaravelDatabaseTranslations\Models;

use Cache;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use MortenDHansen\LaravelDatabaseTranslations\database\Factories\DatabaseLangItemFactory;
use MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans;
use MortenDHansen\LaravelDatabaseTranslations\DbTrans;

class DatabaseLangItem extends \Illuminate\Database\Eloquent\Model
{
Expand All @@ -27,12 +28,14 @@ protected static function boot()
parent::boot();

static::created(function ($model) {
cache()->forget(DbTrans::getCacheKey($model->group, $model->locale));
Cache::driver(config('translations-database.cache-driver'))->forget(DbTrans::getCacheKey($model->group,
$model->locale));
DbTrans::getDatabaseTranslations($model->group, $model->locale);
});

static::updated(function ($model) {
cache()->forget(DbTrans::getCacheKey($model->group, $model->locale));
Cache::driver(config('translations-database.cache-driver'))->forget(DbTrans::getCacheKey($model->group,
$model->locale));
DbTrans::getDatabaseTranslations($model->group, $model->locale);
});
}
Expand Down
10 changes: 0 additions & 10 deletions tests/Feature/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,4 @@ public function databaseMayHaveTranslations()
$this->assertDatabaseCount('database_lang_items', 10);
}

/**
* @test
* @return void
*/
public function canUseFacade()
{
$facade = app('dbtrans');
$this->assertInstanceOf(\MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans::class, $facade);
}

}
22 changes: 21 additions & 1 deletion tests/Feature/DatabaseTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Validator;
use MortenDHansen\LaravelDatabaseTranslations\Facades\DbTrans;
use MortenDHansen\LaravelDatabaseTranslations\DbTrans;
use MortenDHansen\LaravelDatabaseTranslations\Models\DatabaseLangItem;

class DatabaseTranslationsTest extends \MortenDHansen\LaravelDatabaseTranslations\Tests\TestCase
Expand Down Expand Up @@ -58,6 +58,26 @@ public function databaseTranslationsOverrideFiles()
$this->assertEquals('blue', __('food.salad'));
}

/**
* @test
* @return void
*/
public function changesToOverrideTakesImmidiateEffect()
{
__('animal');
$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'locale' => 'en', 'key' => 'animal']);

$record = DatabaseLangItem::where('group', '*')->where('locale', 'en')->where('key', 'animal')->first();
$this->assertNull($record->value);

$record->value = 'bird';
$record->save();
$this->assertDatabaseHas('database_lang_items',
['group' => '*', 'locale' => 'en', 'key' => 'animal', 'value' => 'bird']);

$this->assertEquals('bird', __('animal'));
}

/**
* @test
* @return void
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function getEnvironmentSetUp($app)
'driver' => 'file',
'path' => __DIR__ . '/temp/cache'
]);

$app['path.lang'] = __DIR__ . '/lang';
$app['config']->set('translations-database.cache-prefix', 'laravel-db-translations');
}
Expand Down

0 comments on commit 7582691

Please sign in to comment.