@shelchin/svelte-i18n
Version:
The last Svelte i18n library you'll ever need. Type-safe, AI-powered, zero-config.
60 lines (59 loc) • 2.55 kB
TypeScript
/**
* Extract locale from URL pathname and validate it
*/
interface I18nLocales {
locales: string[];
}
/**
* Extract language code from pathname if it exists and is valid
* @param pathname - The URL pathname (e.g., '/zh/about' or '/en-US/products')
* @returns The language code if found and valid, null otherwise
*
* @example
* extractLocaleFromPathname('/zh/about') // 'zh'
* extractLocaleFromPathname('/en-US/products') // 'en-US'
* extractLocaleFromPathname('/notlang/about') // null
* extractLocaleFromPathname('/about') // null
*/
export declare function extractLocaleFromPathname(pathname: string): string | null;
/**
* Extract locale from pathname and check if it's supported by the app
* @param pathname - The URL pathname
* @param i18n - The i18n instance to check supported locales
* @returns The language code if found, valid, and supported, null otherwise
*
* @example
* extractSupportedLocaleFromPathname('/zh/about', i18n) // 'zh' if supported
* extractSupportedLocaleFromPathname('/eng/about', i18n) // null if not supported
*/
export declare function extractSupportedLocaleFromPathname(pathname: string, i18n: I18nLocales): string | null;
/**
* Get the best locale based on priority:
* 1. URL pathname locale (if valid and supported)
* 2. Provided fallback locale (cookie/localStorage)
* 3. Default locale
*
* @param pathname - The URL pathname
* @param i18n - The i18n instance
* @param fallbackLocale - The fallback locale from cookie/localStorage
* @param defaultLocale - The default locale
* @param availableLocales - Optional array of all available locales (including auto-discovered)
* @returns The best locale based on priority
*
* @example
* getBestLocale('/zh/about', i18n, 'en', 'fr') // 'zh' if supported
* getBestLocale('/xyz/about', i18n, 'en', 'fr') // 'en' (fallback)
* getBestLocale('/about', i18n, null, 'fr') // 'fr' (default)
*/
export declare function getBestLocale(pathname: string, i18n: I18nLocales, fallbackLocale: string | null | undefined, defaultLocale: string, availableLocales?: string[]): string;
/**
* Check if a locale should be loaded based on pathname priority
* Returns true if the pathname locale takes precedence over current locale
*
* @param pathname - The URL pathname
* @param i18n - The i18n instance
* @param currentLocale - The current locale
* @returns True if pathname locale should override current locale
*/
export declare function shouldUsePathnameLocale(pathname: string, i18n: I18nLocales, currentLocale: string): boolean;
export {};