Feature: Localization Restructuring to follow a stricter pattern
This commit is contained in:
@@ -8,7 +8,7 @@ use Lunar\Models\Language;
|
||||
|
||||
/**
|
||||
* Cached read layer over Lunar's `languages` table — the single source both
|
||||
* Modules\Core\Localization\LocaleMiddleware (request-time locale resolution) and
|
||||
* Modules\Core\Localization\Middleware\LocaleMiddleware (request-time locale resolution) and
|
||||
* any other locale-aware code (e.g. Modules\Core\Product\Services\ProductService) read
|
||||
* from, so the language list is fetched once per cache lifetime rather than once
|
||||
* per caller. Cached forever, invalidated via forget() by
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Services;
|
||||
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Spatie\TranslationLoader\LanguageLine;
|
||||
|
||||
class TranslationReader
|
||||
{
|
||||
private const DEFAULT_GROUP = 'storefront';
|
||||
|
||||
/**
|
||||
* All labels in a group for the given (or current) locale, keyed by their
|
||||
* dot-notation key — e.g. ['nav.cart' => 'Cart', 'nav.home' => 'Home'].
|
||||
* Backed by LanguageLine's own forever-cache, so this is a cache hit after
|
||||
* the first call for a given group+locale.
|
||||
*/
|
||||
public function group(string $group = self::DEFAULT_GROUP, ?string $locale = null): array
|
||||
{
|
||||
return LanguageLine::getTranslationsForGroup($locale ?? App::getLocale(), $group);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Modules\Core\Localization\Events\TranslationCreated;
|
||||
use Modules\Core\Localization\Events\TranslationDeleted;
|
||||
use Modules\Core\Localization\Events\TranslationUpdated;
|
||||
use Spatie\TranslationLoader\LanguageLine;
|
||||
|
||||
class TranslationService
|
||||
{
|
||||
public function create(string $group, string $key, array $text): LanguageLine
|
||||
{
|
||||
$languageLine = LanguageLine::create([
|
||||
'group' => $group,
|
||||
'key' => $key,
|
||||
'text' => $text,
|
||||
]);
|
||||
|
||||
Event::dispatch(new TranslationCreated($languageLine));
|
||||
|
||||
return $languageLine;
|
||||
}
|
||||
|
||||
public function update(LanguageLine $languageLine, string $group, string $key, array $text): LanguageLine
|
||||
{
|
||||
// Callers (e.g. Filament's EditRecord) may hand us a model instance
|
||||
// already filled with the new form values in memory — refresh from the
|
||||
// database first so $old reflects what's actually persisted, not what's
|
||||
// about to be written.
|
||||
$persisted = $languageLine->fresh();
|
||||
|
||||
$old = [
|
||||
'group' => $persisted->group,
|
||||
'key' => $persisted->key,
|
||||
'text' => $persisted->text,
|
||||
];
|
||||
|
||||
$languageLine->update([
|
||||
'group' => $group,
|
||||
'key' => $key,
|
||||
'text' => $text,
|
||||
]);
|
||||
|
||||
Event::dispatch(new TranslationUpdated($languageLine, $old));
|
||||
|
||||
return $languageLine;
|
||||
}
|
||||
|
||||
public function delete(LanguageLine $languageLine): void
|
||||
{
|
||||
$languageLine->delete();
|
||||
|
||||
Event::dispatch(new TranslationDeleted($languageLine));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user