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

feat(i18n): fallback logic of defaultLocale for i18n routing #8899

Merged
merged 3 commits into from
Oct 24, 2023

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Oct 23, 2023

Changes

This PR implement the fallback system for when i18n.defaultLocale and i18n.fallbackControl = "redirect" are present in the configuration.

Addresses PLT-1098

The expected behaviour is that if a user defines the defaultLocale to "en" for example, then any route that has /en in, will be redirected to a location that doesn't have /en:

  • /en => /
  • /en/blog/slug => /blog/slug
  • /en/getting-started => /getting-started
  • /blog/en/slug => /blog/slug

The fallback to the default locale assumes that the user doesn't have a folder named after the defaultLocale.

To achieve that, here are the changes I made:

  • in side the middleware of i18n, I added some code that checks the presence of the default locale in the URL. If present a redirect is returned by the middleware;
  • for the build, I had to do a major refactor. I was having a hard time understanding how the routes should be calculated, so I changed the algorithm's logic.
    • now we create a map of locale => RouteData[], essentially we group the routes via their own locale. Routes that don't belong to a locale belong to the default locale;
    • to create the said map, I create a Set, so I can remove those routes that belong to a locale
    • to calculate fallbackFromRoutes and fallbackToRoutes, we now use said map
    • the added logic considers when a locale does a fallback to defaultLocale. The default locale doesn't have a prefix, so we to fallback to the root, e.g.: if defaultLocale is en, then when visitiring /it/getting-started, we do a redirect to /getting-started, NOT /en/getting-started
    • the rest of the logic is the same.

Testing

I updated the test cases based on the previous assumptions

Docs

N/A for now

@changeset-bot
Copy link

changeset-bot bot commented Oct 23, 2023

⚠️ No Changeset found

Latest commit: c667bae

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Oct 23, 2023
@ematipico ematipico changed the title feat(i18n): first draft of configuration (#8607) feat(i18n): fallback logic of defaultLocale for i18n routing Oct 23, 2023
@ematipico ematipico merged commit ea7df28 into feat/i18n-routing Oct 24, 2023
13 checks passed
@ematipico ematipico deleted the feat/i18-routing-fallback-default-locale branch October 24, 2023 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants