astro-i18next
astro-i18next copied to clipboard
Support routes.json to localize URLS?
it would be cool if we could put a routes.json inside any folder inside pages
to change how the page urls are localized. Here's a lib that does something like that already.
This would be a really cool feature!
I think it would be easy to have localized route using the generate command.
- Let's say there are 3 pages:
src
└-- pages
|-- about.astro
|-- contact-us.astro
└-- index.astro
- There must be a route mapping somewhere, like
routes.json
insidepages
' folders as you mentioned or have it directly inside theastro-i18next
config file (not sure what would be best here):
/** @type {import('astro-i18next').AstroI18nextConfig} */
export default {
defaultLanguage: "en",
supportedLanguages: ["en", "fr"],
i18next: {
...
},
routes: {
fr: {
about: "a-propos",
contact-us: "contactez-nous",
}
}
};
Note The home page is the root, so no need to map it.
- The
generate
command should grab the mapping for each language and create a file with the mapped name to get the route localized:
src
└-- pages
├-- fr
| |-- a-propos.astro
| |-- contactez-nous.astro
| └-- index.astro
|-- about.astro
|-- contact-us.astro
└-- index.astro
- The
localizePath(...)
utility function must return the right route name as well:
localizePath('/about');
// --> returns "/fr/a-propos"
In a more complex example, i.e. having deeply nested routes (folders within folders), I guess that it may be best to have a routes.json
mapping live inside the pages' folders for easier maintenance.
Translating routes are now available since beta.13 (thanks to @Alexandre-Fernandez!).
I'm keeping this open as I think having routes.json
files live next to the pages
or locales
folders could be better for maintenance and i18n collaboration.
Hey @yassinedoghri , is it possible to handle localized URLs with Country code top-level domains (.en
, .nl
, etc.)?