UNPKG

nuxt-i18n-micro

Version:

Nuxt I18n Micro is a lightweight, high-performance internationalization module for Nuxt, designed to handle multi-language support with minimal overhead, fast build times, and efficient runtime performance.

34 lines (33 loc) 1.3 kB
import { isNoPrefixStrategy } from "@i18n-micro/core"; import { defineNuxtPlugin, useRuntimeConfig, useRouter, useNuxtApp } from "#imports"; const isDev = process.env.NODE_ENV !== "production"; export default defineNuxtPlugin(async (nuxtApp) => { const config = useRuntimeConfig(); const i18nConfig = config.public.i18nConfig; const router = useRouter(); const { $getLocale, $getRouteName } = useNuxtApp(); const i18nHelper = nuxtApp.$i18n.helper; if (!i18nHelper) { if (isDev) { console.warn("[i18n] Helper is not available. Skipping hooks plugin."); } return; } const locale = $getLocale(); const routeName = $getRouteName(); await nuxtApp.callHook("i18n:register", (translations, selectedLocale) => { i18nHelper.mergeTranslation(selectedLocale ?? locale, routeName, translations, true); }, locale); router.beforeEach(async (to, from, next) => { if (to.path !== from.path || isNoPrefixStrategy(i18nConfig.strategy)) { const locale2 = $getLocale(to); const routeName2 = $getRouteName(to); await nuxtApp.callHook("i18n:register", (translations, selectedLocale) => { i18nHelper.mergeTranslation(selectedLocale ?? locale2, routeName2, translations, true); }, locale2); } if (next) { next(); } }); });