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.

73 lines (72 loc) 3.13 kB
import { SERVER_CC_KEY } from "@i18n-micro/hmr/cache-keys"; import { isEnabledLocale } from "@i18n-micro/utils/active-locales"; import { CacheControl } from "@i18n-micro/utils/cache-control"; import { normalizeConfiguredLocales } from "@i18n-micro/utils/merge-source"; import { fetchTranslationPayloadFromHost } from "@i18n-micro/utils/payload-fetch"; import { resolveTranslationPayloadPage } from "@i18n-micro/utils/payload-url"; import { resolveI18nConfigWithRuntimeOverrides } from "@i18n-micro/utils/runtime-config"; import { loadSourceTranslationsFromStorage } from "@i18n-micro/utils/source-loader"; import { useStorage } from "nitropack/runtime"; import { getI18nConfig } from "#i18n-internal/strategy"; import { getI18nPrivateConfig } from "#i18n-internal/config"; function getServerCacheControl() { const g = globalThis; if (!g[SERVER_CC_KEY]) { const cfg = resolveI18nConfigWithRuntimeOverrides(getI18nConfig()); g[SERVER_CC_KEY] = new CacheControl({ maxSize: cfg.cacheMaxSize ?? 0, ttl: cfg.cacheTtl ?? 0 }); } return g[SERVER_CC_KEY]; } const ASSETS_PREFIX = "assets:i18n"; import { toTranslations } from "@i18n-micro/utils/normalize"; export async function loadTranslationsFromServer(locale, routeName) { const cc = getServerCacheControl(); const cacheKey = `${locale}:${routeName}`; const cached = cc.get(cacheKey); if (cached) { return cached; } const config = resolveI18nConfigWithRuntimeOverrides(getI18nConfig()); const privateConfig = getI18nPrivateConfig(); if (!isEnabledLocale(config.locales, locale)) { const empty = { data: {}, json: "{}" }; cc.set(cacheKey, empty); return empty; } const storage = useStorage(); const routesLocaleLinks = config.routesLocaleLinks || {}; const resolvedPage = resolveTranslationPayloadPage(routeName, routesLocaleLinks); const normalizedPage = resolvedPage.replace(/\//g, ":"); if (privateConfig.apiBaseServerHost) { const data2 = await fetchTranslationPayloadFromHost( { apiBaseUrl: config.apiBaseUrl, apiBaseServerHost: privateConfig.apiBaseServerHost, dateBuild: config.dateBuild }, locale, resolvedPage, $fetch ); const json2 = JSON.stringify(data2).replace(/</g, "\\u003c"); const entry2 = { data: data2, json: json2 }; cc.set(cacheKey, entry2); return entry2; } if (config.translationPayloadMode === "source") { const data2 = await loadSourceTranslationsFromStorage(storage, { locale, pageName: resolvedPage, locales: normalizeConfiguredLocales(config.locales), globalFallbackLocale: config.fallbackLocale, disablePageLocales: config.disablePageLocales }); const json2 = JSON.stringify(data2).replace(/</g, "\\u003c"); const entry2 = { data: data2, json: json2 }; cc.set(cacheKey, entry2); return entry2; } const key = `${ASSETS_PREFIX}:pages:${normalizedPage}:${locale}.json`; const loaded = await storage.getItem(key); const data = toTranslations(loaded); const json = JSON.stringify(data).replace(/</g, "\\u003c"); const entry = { data, json }; cc.set(cacheKey, entry); return entry; }