Locale detection not working with `no_prefix` strategy
Environment
- Operating System: Darwin
- Node Version: v22.15.0
- Nuxt Version: 3.17.3
- CLI Version: 3.25.1
- Nitro Version: 2.11.12
- Package Manager: [email protected]
- Builder: -
- User Config: compatibilityDate, devtools, modules, i18n, nitro
- Runtime Modules: @nuxt/[email protected], @nuxtjs/[email protected]
- Build Modules: -
Reproduction
A minimal reproduction of this issue can be found in the following repository: https://github.com/odan-sandbox/nuxt-content-and-i18n-no_prefix-example
Describe the bug
When using the no_prefix strategy in nuxt-i18n, the locale detection based on the URL pathname is not functioning as expected. This results in incorrect locale assignment during SSR/SSG.
For example, accessing /en/about and /ja/about should render the corresponding locale-specific content, but instead, both paths are rendered using the default or server locale, ignoring the locale information in the pathname.
This issue causes problems for multilingual documentation sites using nuxt/content, where the content is structured by locale (e.g., docs/en/about.md, docs/ja/about.md). During SSG, both paths may be generated with the same language content, leading to incorrect output.
If a PR to resolve this issue is considered appropriate, I would be willing to work on it.
Additional context
Related: https://github.com/nuxt-modules/i18n/issues/3609
Logs
I have the exact same problem! Thanks for raising the issue.
I used a global middleware:
export default defineNuxtRouteMiddleware((to) => {
const app = useNuxtApp();
// Setting the i18n locale because we use no_prefix strategy
app.$i18n.locale.value = getCountryCodeFromUrl(to.path) as 'at' | 'de';
});
Do you think this is a valid strategy? It works for my setup... but I know you shouldn't set the locale directly, but I didn't found another way.
How do you solve it right now?
@LukasHechenbergerID
You can directly set locale, it just won't trigger things like loading translations/redirection/cookie sync, as long as you are aware it bypasses underlying behaviors it is fine. Perhaps the workaround below could help out as well.
@odanado While the requested feature is still under consideration (and partially worked towards), I do have a workaround that might suit your use case. I have forked your reproduction and updated it here: https://github.com/BobbieGoede/nuxt-i18n-3619. This workaround uses the latest v10 beta, which may not be stable enough for your use case 🙇
In this workaround I have added a plugin which attempts to detect the locale based on the current path, this ensures prerendering uses the correct language. This also sets the locale for the user (and updates their cookie to match), switching the locale is desired as this prevents a hydration mismatch.
I reset the cookie to its original value and show a basic dialog in case the current page does not match the cookie locale, allowing users to switch. This is client-only, to ensure no hydration mismatch occurs.
Personally I think it would be nice to redirect to the detected locale, but there is an issue upstream that prevents us from doing so https://github.com/nuxt/nuxt/issues/28174.
Let me know if this workaround works for you or if you are running into issues 🙏 This is not meant to replace/resolve the requested feature.