Fix: Correcting Language Line fallback resolver
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Localization\Models;
|
||||
|
||||
use Modules\Core\Localization\Services\LanguageCache;
|
||||
use Spatie\TranslationLoader\LanguageLine as BaseLanguageLine;
|
||||
|
||||
/**
|
||||
* Overrides the base package's locale fallback (config('app.fallback_locale'), a
|
||||
* static .env value) with the store's actual default language — Lunar's
|
||||
* `languages.default` flag, the same source LocaleMiddleware/LanguageCache already
|
||||
* treat as the single source of truth for "this store's default language".
|
||||
*
|
||||
* Without this, changing the default language via the Filament Languages resource
|
||||
* has no effect on which locale an untranslated storefront label falls back to —
|
||||
* two disconnected "default locale" concepts silently drifting apart. Swapped in
|
||||
* via config('translation-loader.model') (see LocalizationServiceProvider), the
|
||||
* package's own documented extension point for this.
|
||||
*/
|
||||
class LanguageLine extends BaseLanguageLine
|
||||
{
|
||||
public function getTranslation(string $locale): ?string
|
||||
{
|
||||
if (isset($this->text[$locale])) {
|
||||
return $this->text[$locale];
|
||||
}
|
||||
|
||||
$fallback = app(LanguageCache::class)->defaultLocale();
|
||||
|
||||
return $fallback !== null ? ($this->text[$fallback] ?? null) : null;
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,21 @@ use Modules\Core\Localization\Listeners\FlushTranslationCache;
|
||||
use Modules\Core\Localization\Listeners\LogTranslationActivity;
|
||||
use Modules\Core\Localization\Listeners\MigrateTranslationsForRenamedLanguage;
|
||||
use Modules\Core\Localization\Middleware\LocaleMiddleware;
|
||||
use Modules\Core\Localization\Models\LanguageLine;
|
||||
use Modules\Core\Localization\Observers\LanguageCacheObserver;
|
||||
|
||||
class LocalizationServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
// Must run before Spatie\TranslationLoader\TranslationServiceProvider's
|
||||
// register() merges its own config defaults - mergeConfigFrom() only fills
|
||||
// in keys not already set, so setting this here (regardless of provider
|
||||
// boot order) makes it win over the package's default
|
||||
// Spatie\TranslationLoader\LanguageLine::class.
|
||||
config(['translation-loader.model' => LanguageLine::class]);
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->app['router']->aliasMiddleware('locale', LocaleMiddleware::class);
|
||||
|
||||
Reference in New Issue
Block a user