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.

43 lines (42 loc) 1.26 kB
import { defineNuxtPlugin, useNuxtApp } from "#imports"; import { watch, unref, computed, onUnmounted } from "vue"; const normalizeLocales = (locales) => { if (Array.isArray(locales)) { return locales.reduce((acc, locale) => { acc[locale] = {}; return acc; }, {}); } else if (typeof locales === "object" && locales !== null) { return locales; } return {}; }; export default defineNuxtPlugin(() => { const defineI18nRoute = async (routeDefinition) => { const { $getLocale, $mergeGlobalTranslations } = useNuxtApp(); let currentLocale = computed(() => $getLocale()); const normalizedLocales = normalizeLocales(routeDefinition.locales); const updateTranslations = () => { const localeValue = unref(currentLocale); if (localeValue && normalizedLocales[localeValue]) { $mergeGlobalTranslations?.(normalizedLocales[localeValue]); } }; updateTranslations(); if (import.meta.client) { let stopWatcher = watch(currentLocale, updateTranslations); onUnmounted(() => { if (stopWatcher) { stopWatcher(); stopWatcher = null; currentLocale = null; } }); } }; return { provide: { defineI18nRoute } }; });