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.
123 lines (122 loc) • 7.16 kB
TypeScript
import { BaseI18n, type BaseI18nOptions, type TranslationStorage } from '@i18n-micro/core';
import type { PathStrategy, ResolvedRouteLike } from '@i18n-micro/path-strategy';
import type { CleanTranslation, I18nRouteParams, MissingHandler, ModuleOptionsExtend, Params, TranslationKey, Translations } from '@i18n-micro/types';
import type { RouteLocationNamedRaw, RouteLocationNormalizedLoaded, RouteLocationRaw, RouteLocationResolved, RouteLocationResolvedGeneric, Router } from 'vue-router';
import type { GetLocaleFromRoute } from '../composables/useI18nLocale.js';
import { type LoadOptions } from './storage.js';
export interface RouteTranslationContext {
locale: string;
routeName: string;
}
export type GetRouteTranslationContext = (route?: unknown) => RouteTranslationContext;
export type LoadTranslationsChunk = (locale: string, routeName?: string) => Promise<Record<string, unknown>>;
export interface NuxtI18nOptions extends BaseI18nOptions {
}
export declare class NuxtI18n extends BaseI18n {
readonly storage: TranslationStorage;
private readonly contextSignal;
private cachedTranslations;
private pendingCleanState;
private currentLocale;
private currentRouteName;
private resolveRouteContext?;
constructor(options?: NuxtI18nOptions);
setRouteContextResolver(resolver: GetRouteTranslationContext): void;
getCacheKey(locale: string, routeName?: string): string;
getChunk(locale: string, routeName?: string): Record<string, unknown>;
setChunk(locale: string, routeName: string | undefined, data: Record<string, unknown>): void;
hasChunk(locale: string, routeName?: string): boolean;
mergeChunk(locale: string, routeName: string | undefined, data: Record<string, unknown>): Record<string, unknown>;
getLocale(): string;
getFallbackLocale(): string;
getRoute(): string;
getCurrentLocale(): string;
getCurrentRouteName(): string;
applySwitchContext(locale: string, routeName: string | undefined, data: Record<string, unknown>): void;
finishTransition(): void;
mergeTranslations(newTranslations: Translations): void;
loadPageTranslations(locale: string, routeName: string, translations: Translations): Promise<void>;
mergeTranslationAsync(locale: string, routeName: string, newTranslations: Translations, loadIfMissing: LoadTranslationsChunk): Promise<void>;
tForRoute(route: unknown): (key: string, params?: Params, defaultValue?: string | null) => CleanTranslation;
tsForRoute(route: unknown): (key: string, params?: Params, defaultValue?: string) => string;
protected touch(): void;
protected resolveLookup(key: TranslationKey, routeContext?: unknown): unknown | null;
protected resolveHas(key: TranslationKey): boolean;
protected getMissingContext(_routeContext?: unknown): {
locale: string;
routeName: string;
};
protected warnMissing(key: TranslationKey): void;
clearCache(): void;
}
export interface NuxtTranslationLoaderOptions {
i18n: NuxtI18n;
loadOptions: LoadOptions;
getSsrChunks: () => Record<string, Record<string, unknown>>;
setSsrChunk: (cacheKey: string, data: Record<string, unknown>) => void;
isDev?: boolean;
}
export declare class NuxtTranslationLoader {
private readonly options;
private readonly pendingLoads;
constructor(options: NuxtTranslationLoaderOptions);
loadFromCacheSync(locale: string, routeName?: string): Record<string, unknown> | null;
loadAsync(locale: string, routeName?: string): Promise<Record<string, unknown>>;
switchContext(locale: string, routeName?: string): Promise<void>;
}
export interface NuxtI18nPluginApiDeps {
i18n: NuxtI18n;
loader: NuxtTranslationLoader;
i18nStrategy: PathStrategy;
i18nConfig: ModuleOptionsExtend;
router: Router;
getCurrentLocale: (route?: ResolvedRouteLike) => string;
getEffectiveLocale: (route: unknown, getLocaleFromRoute: GetLocaleFromRoute) => string;
getPluginRouteName: (route: ResolvedRouteLike, locale: string) => string;
getRouteName: (route?: RouteLocationNormalizedLoaded | RouteLocationResolvedGeneric, locale?: string) => string;
i18nRouteParams: {
value: I18nRouteParams;
};
setLocale: (locale: string) => void;
isValidLocale: (locale: string) => boolean;
navigateTo: (to: string, options?: {
redirectCode?: number;
external?: boolean;
}) => unknown;
setMissingHandler: (handler: MissingHandler | null) => void;
}
export declare function createNuxtI18nPluginApi(deps: NuxtI18nPluginApiDeps): {
helper: {
mergeTranslation(locale: string, routeName: string, newTranslations: Translations, _force?: boolean): Promise<void>;
};
switchContext: (locale: string, routeName?: string) => Promise<void>;
provide: {
i18nStrategy: PathStrategy;
getLocale: (route?: RouteLocationNormalizedLoaded | RouteLocationResolvedGeneric) => string;
getLocaleName: () => string | null;
defaultLocale: () => string | undefined;
getLocales: () => import("@i18n-micro/types").Locale[];
getRouteName: (route?: RouteLocationNormalizedLoaded | RouteLocationResolvedGeneric, locale?: string) => string;
t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeContext?: unknown) => CleanTranslation;
ts: (key: string, params?: Params, defaultValue?: string, route?: RouteLocationNormalizedLoaded) => string;
_t: (route: RouteLocationNormalizedLoaded) => (key: string, params?: Params, defaultValue?: string | null) => CleanTranslation;
_ts: (route: RouteLocationNormalizedLoaded) => (key: string, params?: Params, defaultValue?: string) => string;
tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
tn: (value: number, options?: Intl.NumberFormatOptions) => string;
td: (value: Date | number | string, options?: Intl.DateTimeFormatOptions) => string;
tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
has: (key: TranslationKey, routeContext?: unknown) => boolean;
mergeTranslations: (newTranslations: Translations) => void;
switchLocaleRoute: (toLocale: string) => RouteLocationRaw;
clearCache: () => void;
switchLocalePath: (toLocale: string) => string;
switchLocale: (toLocale: string) => Promise<unknown>;
switchRoute: (route: RouteLocationNamedRaw | RouteLocationResolvedGeneric | string, toLocale?: string) => unknown;
localeRoute(to: RouteLocationNamedRaw | RouteLocationResolvedGeneric | string, locale?: string): RouteLocationResolved;
localePath(to: RouteLocationNamedRaw | RouteLocationResolvedGeneric | string, locale?: string): string;
setI18nRouteParams: (value: I18nRouteParams) => I18nRouteParams;
loadPageTranslations: (locale: string, routeName: string, translations: Translations) => Promise<void>;
setMissingHandler: (handler: MissingHandler | null) => void;
};
};
export type NuxtI18nPluginProvide = ReturnType<typeof createNuxtI18nPluginApi>['provide'];