Replies: 2 comments 1 reply
-
|
I think all utilities that work on locales and URLs can be ported on the client too. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for opening this discussion @delucis! We built a static marketing website (https://awareways.com/en/) with 2 languages. There are a couple client-side interactions (e.g. the navigation menu) but no server-side code running through Astro, just the SSG. The only function we are currently using from I followed the add i18n features guide and adapted some of the code examples to fit our needs. Here is our utility that wraps type LocaleCode = "en" | "nl";
export function useTranslatedPath(lang: LocaleCode) {
return function translatePath(path: string, l: LocaleCode = lang) {
const route = path.replaceAll("/", "");
const hasTranslation =
defaultLang !== l &&
routes[l] !== undefined &&
routes[l][route] !== undefined;
const translatedPath = hasTranslation ? routes[l][route] : path;
const [pathname, hash] = translatedPath.split("#");
if (hash) return `${getRelativeLocaleUrl(l, pathname)}#${hash}`;
return getRelativeLocaleUrl(l, pathname);
};
}For consistency, we use this utility for all anchors in the website. The key difference is that our Taking a step back, all we really need
There are probably a handful of ways for us to work around this problem. We could move all the Working with Astro has been an absolute pleasure so far, but |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
Astro’s
astro:i18nmodule provides a number of utilities for working with localized URLs, but is intended for server use only. This proposal discusses adding a way to use these from the client.Background & Motivation
Astro’s utilities like
getRelativeLocaleUrl()orgetPathByLocale()help work with URLs in sites using i18n but aren’t officially available on the client. Some users have been importingastro:i18nfrom browser code, but this is risky for a number of reasons:astro:i18non the client in sites with manual i18n configs will mean the utilities don’t behave correctly.Goals
astro:i18non the client to support use cases like language switcher components.Example
Existing APIs and usage should be possible
Challenges
This would require a significant refactor of the i18n feature and likely a breaking change to the manual i18n option to move away from the current side-effect-powered implementation.
Note
We need more user feedback and information to know what the common use cases are and whether the effort would be justified. Comment below!
Beta Was this translation helpful? Give feedback.
All reactions