In Astro's docs, they suggest using a function like this to generate the translated version of a given URL:
import { ui, defaultLang, showDefaultLang } from './ui';
export function useTranslatedPath(lang: keyof typeof ui) {
return function translatePath(path: string, l: string = lang) {
return !showDefaultLang && l === defaultLang ? path : `/${l}${path}`
}
}
However, this only works well when converting from a path using the default language (without any language subdirectory) to one of the non-default languages.
Assuming my default site language is Spanish and my secondary language (the one using a subfolder) is English, how could create a function that also translates a localized English URL like this: https://mysite.com/en/about to this: https://mysite.com/about.
The function from above provided in the docs would return the same path that was provided to it (https://mysite.com/en/about), because if you pass the default locale to it, it just returns the unmodified path.