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

Astro.currentLocale not working correctly in root 404.astro with i18n #12356

Open
1 task
otra-ch opened this issue Nov 2, 2024 · 6 comments · May be fixed by #12365
Open
1 task

Astro.currentLocale not working correctly in root 404.astro with i18n #12356

otra-ch opened this issue Nov 2, 2024 · 6 comments · May be fixed by #12365
Labels
needs triage Issue needs to be triaged

Comments

@otra-ch
Copy link

otra-ch commented Nov 2, 2024

Astro Info

Astro                    v4.16.8
Node                     v22.9.0
System                   macOS (arm64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             astro-icon
                         @sanity/astro
                         @astrojs/react

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

No response

Describe the Bug

When using a 404.astro page at the root level (pages/404.astro), Astro.currentLocale always returns the defaultLocale even when visiting a different locale URL path.

Example:

  • URL: /de/nonexistent-page
  • Astro.currentLocale returns 'fr' (defaultLocale) instead of 'de'

Console log shows:
{ currentLocale: 'fr', url: '/de/fasf', params: {} }

This works correctly in some projects but not others with similar configurations.

My config:

i18n: {
   defaultLocale: "fr",
   locales: ["fr", "de"],
   routing: {
      prefixDefaultLocale: true,
      redirectToDefaultLocale: false,
   }
}

What's the expected result?

Astro.currentLocale should return 'de' when visiting /de/* paths

Link to Minimal Reproducible Example

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 2, 2024
@apatel369
Copy link
Contributor

apatel369 commented Nov 2, 2024

@otra-ch Please add a link to a reproducible example.

@otra-ch
Copy link
Author

otra-ch commented Nov 2, 2024

Sure here you go : https://stackblitz.com/edit/github-e8pwbg?file=src%2Fpages%2Ffr%2Fabout.astro

@ematipico
Copy link
Member

ematipico commented Nov 3, 2024

I don't think your expectations are correct, or they are perhaps too broad.

I think the expectation should be only for pages that don't exist that don't use the fallback. The reason why I say so is that because there's also a fallback system involved, if opted in. And if so, the page that doesn't exist will use the fallback, and the current locale should be the locale of the rewritten route.

What do you think, @otra-ch ?

@otra-ch
Copy link
Author

otra-ch commented Nov 3, 2024

Hey @ematipico, my use case is specifically about 404 pages without any fallback configuration. When a user visits /de/non-existent-page, I expect Astro.currentLocale to reflect the language in the URL ('de'), since that's what the user explicitly requested, even if the page doesn't exist.

@stefanprobst
Copy link

this was working for me in 4.15.12 but broke with 4.16.0

@apatel369
Copy link
Contributor

@ematipico My PR specifically handles 404 pages without any fallback configuration.

apatel369 added a commit to apatel369/astro that referenced this issue Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants