Skip to content

Commit

Permalink
refactored translated_url
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Jul 3, 2022
1 parent 7e27ad0 commit 42bd135
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/LoadLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,22 @@ private function registerTranslatedUrlHelper(Jigsaw $jigsaw)
* ! This helper relies on the language prefix folder structure
*/
function ($page, string $trans_lang, string|null $current_lang = null): string {
$href = '';

$current_lang ??= $page->current_path_lang();

if ($current_lang === $page->default_lang) {
// "default_lang" isn't shown at the beginning of the URL
// So just prefix the translation lang "/YY"
$href = "/$trans_lang" . $page->getPath();
} else {
// Remove the lang prefix "/XX"
// prefix the translation lang "/YY"
$href = "/$trans_lang" . substr($page->getPath(), 3);
}
$partial_path = $current_lang === $page->default_lang ?
$page->getPath() :
substr($page->getPath(), 3);

if (str_starts_with($href, '/' . $page->default_lang)) {
$href = substr($href, 3);
}
$path = "/$trans_lang" . $partial_path;

if (empty($href)) {
return $page->url('/');
if (str_starts_with($path, '/' . $page->default_lang)) {
$path = substr($path, 3);
}

return $page->url($href);
return empty($path) ?
$page->url('/') :
$page->url($path);
}
);
}
Expand All @@ -122,7 +116,7 @@ function ($page, string $partial_path, string|null $current_lang = null): string
$current_lang ??= $page->current_path_lang();

if (!str_starts_with($partial_path, '/')) {
$partial_path = '/' . $partial_path;
$partial_path = "/$partial_path";
}

if ($current_lang === $page->default_lang) {
Expand Down

0 comments on commit 42bd135

Please sign in to comment.