π Migration from nuxt-i18n
to Nuxt I18n Micro
β
π Introduction β
Migrating from nuxt-i18n
to Nuxt I18n Micro
can significantly improve the performance of your Nuxt application, especially in high-traffic environments or projects with large translation files. This guide provides a step-by-step approach to help you smoothly transition from nuxt-i18n
to Nuxt I18n Micro
.
π Why Migrate? β
The Nuxt I18n Micro
module offers several advantages over the traditional nuxt-i18n
:
- β‘ Improved Performance: Faster build times, lower memory usage, and smaller bundle sizes.
- π§ Simplified Configuration: A more streamlined setup process with fewer moving parts.
- π Better Resource Management: Optimized handling of large translation files and reduced server load.
π Key Differences β
Before you begin the migration process, itβs essential to understand the key differences between nuxt-i18n
and Nuxt I18n Micro
:
- π Route Management:
Nuxt I18n Micro
uses dynamic regex-based routing, generating only two routes regardless of the number of locales, unlikenuxt-i18n
which creates a separate route for each locale. - ποΈ Translation Files: Only JSON files are supported in
Nuxt I18n Micro
. The translations are split into global and page-specific files, which are auto-generated in development mode if not present. - π SEO Integration:
Nuxt I18n Micro
offers built-in SEO optimization with automatic meta tag generation and support forhreflang
tags.
π οΈ Step-by-Step Migration β
1. π οΈ Install Nuxt I18n Micro
β
First, add Nuxt I18n Micro
to your Nuxt project:
npm install nuxt-i18n-micro
2. π Update Configuration β
Replace your existing nuxt-i18n
configuration in nuxt.config.ts
with the Nuxt I18n Micro
configuration. Hereβs an example:
Before (nuxt-i18n):
export default defineNuxtConfig({
modules: ['nuxt-i18n'],
i18n: {
locales: [
{ code: 'en', iso: 'en-US' },
{ code: 'fr', iso: 'fr-FR' },
],
defaultLocale: 'en',
vueI18n: './i18n.config.js',
},
})
After (Nuxt I18n Micro):
export default defineNuxtConfig({
modules: ['nuxt-i18n-micro'],
i18n: {
locales: [
{ code: 'en', iso: 'en-US', dir: 'ltr' },
{ code: 'fr', iso: 'fr-FR', dir: 'ltr' },
],
defaultLocale: 'en',
translationDir: 'locales',
meta: true,
},
})
3. ποΈ Reorganize Translation Files β
Move your translation files to the locales
directory. Ensure they are in JSON format and organized by locale. For example:
/locales
βββ /pages
β βββ /index
β β βββ en.json
β β βββ fr.json
β β βββ ar.json
β βββ /about
β β βββ en.json
β β βββ fr.json
β β βββ ar.json
βββ en.json
βββ fr.json
βββ ar.json
4. π Replace <nuxt-link>
with <NuxtLink>
β
If you are using <nuxt-link>
for navigation, replace it with <NuxtLink>
to ensure compatibility with the new module.
Before:
<nuxt-link :to="{ name: 'index' }">Home</nuxt-link>
After:
<NuxtLink :to="$localeRoute({ name: 'index' })">Home</NuxtLink>
<!-- or -->
<i18n-link :to="{ name: 'index' }">Home</i18n-link>
5. π οΈ Handle SEO Configurations β
Ensure that your SEO configurations are updated to take advantage of Nuxt I18n Micro
βs automatic meta tag generation. Remove any redundant SEO configurations that were specific to nuxt-i18n
.
6. π§ͺ Test Your Application β
After completing the migration steps, thoroughly test your application to ensure that all translations are loading correctly and that navigation between locales works as expected. Pay special attention to SEO-related tags and ensure that they are generated as intended.
π‘οΈ Common Issues and Troubleshooting β
β Translation Files Not Loading β
Ensure that your translation files are in the correct directory and follow the JSON format. Also, confirm that the translationDir
option in your configuration matches the location of your translation files.
β οΈ Route Not Found Errors β
Check that the routes are correctly set up in your application and that the locales
array in the configuration includes all necessary locale codes.
π·οΈ Missing SEO Tags β
If SEO tags are not being generated, verify that the meta
option is enabled in your configuration and that each locale has a valid iso
code.