Skip to content

Commit

Permalink
fix: usage of defaultLocale property
Browse files Browse the repository at this point in the history
Need to use the function packageDefaultLocale to prevent problems when
defaultLocale is not defined

Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Apr 3, 2024
1 parent f988a16 commit 5b6bce8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ function current_path_locale($page): string
{
$path = trim($page->getPath(), '/');

$default_locale = $page->defaultLocale ?? packageDefaultLocale();

/**
* - [a-z]{2,3} language code
* - [A-Z]{2} region code
Expand All @@ -41,7 +39,7 @@ function current_path_locale($page): string

preg_match($locale_regex, $path, $matches);

return $matches['locale'] ?? $default_locale;
return $matches['locale'] ?? packageDefaultLocale();
}

/**
Expand All @@ -56,12 +54,12 @@ function translate_path($page, ?string $target_locale = null): string
$current_locale = current_path_locale($page);

$partial_path = match (true) {
$current_locale === $page->defaultLocale => $page->getPath(),
$current_locale === packageDefaultLocale($page) => $page->getPath(),
default => substr($page->getPath(), strlen($current_locale) + 1),
};

return match (true) {
$target_locale === $page->defaultLocale => "{$partial_path}",
$target_locale === packageDefaultLocale($page) => "{$partial_path}",
default => "/{$target_locale}{$partial_path}",
};
}
Expand Down Expand Up @@ -89,7 +87,7 @@ function locale_path($page, string $partial_path, ?string $target_locale = null)
$partial_path = '/'.trim($partial_path, '/');

return match (true) {
$target_locale === $page->defaultLocale => $partial_path,
$target_locale === packageDefaultLocale($page) => $partial_path,
default => "/{$target_locale}{$partial_path}"
};
}
Expand Down
5 changes: 0 additions & 5 deletions tests/_Mocks/PageMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class PageMock

public array $localization = [];

public function __construct()
{
$this->defaultLocale = packageDefaultLocale();
}

public function setPath(string $path): static
{
$this->path = '/'.trim($path, '/');
Expand Down

0 comments on commit 5b6bce8

Please sign in to comment.