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

i18n fallback does not work with manual routing #12431

Open
1 task done
Alejandro-FA opened this issue Nov 14, 2024 · 0 comments
Open
1 task done

i18n fallback does not work with manual routing #12431

Alejandro-FA opened this issue Nov 14, 2024 · 0 comments
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: i18n Related to internalization (scope)

Comments

@Alejandro-FA
Copy link

Alejandro-FA commented Nov 14, 2024

Astro Info

Astro                    v4.16.12
Node                     v22.11.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

The i18n.fallback and i18n.routing.fallbackType options are ignored by the middleware function from astro:i18n when using manual routing.

As far as I understand, the following two setups should have the same behavior:

Configuration 1 (default routing middleware)

Configure astro.config.mjs to set the fallback of the es locale to en. Furthermore, specify the fallbackType to "redirect".

import { defineConfig } from "astro/config"
export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "es"],
    fallback: { es: "en" },
    routing: {
      prefixDefaultLocale: true,
      fallbackType: "redirect",
    },
  }
})

Configuration 2 (manual routing)

Configure astro.config.mjs to set the fallback of the es locale to en.

import { defineConfig } from "astro/config"
export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "es"],
    fallback: { es: "en" },
    routing: "manual",
  }
})

Use the middleware function in src/middleware.ts to recover the default routing behavior (and set the fallbackType to `"redirect" as done before)

import { sequence } from "astro:middleware";
import { middleware } from "astro:i18n";

export const onRequest = sequence(
  middleware({
    prefixDefaultLocale: true,
    redirectToDefaultLocale: true,
    fallbackType: "redirect",
  }),
);

Behavior

With the first configuration, navigating to /es correctly redirects to /en if src/pages/es/index.astro does not exist. However, with the second configuration, navigating to /es throws a 404 error instead of redirecting to /en.

What's the expected result?

The fallback and fallbackType options behave in manual routing exactly as in "automatic" routing.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-wtyvk1?file=src%2Fmiddleware.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Nov 14, 2024
@ematipico ematipico added - P3: minor bug An edge case that only affects very specific usage (priority) feat: i18n Related to internalization (scope) labels Nov 18, 2024
@ematipico ematipico self-assigned this Nov 18, 2024
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: i18n Related to internalization (scope)
Projects
None yet
Development

No branches or pull requests

2 participants