Skip to content

Commit

Permalink
Fixing a caching trouble
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Concerns/Localized.php
#	src/Services/Locales.php
#	tests/Unit/Facades/Locales/InfoTest.php
  • Loading branch information
andrey-helldar committed Apr 6, 2024
1 parent 39c8e52 commit fffce77
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/Concerns/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the "laravel-lang/locales" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <[email protected]>
* @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;
}
}
7 changes: 0 additions & 7 deletions src/Concerns/Localized.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
6 changes: 1 addition & 5 deletions src/Concerns/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/Services/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
class Locales
{
use Aliases;
use Application;
use Localized;
use Mapping;
use Registry;
Expand Down Expand Up @@ -94,15 +96,15 @@ 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)
);
}

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)
);
}
Expand All @@ -115,15 +117,15 @@ 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)
);
}

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)
);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Services/RawLocales.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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(
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Facades/Locales/GetCurrentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
14 changes: 14 additions & 0 deletions tests/Unit/Facades/Locales/GetDefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
15 changes: 15 additions & 0 deletions tests/Unit/Facades/Locales/GetFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
14 changes: 14 additions & 0 deletions tests/Unit/Facades/Locales/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
8 changes: 4 additions & 4 deletions tests/Unit/Facades/Locales/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit fffce77

Please sign in to comment.