astro-i18next icon indicating copy to clipboard operation
astro-i18next copied to clipboard

Support routes.json to localize URLS?

Open L1lith opened this issue 2 years ago • 3 comments

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.

L1lith avatar Aug 29 '22 18:08 L1lith

This would be a really cool feature!

I think it would be easy to have localized route using the generate command.

  1. Let's say there are 3 pages:
src
 └-- pages
      |-- about.astro 
      |-- contact-us.astro 
      └-- index.astro
  1. There must be a route mapping somewhere, like routes.json inside pages' folders as you mentioned or have it directly inside the astro-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.

  1. 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
  1. 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.

yassinedoghri avatar Sep 03 '22 10:09 yassinedoghri

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.

yassinedoghri avatar Nov 06 '22 18:11 yassinedoghri

Hey @yassinedoghri , is it possible to handle localized URLs with Country code top-level domains (.en, .nl, etc.)?

fprl avatar Apr 26 '23 14:04 fprl