From fffce7795086e7a585977fb5810e4e772153c95d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 6 Apr 2024 15:05:13 +0300 Subject: [PATCH] Fixing a caching trouble # Conflicts: # src/Concerns/Localized.php # src/Services/Locales.php # tests/Unit/Facades/Locales/InfoTest.php --- src/Concerns/Application.php | 28 +++++++++++++++++++ src/Concerns/Localized.php | 7 ----- src/Concerns/Registry.php | 6 +--- src/Services/Locales.php | 10 ++++--- src/Services/RawLocales.php | 10 ++++--- tests/Unit/Facades/Locales/GetCurrentTest.php | 14 ++++++++++ tests/Unit/Facades/Locales/GetDefaultTest.php | 14 ++++++++++ .../Unit/Facades/Locales/GetFallbackTest.php | 15 ++++++++++ tests/Unit/Facades/Locales/GetTest.php | 14 ++++++++++ tests/Unit/Facades/Locales/InfoTest.php | 8 +++--- 10 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 src/Concerns/Application.php diff --git a/src/Concerns/Application.php b/src/Concerns/Application.php new file mode 100644 index 0000000..738b2f2 --- /dev/null +++ b/src/Concerns/Application.php @@ -0,0 +1,28 @@ + + * @copyright 2024 Laravel Lang Team + * @license MIT + * + * @see https://laravel-lang.com + */ + +declare(strict_types=1); + +namespace LaravelLang\Locales\Concerns; + +use LaravelLang\LocaleList\Locale; + +trait Application +{ + protected function appLocale(): string + { + return config('app.locale') ?: Locale::English->value; + } +} diff --git a/src/Concerns/Localized.php b/src/Concerns/Localized.php index 81b28da..00001f4 100644 --- a/src/Concerns/Localized.php +++ b/src/Concerns/Localized.php @@ -55,11 +55,4 @@ public function localizedCurrencies(bool $withCurrencies): ?NativeData CurrencyNames::get($this->appLocale())->all() )); } - - protected function appLocale(): string - { - return $this->fromAlias( - $this->raw->getCurrent() - ); - } } diff --git a/src/Concerns/Registry.php b/src/Concerns/Registry.php index c965ae6..2c0a596 100644 --- a/src/Concerns/Registry.php +++ b/src/Concerns/Registry.php @@ -28,11 +28,7 @@ protected function registry(array|string $key, Closure $callback): mixed { $key = $this->registryKey($key); - if (array_key_exists($key, $this->registry)) { - return $this->registry[$key]; - } - - return $this->registry[$key] = $callback(); + return $this->registry[$key] ??= $callback(); } protected function registryKey(array|string $key): string diff --git a/src/Services/Locales.php b/src/Services/Locales.php index c56b530..6bf6637 100644 --- a/src/Services/Locales.php +++ b/src/Services/Locales.php @@ -20,6 +20,7 @@ use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Locales\Concerns\Aliases; +use LaravelLang\Locales\Concerns\Application; use LaravelLang\Locales\Concerns\Localized; use LaravelLang\Locales\Concerns\Mapping; use LaravelLang\Locales\Concerns\Registry; @@ -28,6 +29,7 @@ class Locales { use Aliases; + use Application; use Localized; use Mapping; use Registry; @@ -94,7 +96,7 @@ public function isProtected(Locale|string|null $locale): bool public function get(mixed $locale, bool $withCountry = true, bool $withCurrency = true): LocaleData { return $this->registry( - [__METHOD__, $locale], + [__METHOD__, $locale, $this->appLocale()], fn () => $this->map($this->raw->get($locale), $withCountry, $withCurrency) ); } @@ -102,7 +104,7 @@ public function get(mixed $locale, bool $withCountry = true, bool $withCurrency public function info(mixed $locale, bool $withCountry = true, bool $withCurrency = true): LocaleData { return $this->registry( - [__METHOD__, $locale, $this->raw->getCurrent()], + [__METHOD__, $locale, $this->appLocale()], fn () => $this->map($this->raw->info($locale), $withCountry, $withCurrency) ); } @@ -115,7 +117,7 @@ public function getCurrent(bool $withCountry = true, bool $withCurrency = true): public function getDefault(bool $withCountry = true, bool $withCurrency = true): LocaleData { return $this->registry( - __METHOD__, + [__METHOD__, $this->appLocale()], fn () => $this->map($this->raw->getDefault(), $withCountry, $withCurrency) ); } @@ -123,7 +125,7 @@ public function getDefault(bool $withCountry = true, bool $withCurrency = true): public function getFallback(bool $withCountry = true, bool $withCurrency = true): LocaleData { return $this->registry( - __METHOD__, + [__METHOD__, $this->appLocale()], fn () => $this->map($this->raw->getFallback(), $withCountry, $withCurrency) ); } diff --git a/src/Services/RawLocales.php b/src/Services/RawLocales.php index dd2151f..bb8c986 100644 --- a/src/Services/RawLocales.php +++ b/src/Services/RawLocales.php @@ -21,12 +21,14 @@ use Illuminate\Support\Collection; use LaravelLang\LocaleList\Locale; use LaravelLang\Locales\Concerns\Aliases; +use LaravelLang\Locales\Concerns\Application; use LaravelLang\Locales\Concerns\Pathable; use LaravelLang\Locales\Concerns\Registry; class RawLocales { use Aliases; + use Application; use Pathable; use Registry; @@ -110,7 +112,7 @@ public function isProtected(Locale|string|null $locale): bool public function get(mixed $locale): string { - return $this->registry([__METHOD__, $locale], function () use ($locale) { + return $this->registry([__METHOD__, $locale, $this->appLocale()], function () use ($locale) { $locale = Resolver::fromMixed($locale); if ($this->isInstalled($locale)) { @@ -123,7 +125,7 @@ public function get(mixed $locale): string public function getDefault(): string { - return $this->registry(__METHOD__, function () { + return $this->registry([__METHOD__, $this->appLocale()], function () { $locale = config('app.locale'); return $this->toAlias( @@ -139,7 +141,7 @@ public function getCurrent(): string public function getFallback(): string { - return $this->registry(__METHOD__, function () { + return $this->registry([__METHOD__, $this->appLocale()], function () { $locale = config('app.fallback_locale'); if ($this->isAvailable($locale)) { @@ -156,7 +158,7 @@ public function getFallback(): string public function info(mixed $locale): string { - return $this->registry([__METHOD__, $locale], function () use ($locale) { + return $this->registry([__METHOD__, $locale, $this->appLocale()], function () use ($locale) { $locale = Resolver::fromMixed($locale); if ($this->isAvailable($locale)) { diff --git a/tests/Unit/Facades/Locales/GetCurrentTest.php b/tests/Unit/Facades/Locales/GetCurrentTest.php index 1d7829f..a34bedd 100644 --- a/tests/Unit/Facades/Locales/GetCurrentTest.php +++ b/tests/Unit/Facades/Locales/GetCurrentTest.php @@ -82,3 +82,17 @@ ->expect(fn () => Locales::getCurrent(false, false)) ->country->toBeNull() ->currency->toBeNull(); + +it('returns the correct localized name if the non-default locale is set', function () { + createLocales(Locale::German, Locale::English); + + Locales::set(Locale::English); + + expect(Locales::getCurrent()) + ->localized->toBeString()->toBe('English'); + + Locales::set(Locale::German); + + expect(Locales::getCurrent()) + ->localized->toBeString()->toBe('Deutsch'); +}); diff --git a/tests/Unit/Facades/Locales/GetDefaultTest.php b/tests/Unit/Facades/Locales/GetDefaultTest.php index f871819..30424ae 100644 --- a/tests/Unit/Facades/Locales/GetDefaultTest.php +++ b/tests/Unit/Facades/Locales/GetDefaultTest.php @@ -82,3 +82,17 @@ ->expect(fn () => Locales::getDefault(false, false)) ->country->toBeNull() ->currency->toBeNull(); + +it('returns the correct localized name if the non-default locale is set', function () { + createLocales(Locale::German, Locale::English); + + Locales::set(Locale::English); + + expect(Locales::getDefault()) + ->localized->toBeString()->toBe('English'); + + Locales::set(Locale::German); + + expect(Locales::getDefault()) + ->localized->toBeString()->toBe('Deutsch'); +}); diff --git a/tests/Unit/Facades/Locales/GetFallbackTest.php b/tests/Unit/Facades/Locales/GetFallbackTest.php index b897412..ef1e580 100644 --- a/tests/Unit/Facades/Locales/GetFallbackTest.php +++ b/tests/Unit/Facades/Locales/GetFallbackTest.php @@ -89,3 +89,18 @@ ->expect(fn () => Locales::getFallback(false, false)) ->country->toBeNull() ->currency->toBeNull(); + +it('returns the correct localized name if the non-default locale is set', function () { + createLocales(Locale::German, Locale::English); + setLocales(fallback: Locale::German); + + Locales::set(Locale::English); + + expect(Locales::getFallback()) + ->localized->toBeString()->toBe('German'); + + Locales::set(Locale::German); + + expect(Locales::getFallback()) + ->localized->toBeString()->toBe('Deutsch'); +}); diff --git a/tests/Unit/Facades/Locales/GetTest.php b/tests/Unit/Facades/Locales/GetTest.php index f5a8327..301d085 100644 --- a/tests/Unit/Facades/Locales/GetTest.php +++ b/tests/Unit/Facades/Locales/GetTest.php @@ -249,3 +249,17 @@ function () { ->expect(fn () => Locales::get(Locale::Vietnamese, false, false)) ->country->toBeNull() ->currency->toBeNull(); + +it('returns the correct localized name if the non-default locale is set', function () { + createLocales(Locale::German, Locale::English); + + Locales::set(Locale::English); + + expect(Locales::get(Locale::German)) + ->localized->toBeString()->toBe('German'); + + Locales::set(Locale::German); + + expect(Locales::get(Locale::German)) + ->localized->toBeString()->toBe('Deutsch'); +}); diff --git a/tests/Unit/Facades/Locales/InfoTest.php b/tests/Unit/Facades/Locales/InfoTest.php index 92b3c9c..b1233ae 100644 --- a/tests/Unit/Facades/Locales/InfoTest.php +++ b/tests/Unit/Facades/Locales/InfoTest.php @@ -171,13 +171,13 @@ it('returns the correct localized name if the non-default locale is set', function () { createLocales(Locale::German, Locale::English); - Locales::set(Locale::English->value); + Locales::set(Locale::English); - expect(Locales::info(Locale::German->value)) + expect(Locales::info(Locale::German)) ->localized->toBeString()->toBe('German'); - Locales::set(Locale::German->value); + Locales::set(Locale::German); - expect(Locales::info(Locale::German->value)) + expect(Locales::info(Locale::German)) ->localized->toBeString()->toBe('Deutsch'); });