Nuxt-i18n language switcher
So I'm trying to setup localization for my app. Now I've used the provided setup from the docs and managed to get it working for one language. When I try to switch the language, I get a lot of warnings for missing keys in my locale messages.
Which is very strange as both of the language JSONs are exactly the same. So for a test I changed the config to use the other language as default and it works, but when switching the result is the same.
So this is how I setup in my nuxt.config:
In my root folder I've created a new folder(s) i18n/locales/ with 2 json files, en.json and sl.json.
When I try to switch with:
It loads, but it doesn't find any keys. Then I do a direct URL with the updated language path it does load the correct translations.
When using:
It does switch, but because there is no call for refreshData(), so the data is not translated.
So my question is how to correctly switch between translations?
3 Replies
Hi @Piskr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
It looks like your Nuxt i18n configuration and language files are set up correctly, but you’re experiencing issues where switching languages via your custom function results in missing translation keys, while direct URL navigation works as expected.
Based on the knowledge sources, here are some key points and suggestions:
1. File-based Lazy Loading:
If you specify a
Example: See related discussion 2. Switching Locale and Data Refresh:
When you use
The recommended way is to use
This suggests your translation files and routing are correct, but the manual locale switch is not triggering the correct reactivity or file loading. 4. Summary of Correct Approach:
- Use
If you specify a
file
property for each locale, you must also set lazy: true
and provide the langDir
option pointing to your locales directory. Otherwise, Nuxt i18n will not know where to load the language files from. Your current config has lazy: false
, which means it expects all translations to be inlined, not loaded from files.Example: See related discussion 2. Switching Locale and Data Refresh:
When you use
switchLocalePath
and navigate, Nuxt i18n will handle the locale change and load the correct translations. However, if you manually set locale.value = langCode
and call refreshData()
, you might be changing the locale before the route actually updates, leading to a mismatch in loaded translations.The recommended way is to use
NuxtLink
or programmatic navigation with the path from switchLocalePath
, and let Nuxt i18n handle the rest. If you need to refresh data after the locale changes, you should do so in a watcher or after navigation is complete.
3. Direct Navigation Works:This suggests your translation files and routing are correct, but the manual locale switch is not triggering the correct reactivity or file loading. 4. Summary of Correct Approach:
- Use
lazy: true
and langDir
if you use external JSON files.
- Use NuxtLink
or router.push(switchLocalePath(langCode))
to switch languages.
- If you need to refresh data, do it after navigation is complete, possibly by watching the locale or route.
Example config:
Example switcher:
If you need to refresh data, watch the locale:
This approach ensures translations are loaded and data is refreshed after the language actually changes.
If you follow these steps and still encounter issues, double-check your file paths and ensure your JSON files are valid and accessible. If you need more advanced control, refer to the official Nuxt i18n documentation.
The knowledge sources do not provide a direct example of your exact use case, but the above advice is based on similar issues and recommended practices found in the sources, especially regarding lazy loading and file-based locales Stack Overflow Post.---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@214445863982858240> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
I'm kinda new to this too, but I get it working a few days ago so I can share my files and you can test it out. Firstly I assume you want to persist language changes in which case you need to enable cookies in your i18n setup so it persists through a refresh this is my nuxt.config.ts, in my case I didn't want the locale in the url but if you do you just switch the strategy
then in my app.vue i have this in my script tag which sets my html header with the language as you change languages, aswell as setting the locale in uapp
granted im also using nuxt-ui-pro language switcher in my setup but it should be similar. that's my language switcher component
so on page load it grabs the users browsers default locale, unless they're manually switches languages in which case it will update my pages text to that language and update the cookie to persist the new language on page reloads
then in my etc i have and then for example in my sign up button to display the translated text i have this so i can change language and the text updates immediately throughout
But yeah i think i remember something potentially about missing keys or at least the text didn't update immediately until i refreshed my page but with this setup it works great. probably just missing the cookie which i believe when updated will refresh your text translations so you don't get missing keys warning
I'm not using a pinia store for this either btw, didn't see a reason since the cookie persists their language nicely
you might not need the UApp locale part maybe that's only a nuxt ui pro thing and you can replace the localeselector with your diy one there's an example in their docs
also if their browser default language is set to one you don't have configured in your nuxt config it won't set the cookie to it it defaults to your fallback