[kapa.ai] i18n/nuxtjs. Issues with differentDomains
./nuxt.config.ts:
./locale-domains.config.ts:
./app/pages/test.vue:
The problem, is that currently the creates a path that has locale code in both subdomain and url path (i.e. http://lv.localhost:3000/lv/test).
If I delete the locale code from url pth using , then starts to display only default language no matter what subdomain I'm on.
How to fix?
6 Replies
Hi @Mustlane. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Your issue with differentDomains and the locale code appearing both in the subdomain and the URL path (e.g., http://lv.localhost:3000/lv/test) is a common configuration pitfall with @nuxtjs/i18n. Here’s what’s happening and how to address it:
Problem Analysis
- When you use differentDomains: true, @nuxtjs/i18n expects to handle locale switching via domains (subdomains or separate domains), not via URL prefixes.
- If you keep the default strategy (like prefix_except_default or prefix_and_default), the locale code will appear in the path as well as the domain, resulting in URLs like http://lv.localhost:3000/lv/test.
- If you switch to strategy: 'no_prefix', the locale code is removed from the path, but you’re seeing that $t('welcome') only shows the default language, regardless of the subdomain.
Solution
1. Configuration Placement
- The locales, defaultLocale, differentDomains, and strategy options should be set in nuxt.config.ts, not in i18n.config.ts. This is confirmed by the troubleshooting in the knowledge sources, where misplacement of config options led to unexpected behavior Nuxt 3 i18n browser detection not working Part 6.
2. Correct Strategy
- When using differentDomains: true, you must use strategy: 'no_prefix'. This ensures that the locale is determined by the domain/subdomain, not by the URL path.
3. Domain Mapping
- Make sure that the domain values in your locales array are correctly set and that your development environment supports these subdomains (e.g., lv.localhost, en.localhost, etc.).
4. Locale Detection
- With differentDomains: true and strategy: 'no_prefix', @nuxtjs/i18n should detect the locale from the domain and set the correct language context, so $t('welcome') should work as expected.
5. Example Configuration
6. Troubleshooting
- If $t('welcome') still only shows the default language, double-check that your subdomains are correctly mapped to the expected locale in your hosts file or DNS, and that your browser is actually loading the site from the correct subdomain.
- Also, ensure that your translation files (e.g., lv.json, en.json) are correctly loaded and available.
Reference
This approach is supported by the troubleshooting and configuration advice found in Nuxt 3 i18n browser detection not working Part 6, which clarifies the importance of placing the correct options in nuxt.config.ts and using the right strategy for your domain setup.
---
Summary:
Use strategy: 'no_prefix' with differentDomains: true, and ensure all i18n options are in nuxt.config.ts. This will remove the locale code from the path and rely on the domain/subdomain for locale detection, making $t('welcome') work as expected for each domain. If issues persist, check your subdomain setup and translation file loading. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
Use strategy: 'no_prefix' with differentDomains: true, and ensure all i18n options are in nuxt.config.ts. This will remove the locale code from the path and rely on the domain/subdomain for locale detection, making $t('welcome') work as expected for each domain. If issues persist, check your subdomain setup and translation file loading. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@698588797394354227> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
"Make sure that the domain values in your locales array are correctly set and that your development environment supports these subdomains (e.g., lv.localhost, en.localhost, etc.)."
What do you mean by "supports these subdomains"?
Let me check the knowledge base for you again. One moment! :books:
----------------------
When I say "supports these subdomains," I mean that your local development environment must be configured so that addresses like lv.localhost, en.localhost, etc., actually resolve to your Nuxt app. This typically involves editing your system's hosts file to map each subdomain to 127.0.0.1 (localhost).
For example, you would add lines like:
This setup ensures that when you visit http://lv.localhost:3000 in your browser, it points to your local Nuxt development server, allowing the i18n module to detect the locale based on the subdomain.
The knowledge sources do not explicitly describe this hosts file setup, but they do mention that the domain values in your locales array must be correctly set and that your environment should route those domains to your Nuxt app for proper locale detection with differentDomains: true Nuxt 3 i18n browser detection not working Part 6.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@698588797394354227> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Eh.. I guess I gotta figure it out on my own..