Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support routes.json to localize URLS? #29

Open
L1lith opened this issue Aug 29, 2022 · 3 comments
Open

Support routes.json to localize URLS? #29

L1lith opened this issue Aug 29, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@L1lith
Copy link

L1lith commented Aug 29, 2022

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.

@yassinedoghri
Copy link
Owner

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 pushed a commit that referenced this issue Oct 10, 2022
@yassinedoghri yassinedoghri added the enhancement New feature or request label Nov 6, 2022
yassinedoghri pushed a commit that referenced this issue Nov 6, 2022
yassinedoghri pushed a commit that referenced this issue Nov 6, 2022
# [1.0.0-beta.13](v1.0.0-beta.12...v1.0.0-beta.13) (2022-11-06)

### Bug Fixes

* add isFileHidden function + tests to prevent missing hidden files ([7dcd0aa](7dcd0aa))
* **generate:** replace isLocale check with user defined locales to prevent nested folders generation ([a598e2e](a598e2e)), closes [#56](#56)
* **i18next-server:** load locale files synchronously ([e7892e2](e7892e2))
* update types import to relative ([#58](#58)) ([44a5422](44a5422))

### Features

* add option to show the default locale in the url ([#51](#51)) ([ea939db](ea939db)), closes [#54](#54)
* add support for route translations ([db5200b](db5200b)), closes [#50](#50) [#29](#29)
* allow implicit key for <Trans> when omitting i18nKey prop ([ff14354](ff14354)), closes [#53](#53)
* simplified API + instanciate i18next both in server and client side ([ed44510](ed44510)), closes [#57](#57) [#46](#46) [#37](#37)

### BREAKING CHANGES

* - defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`
@yassinedoghri
Copy link
Owner

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.

@fprl
Copy link

fprl commented Apr 26, 2023

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

AliLee0923 pushed a commit to AliLee0923/astro-i18N that referenced this issue Dec 2, 2023
# [1.0.0-beta.13](yassinedoghri/astro-i18next@v1.0.0-beta.12...v1.0.0-beta.13) (2022-11-06)

### Bug Fixes

* add isFileHidden function + tests to prevent missing hidden files ([7dcd0aa](yassinedoghri/astro-i18next@7dcd0aa))
* **generate:** replace isLocale check with user defined locales to prevent nested folders generation ([a598e2e](yassinedoghri/astro-i18next@a598e2e)), closes [#56](yassinedoghri/astro-i18next#56)
* **i18next-server:** load locale files synchronously ([e7892e2](yassinedoghri/astro-i18next@e7892e2))
* update types import to relative ([#58](yassinedoghri/astro-i18next#58)) ([44a5422](yassinedoghri/astro-i18next@44a5422))

### Features

* add option to show the default locale in the url ([#51](yassinedoghri/astro-i18next#51)) ([ea939db](yassinedoghri/astro-i18next@ea939db)), closes [#54](yassinedoghri/astro-i18next#54)
* add support for route translations ([db5200b](yassinedoghri/astro-i18next@db5200b)), closes [#50](yassinedoghri/astro-i18next#50) [#29](yassinedoghri/astro-i18next#29)
* allow implicit key for <Trans> when omitting i18nKey prop ([ff14354](yassinedoghri/astro-i18next@ff14354)), closes [#53](yassinedoghri/astro-i18next#53)
* simplified API + instanciate i18next both in server and client side ([ed44510](yassinedoghri/astro-i18next@ed44510)), closes [#57](yassinedoghri/astro-i18next#57) [#46](yassinedoghri/astro-i18next#46) [#37](yassinedoghri/astro-i18next#37)

### BREAKING CHANGES

* - defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants