Nuxt I18n Micro is a fast, simple, and lightweight internationalization (i18n) module for Nuxt. Despite its compact size, it’s designed with large projects in mind, offering significant performance improvements over traditional i18n solutions like nuxt-i18n. The module was built from the ground up to be highly efficient, focusing on minimizing build times, reducing server load, and shrinking bundle sizes.
The Nuxt I18n Micro module was created to address critical performance issues found in the original nuxt-i18n module, particularly in high-traffic environments and projects with large translation files. Key issues with nuxt-i18n include:
To showcase the efficiency of Nuxt I18n Micro, we conducted tests under identical conditions. Both modules were tested with a 10MB translation file on the same hardware.
undefinedNuxt I18n:
undefinedNuxt I18n Micro:
undefinedNuxt I18n:
undefinedNuxt I18n Micro:
These results clearly demonstrate that Nuxt I18n Micro significantly outperforms the original module in every critical area.
Nuxt I18n Micro is designed for large-scale projects, focusing on performance and efficiency.dev mode if not present.Install the module in your Nuxt application with:
npm install nuxt-i18n-micro
Then, add it to your nuxt.config.ts:
export default defineNuxtConfig({
modules: [
'nuxt-i18n-micro',
],
i18n: {
locales: [
{ code: 'en', iso: 'en-US', dir: 'ltr' },
{ code: 'fr', iso: 'fr-FR', dir: 'ltr' },
{ code: 'ar', iso: 'ar-SA', dir: 'rtl' },
],
defaultLocale: 'en',
translationDir: 'locales',
meta: true,
},
})
That’s it! You’re now ready to use Nuxt I18n Micro in your Nuxt app.
Translations are organized into global and page-specific files:
/locales
/pages
/index
en.json
fr.json
ar.json
/about
en.json
fr.json
ar.json
en.json
fr.json
ar.json
locales/{locale}.json (e.g., locales/en.json). Used for common texts shared across multiple pages, such as menus.locales/pages/{routeName}/{locale}.json (e.g., locales/pages/index/en.json). These are used for translations specific to individual pages.$getLocale()Returns the current locale code.
const locale = $getLocale()
$getLocales()Returns an array of all available locales configured in the module.
const locales = $getLocales()
$t(key: string, params?: Record<string, any>, defaultValue?: string)Fetches a translation for the given key. Optionally interpolates parameters into the translation.
const welcomeMessage = $t('welcome', { username: 'Alice', unreadCount: 5 })
$tc(key: string, count: number, defaultValue?: string)Fetches a pluralized translation for the given key based on the count.
const appleCountMessage = $tc('apples', 10)
$switchLocale(locale: string)Switches to the given locale and redirects the user to the appropriate localized route.
$switchLocale('fr')
$localeRoute(to: RouteLocationRaw, locale?: string): RouteLocationRawGenerates a localized route object based on the target route.
const localizedRoute = $localeRoute({ name: 'index' })
$mergeTranslations(newTranslations: Translations)Merges new translations into the existing translation cache for the current route and locale.
$mergeTranslations({
welcome: 'Bienvenue, {username}!'
})
import { useNuxtApp } from '#imports'
const { $getLocale, $switchLocale, $getLocales, $localeRoute, $t } = useNuxtApp()
import { useI18n } from '#imports'
const { $getLocale, $switchLocale, $getLocales, $localeRoute, $t } = useI18n()
// or
const i18n = useI18n()
<template>
<div>
<p>{{ $t('key2.key2.key2.key2.key2') }}</p>
<p>Current Locale: {{ $getLocale() }}</p>
<div>
{{ $t('welcome', { username: 'Alice', unreadCount: 5 }) }}
</div>
<div>
{{ $tc('apples', 10) }}
</div>
<!-- Ссылки для переключения локалей -->
<div>
<button
v-for="locale in $getLocales()"
:key="locale"
:disabled="locale === $getLocale()"
@click="$switchLocale(locale.code)"
>
Switch to {{ locale.code }}
</button>
</div>
<div>
<NuxtLink :to="$localeRoute({ name: 'index' })">
Go to Index
</NuxtLink>
</div>
</div>
</template>
<script setup>
import { useI18n } from '#imports'
const { $getLocale, $switchLocale, $getLocales, $localeRoute, $t, $tc } = useI18n()
</script>
The module accepts the following options in the Nuxt configuration:
code, and optionally, iso and dir (for RTL/LTR).
locales: [
{ code: 'en', iso: 'en-US', dir: 'ltr' },
{ code: 'fr', iso: 'fr-FR', dir: 'ltr' },
{ code: 'ar', iso: 'ar-SA', dir: 'rtl' }
]
alternate links).'en').'locales').true, automatically detects the user’s preferred language and redirects accordingly.The Nuxt I18n Micro module provides an efficient way to manage internationalization (i18n) in your Nuxt application, with a focus on performance and simplicity. One of the key features of this module is how it handles the loading of locale-specific translations. Below is an explanation of how this process works.
In Nuxt I18n Micro, translations are loaded dynamically based on the user’s selected locale. The module uses a specific route pattern (/_locales/:page/:locale/data.json) to fetch the translation files for each page and locale. This approach is designed to minimize the initial load time and only load the necessary translations for the current page and locale.
The translations are organized into JSON files located in the locales directory. These files are split into:
locales/{locale}.json (e.g., locales/en.json). These files contain translations shared across the entire application, such as menu items or common UI elements.locales/pages/{routeName}/{locale}.json (e.g., locales/pages/index/en.json). These files contain translations specific to individual pages.To accommodate the dynamic nature of translation loading, Nuxt I18n Micro extends the default routing configuration by adding locale-specific routes. This means that for each page, a localized version of the route is generated. For example, if your application has an index page and supports English (en) and French (fr), the following routes would be generated:
/en/index/fr/indexThis structure allows the module to load the appropriate translations based on the user’s selected locale.
Given the way Vite and Nuxt handle modules, the Nuxt I18n Micro module uses a special route (/_locales/:page/:locale/data.json) to fetch translations. This is necessary because, during the build process, Vite may not bundle all translation files directly into the application. Instead, the module dynamically loads these files from the server as needed.
When a user navigates to a page, the module will:
_locales route to fetch the required translation file.To further optimize performance, Nuxt I18n Micro supports caching and pre-rendering of translation files:
Nuxt I18n Micro offers a highly efficient, minimalist approach to internationalization in Nuxt applications. By focusing on performance and simplicity, it provides a powerful alternative to heavier, more complex i18n solutions. Whether you’re building a small website or a large-scale application, Nuxt I18n Micro helps you manage multilingual content with ease.
For more details and updates, visit the Nuxt I18n Micro GitHub repository.