From 0edf7b156d76d32371d8dbe9d3d67b15b6e2d9db Mon Sep 17 00:00:00 2001 From: Konstantinos Arvanitakis Date: Wed, 26 Aug 2026 23:52:33 +0300 Subject: [PATCH] Feature: Updating Product Service to handle multiple locales, fetching the correct locale, or, the default locale as fallback --- src/Catalog/ProductOptionTypeInterface.php | 34 +++++++++++ src/Catalog/ProductService.php | 22 +++++-- .../Listeners/FlushLanguageCache.php | 6 +- src/Localization/LocaleMiddleware.php | 30 +--------- src/Localization/Services/LanguageCache.php | 58 +++++++++++++++++++ 5 files changed, 115 insertions(+), 35 deletions(-) create mode 100644 src/Catalog/ProductOptionTypeInterface.php create mode 100644 src/Localization/Services/LanguageCache.php diff --git a/src/Catalog/ProductOptionTypeInterface.php b/src/Catalog/ProductOptionTypeInterface.php new file mode 100644 index 0000000..248459d --- /dev/null +++ b/src/Catalog/ProductOptionTypeInterface.php @@ -0,0 +1,34 @@ + + */ + public function getMetaForm(): array; +} diff --git a/src/Catalog/ProductService.php b/src/Catalog/ProductService.php index 6c7631b..f464d31 100644 --- a/src/Catalog/ProductService.php +++ b/src/Catalog/ProductService.php @@ -6,7 +6,7 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use Lunar\Models\Product; -use Modules\Core\Localization\LocaleMiddleware; +use Modules\Core\Localization\Services\LanguageCache; /** * Storefront product listing/filtering AND single-product lookup, all reading directly @@ -19,6 +19,8 @@ use Modules\Core\Localization\LocaleMiddleware; */ class ProductService { + public function __construct(private readonly LanguageCache $languages) {} + /** * @return array{data: array, meta: array} */ @@ -80,10 +82,14 @@ class ProductService /** * Resolves the current-locale `name`/`description` from the indexer's * per-locale `name_{locale}`/`description_{locale}` fields, falling back to - * the store's default language (Language::default, see - * LocaleMiddleware::defaultLocale()) when the current locale has no - * translation - e.g. a product with no English copy yet still shows its - * Greek name/description on /en/ rather than rendering blank. + * the store's default language (LanguageCache::defaultLocale()) when the + * current locale has no translation - e.g. a product with no English copy + * yet still shows its Greek name/description on /en/ rather than rendering + * blank. The raw per-locale keys are then stripped - every configured + * locale's translation is indexed in Meilisearch (Lunar's base indexer + * explodes every TranslatedText attribute into name_{locale}/ + * description_{locale} per store language), but once resolved into `name`/ + * `description`, callers only ever need the one that matched. * * Deliberately not config('app.locale') - App::setLocale() overwrites that * config value on every request, so by request time it's just whatever the @@ -92,11 +98,15 @@ class ProductService private function withLocalizedFields(array $product): array { $locale = App::getLocale(); - $fallbackLocale = LocaleMiddleware::defaultLocale(); + $fallbackLocale = $this->languages->defaultLocale(); $product['name'] = $product['name_'.$locale] ?? $product['name_'.$fallbackLocale] ?? null; $product['description'] = $product['description_'.$locale] ?? $product['description_'.$fallbackLocale] ?? null; + foreach ($this->languages->availableLocales() as $availableLocale) { + unset($product['name_'.$availableLocale], $product['description_'.$availableLocale]); + } + return $product; } diff --git a/src/Localization/Listeners/FlushLanguageCache.php b/src/Localization/Listeners/FlushLanguageCache.php index 575811c..e79016c 100644 --- a/src/Localization/Listeners/FlushLanguageCache.php +++ b/src/Localization/Listeners/FlushLanguageCache.php @@ -5,12 +5,14 @@ namespace Modules\Core\Localization\Listeners; use Modules\Core\Localization\Events\LanguageCreated; use Modules\Core\Localization\Events\LanguageDeleted; use Modules\Core\Localization\Events\LanguageUpdated; -use Modules\Core\Localization\LocaleMiddleware; +use Modules\Core\Localization\Services\LanguageCache; class FlushLanguageCache { + public function __construct(private readonly LanguageCache $languages) {} + public function handle(LanguageCreated|LanguageUpdated|LanguageDeleted $event): void { - LocaleMiddleware::forgetLanguagesCache(); + $this->languages->forget(); } } diff --git a/src/Localization/LocaleMiddleware.php b/src/Localization/LocaleMiddleware.php index 5cd694d..edb1d58 100644 --- a/src/Localization/LocaleMiddleware.php +++ b/src/Localization/LocaleMiddleware.php @@ -6,19 +6,19 @@ use Closure; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; -use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\View; use Lunar\Models\Language; +use Modules\Core\Localization\Services\LanguageCache; use Symfony\Component\HttpFoundation\Response; class LocaleMiddleware { - private const CACHE_KEY = 'core.localization.languages'; + public function __construct(private readonly LanguageCache $languages) {} public function handle(Request $request, Closure $next): Response { - $languages = $this->availableLanguages(); + $languages = $this->languages->all(); if ($languages->isEmpty()) { return $next($request); @@ -45,22 +45,6 @@ class LocaleMiddleware return $next($request); } - public static function forgetLanguagesCache(): void - { - Cache::forget(self::CACHE_KEY); - } - - /** - * The store's default language code (e.g. 'el') - the fixed fallback other - * locale-aware code (Modules\Core\Catalog\ProductService) should use, as - * opposed to config('app.locale') which App::setLocale() mutates per - * request and so can't serve as a stable fallback. - */ - public static function defaultLocale(): ?string - { - return (new self)->availableLanguages()->firstWhere('default', true)?->code; - } - /** * Shares the current locale and every OTHER available locale (each with its * own URL for the current page) with all views, so the header language @@ -120,12 +104,4 @@ class LocaleMiddleware return $languages->firstWhere('default', true)?->code ?? $languages->first()->code; } - - private function availableLanguages(): Collection - { - return Cache::rememberForever( - self::CACHE_KEY, - fn () => Language::query()->get(['id', 'code', 'name', 'default']), - ); - } } diff --git a/src/Localization/Services/LanguageCache.php b/src/Localization/Services/LanguageCache.php new file mode 100644 index 0000000..62f9b3e --- /dev/null +++ b/src/Localization/Services/LanguageCache.php @@ -0,0 +1,58 @@ + Language::query()->get(['id', 'code', 'name', 'default']), + ); + } + + /** + * The store's default language code (e.g. 'el') - the fixed fallback other + * locale-aware code should use, as opposed to config('app.locale') which + * App::setLocale() mutates per request and so can't serve as a stable + * fallback. + */ + public function defaultLocale(): ?string + { + return $this->all()->firstWhere('default', true)?->code; + } + + /** + * Every configured store locale code (e.g. ['el', 'en']) - for code that needs + * to enumerate all locales a TranslatedText attribute was indexed under (see + * Modules\Core\Catalog\ProductService::withLocalizedFields()), rather than + * hardcoding locale codes. + * + * @return array + */ + public function availableLocales(): array + { + return $this->all()->pluck('code')->all(); + } + + public function forget(): void + { + Cache::forget(self::CACHE_KEY); + } +}