Skip to content

virtual:i18n Module

The virtual:i18n module is auto-generated by the createI18n integration. It provides all i18n helpers and config values as named exports.

Type declaration

Add this to your src/env.d.ts:

declare module 'virtual:i18n' {
export const t: (locale: string, key: string) => string;
export const localePath: (locale: string, path: string) => string;
export const switchLocalePath: (currentPath: string, targetLocale: string) => string;
export const getLocaleFromPath: (pathname: string) => string;
export const localized: <T>(field: Record<string, T>, locale: string) => T;
export const getLocalizedSlug: (category: string, canonicalSlug: string, locale: string) => string;
export const getCanonicalSlug: (category: string, localizedSlug: string, locale: string) => string | undefined;
export const config: import('@otrodigital/astro-i18n-next').LocaleConfig;
export const defaultLocale: string;
export const locales: string[];
export const localeLabels: Record<string, string>;
export const localeHtmlLang: Record<string, string>;
}

Functions

t(locale, key)

Translate a key for a given locale using i18next. Falls back to defaultLocale for missing keys.

t('en', 'nav.about'); // "About"
t('es', 'nav.about'); // "Acerca de"
t('es', 'missing.key'); // Falls back to English value

Supports dot-notation for nested keys. HTML is not escaped (safe for set:html).

localePath(locale, path)

Translate a canonical path to a locale-specific URL.

localePath('en', '/about/'); // "/about/"
localePath('es', '/about/'); // "/es/sobre/"
localePath('es', '/saunas/model-165/'); // "/es/saunas/modelo-165/"

switchLocalePath(currentPath, targetLocale)

Convert a path from its current locale to a target locale.

switchLocalePath('/es/sobre/', 'en'); // "/about/"
switchLocalePath('/about/', 'es'); // "/es/sobre/"
switchLocalePath('/es/saunas/modelo-165/', 'en'); // "/saunas/model-165/"

getLocaleFromPath(pathname)

Extract the locale from a URL pathname.

getLocaleFromPath('/es/sobre/'); // "es"
getLocaleFromPath('/about/'); // "en" (default)

localized(field, locale)

Extract a locale-specific value from a multilingual field object. Falls back to defaultLocale.

localized({ en: 'Hello', es: 'Hola' }, 'es'); // "Hola"
localized({ en: 'Hello' }, 'es'); // "Hello" (fallback)

getLocalizedSlug(category, canonicalSlug, locale)

Translate a canonical slug to a locale-specific slug.

getLocalizedSlug('saunas', 'model-165', 'es'); // "modelo-165"

getCanonicalSlug(category, localizedSlug, locale)

Reverse lookup — find the canonical slug from a localized one.

getCanonicalSlug('saunas', 'modelo-165', 'es'); // "model-165"

Config values

config

Type: LocaleConfig

The full locale configuration object.

defaultLocale

Type: string

The default locale code (e.g., 'en').

locales

Type: string[]

Array of all supported locale codes.

localeLabels

Type: Record<string, string>

Human-readable locale names (e.g., { en: 'English', es: 'Español' }).

localeHtmlLang

Type: Record<string, string>

HTML lang attribute values per locale.