Skip to content

Commit

Permalink
Fixed work with aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Oct 29, 2023
1 parent 51474ec commit 20e458c
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 71 deletions.
15 changes: 13 additions & 2 deletions src/Concerns/Pathable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use DragonCode\Support\Facades\Filesystem\Directory;
use DragonCode\Support\Facades\Filesystem\File;
use Illuminate\Support\Collection;

trait Pathable
{
Expand All @@ -32,9 +33,19 @@ protected function directoryDoesntExist(): bool
return ! $this->directoryExists();
}

protected function directories(): array
protected function directories(string $path = ''): array
{
return Directory::names(lang_path());
return collect(Directory::names(lang_path($path)))
->when(
fn (Collection $items) => $items->contains('vendor'),
fn (Collection $items) => $items->merge(
collect($this->directories('vendor'))->map(
fn (string $folder) => $this->directories('vendor/' . $folder)
)
)
)
->flatten()
->all();
}

protected function jsons(): array
Expand Down
21 changes: 18 additions & 3 deletions src/Services/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,22 @@ public function isInstalled(Locale|string|null $locale): bool

public function isProtected(Locale|string|null $locale): bool
{
return $this->registry([__METHOD__, $locale], fn () => $this->inArray($locale, $this->protects()));
return $this->registry([__METHOD__, $locale], function () use ($locale) {
$locales = $this->protects();

return $this->inArray($this->fromAlias($locale), $locales)
|| $this->inArray($this->toAlias($locale), $locales);
});
}

public function getDefault(): string
{
return $this->registry(__METHOD__, function () {
$locale = config('app.locale');

return $this->isAvailable($locale) ? $locale : $this->getFallback();
return $this->toAlias(
$this->isAvailable($locale) ? $locale : $this->getFallback()
);
});
}

Expand All @@ -113,7 +120,15 @@ public function getFallback(): string
return $this->registry(__METHOD__, function () {
$locale = config('app.fallback_locale');

return $this->isAvailable($locale) ? $locale : Locale::English->value;
if ($this->isAvailable($locale)) {
return $this->toAlias($locale);
}

$fallback = config('app.locale');

return $this->toAlias(
$this->isAvailable($fallback) ? $fallback : Locale::English->value
);
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Helpers/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ function setLocales(bool|Locale|string|null $main = false, bool|Locale|string|nu
}
}

function setMainLocale(Locale|string $locale): void
function setMainLocale(Locale|string|null $locale): void
{
config()->set('app.locale', $locale->value ?? $locale);
}

function setFallbackLocale(Locale|string $locale): void
function setFallbackLocale(Locale|string|null $locale): void
{
config()->set('app.fallback_locale', $locale->value ?? $locale);
}
19 changes: 19 additions & 0 deletions tests/Unit/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

declare(strict_types=1);

use DragonCode\Support\Facades\Filesystem\Directory;
use DragonCode\Support\Facades\Filesystem\File;
use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Facades\Locales;

Expand All @@ -33,3 +35,20 @@
'sr_Cyrl',
]);
});

it('checks aliases of installed locales in the vendor folder', function () {
File::store(lang_path('vendor/custom/de.json'), '{}');
File::store(lang_path('vendor/custom/de-DE.json'), '{}');

Directory::ensureDirectory(lang_path('vendor/custom/de_CH'));
Directory::ensureDirectory(lang_path('vendor/custom/de-CH'));

setAlias(Locale::German, 'de-DE');
setAlias(Locale::GermanSwitzerland, 'de-CH');

expect(Locales::installed())->toBe([
'de-CH',
'de-DE',
'en',
]);
});
13 changes: 8 additions & 5 deletions tests/Unit/Facades/Locales/AvailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
it('checks the list of available locales', function () {
expect(Locales::available())
->toBeArray()
->toBeIn([Locale::English->value])
->not->toBeIn(['foo', 'bar']);
->toContain(
Locale::English->value,
Locale::German->value
)
->not->toContain('foo', 'bar');
});

it('checks the list of available locales taking into account aliases', function (Locale $locale, string $alias) {
setAlias($locale, $alias);

expect(Locales::available())
->toBeArray()
->toBeIn([$alias])
->not->toBeIn([$locale->value]);
->toContain($alias)
->not->toContain($locale->value);
})->with('aliased-locales');

it('checks incorrect locales against the list of available ones', function (?string $locale) {
expect(Locales::available())->toBeArray()->not->toBeIn([$locale]);
expect(Locales::available())->toBeArray()->not->toContain($locale);
})->with('incorrect-locales');
18 changes: 8 additions & 10 deletions tests/Unit/Facades/Locales/GetDefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
->toBe(Locale::English->value);
});

it('returns a backup locale if the main one is null', function () {
setLocales(null, Locale::German);

expect(Locales::getDefault())
->toBe(Locale::German->value);
});

it('returns English locale if primary and fallback are incorrect', function () {
setLocales('foo', 'foo');

Expand All @@ -54,18 +61,9 @@
->not->toBe($locale->value);
})->with('aliased-locales');

it('will return a backup locale if the main one is null', function () {
setLocales(null, Locale::German);

expect(Locales::getDefault())
->toBe(Locale::German->value)
->not->toBeNull();
});

it('will return the English locale if both are set to null', function () {
setLocales(null, null);

expect(Locales::getDefault())
->toBe(Locale::English->value)
->not->toBeNull();
->toBe(Locale::English->value);
});
30 changes: 14 additions & 16 deletions tests/Unit/Facades/Locales/GetFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
->toBe(Locale::German->value);
});

it('returns the main localization if the spare is null', function () {
setLocales(Locale::German, null);

expect(Locales::getFallback())
->toBe(Locale::German->value);
});

it('returns the English locale if both are set to null', function () {
setLocales(null, null);

expect(Locales::getFallback())
->toBe(Locale::English->value);
});

it('returns English locale if both are invalid', function () {
setLocales('foo', 'foo');

Expand All @@ -60,19 +74,3 @@
->toBe($alias)
->not->toBe($locale->value);
})->with('aliased-locales');

it('will return the main localization if the spare is null', function () {
setLocales(Locale::German, null);

expect(Locales::getFallback())
->toBe(Locale::German->value)
->not->toBeNull();
});

it('will return the English locale if both are set to null', function () {
setLocales(null, null);

expect(Locales::getFallback())
->toBe(Locale::English->value)
->not->toBeNull();
});
36 changes: 22 additions & 14 deletions tests/Unit/Facades/Locales/InstalledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@

it('returns English locale as the only one installed', function () {
expect(Locales::installed())
->toBe([Locale::English->value])
->not->toBeIn([Locale::Thai->value, Locale::Turkish->value]);
->toBe([Locale::English->value]);
});

it('returns English and German locales as the only ones installed', function () {
setLocales(Locale::German);

expect(Locales::installed())
->toBe([
Locale::English->value,
Locale::German->value,
Locale::English->value,
])
->not->toBeIn([
->not->toContain([
Locale::Thai->value,
Locale::Turkish->value,
]);
Expand All @@ -44,17 +43,16 @@
setLocales('foo', 'foo');

expect(Locales::installed())
->toBe([Locale::English->value])
->not->toBeIn([Locale::Thai->value, Locale::Turkish->value]);
->toBe([Locale::English->value]);
});

it('returns German and French locales as the only ones installed', function () {
setLocales(Locale::German, Locale::French);

expect(Locales::installed())
->toBe([
Locale::French->value,
Locale::German->value,
Locale::French->value,
]);
});

Expand All @@ -65,15 +63,11 @@

expect(Locales::installed())
->toBe([
Locale::German->value,
Locale::English->value,
Locale::French->value,
Locale::German->value,
Locale::Russian->value,
Locale::Ukrainian->value,
])
->not->toBeIn([
Locale::Thai->value,
Locale::Turkish->value,
]);
});

Expand All @@ -82,8 +76,8 @@

expect(Locales::installed())
->toBe([
Locale::English->value,
Locale::German->value,
Locale::English->value,
]);
});

Expand All @@ -92,7 +86,21 @@

expect(Locales::installed())
->toBe([
Locale::English->value,
Locale::German->value,
Locale::English->value,
]);
});

it('checks installed localizations without declaring aliases', function () {
File::store(lang_path('vendor/custom/de.json'), '{}');
File::store(lang_path('vendor/custom/de-DE.json'), '{}');

Directory::ensureDirectory(lang_path('vendor/custom/de_CH'));
Directory::ensureDirectory(lang_path('vendor/custom/de-CH'));

expect(Locales::installed())->toBe([
'de',
'de_CH',
'en',
]);
});
2 changes: 1 addition & 1 deletion tests/Unit/Facades/Locales/IsAvailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Facades\Locales;

it('checks availability of all locales', function (Locale $locale) {
it('checks availability of all locales', function (string $locale) {
expect(Locales::isAvailable($locale))->toBeBool()->toBeTrue();
})->with('locales');

Expand Down
30 changes: 15 additions & 15 deletions tests/Unit/Facades/Locales/NotInstalledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
it('checks whether protected locales are installed', function () {
expect(Locales::notInstalled())
->toBeArray()
->toBeIn([Locale::German])
->not->toBeIn([Locale::English->value]);
->toContain(Locale::German->value)
->not->toContain(Locale::English->value);
});

it('checks whether the main locale is installed', function () {
setLocales(main: Locale::German);

expect(Locales::notInstalled())
->toBeIn([Locale::French])
->not->toBeIn([Locale::German->value, Locale::English->value]);
->toContain(Locale::French->value)
->not->toContain(Locale::German->value, Locale::English->value);
});

it('checks whether a fallback locale is installed', function () {
setLocales(fallback: Locale::German);

expect(Locales::notInstalled())
->toBeIn([Locale::French])
->not->toBeIn([Locale::German->value, Locale::English->value]);
->toContain(Locale::French->value)
->not->toContain(Locale::German->value, Locale::English->value);
});

it('checks the availability of installed localizations', function () {
Expand All @@ -49,34 +49,34 @@
setLocales(fallback: Locale::German);

expect(Locales::notInstalled())
->toBeIn([Locale::Urdu])
->not->toBe([
->toContain(Locale::Urdu->value)
->not->toContain(
Locale::English->value,
Locale::French->value,
Locale::German->value,
Locale::Russian->value,
Locale::Ukrainian->value,
]);
);
});

it('check localization detection by file availability', function () {
File::store(lang_path('vendor/custom/de.json'), '{}');

expect(Locales::notInstalled())
->toBeIn([Locale::French])
->not->toBe([
->toContain(Locale::French->value)
->not->toContain(
Locale::English->value,
Locale::German->value,
]);
);
});

it('check localization detection by directory availability', function () {
Directory::ensureDirectory(lang_path('vendor/custom/de'));

expect(Locales::notInstalled())
->toBeIn([Locale::French])
->not->toBe([
->toContain(Locale::French->value)
->not->toContain(
Locale::English->value,
Locale::German->value,
]);
);
});
Loading

0 comments on commit 20e458c

Please sign in to comment.