getRelativeLocaleUrl() should correctly identify and parse paths that are already localized to one language, so they can be "translated" to a different language. #1100
victor-marino
started this conversation in
Proposal
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Body
Summary
The
getRelativeLocaleUrl()
currently only works if you pass a "clean", unlocalized path to it. It's not able to change the language for an already localized path. This makes it a bit challenging and counter-intuitive to implement a reliable language switcher in our websites.Background & Motivation
In my site, I have two languages set up:
/
/en/
I have a navbar that correctly links to the right pages depending on which language the user is browsing now, which is easy by using
getRelativeLocaleUrl()
since the navbar links always point to a known, static page that I can hard-code. E.g.:However, things get trickier when implementing the "language switcher" control. The expected behaviour is that, if the user is browsing the
about
page and they switch languages, they should be taken to the same page they're currently viewing, but in the other language.The issue is, the only function that I can find to get the current URL path is
Astro.url.pathname
, and that will return the full path, including any localization subfolders. But thegetRelativeLocaleUrl()
seems to always expect a "clean" path without any language subfolders.So if the user is browsing a page in the default language without any localized subfolder, such as
mysite.com/about/
, all will be good. The function will return/about/
, which I can correctly pass like:However, if the user is browsing the English version at
mysite.com/en/about/
and I want to take him to the default (es
) language of that page, that doesn't work.as
getRelativeLocaleUrl()
assumes the provided path is always a default path free of subdomains, and so it doesn't modify it.Right now, I'm using a little one-liner helper to strip the locale from a path (if present) before passing it to
getRelativeLocaleUrl()
:But I'm not sure how reliable that will be going forward, and either way I think this functionality should be built-in.
Goals
In my view, we should either:
getRelativeLocaleUrl()
function correctly identify and parse localized paths nativelyand/or:
Astro.url.pathname
, something likeAstro.url.unlocalizedPathname
, that returns the current path without the language subfolder, so that it can always be reliably passed togetRelativeLocaleUrl()
.Does this make sense? Or am I missing something and there is perhaps an existing, 'native' way of doing this already?
Beta Was this translation helpful? Give feedback.
All reactions