@shelchin/svelte-i18n
Version:
The last Svelte i18n library you'll ever need. Type-safe, AI-powered, zero-config.
100 lines (99 loc) • 3.33 kB
TypeScript
/**
* Unified i18n initialization for both packages and applications
* Provides consistent API with optional type safety
*/
import type { I18nConfig, TranslationSchema, I18nInstance, InterpolationParams } from './core/types.js';
export interface UnifiedI18nConfig extends Partial<I18nConfig> {
/**
* The namespace for this i18n instance
* - Use 'app' or leave empty for main application
* - Use package name for libraries
*/
namespace?: string;
/**
* Whether this is the main application instance
* When true, other instances will inherit its configuration
*/
isMain?: boolean;
/**
* Built-in translations for this instance
*/
translations?: Record<string, TranslationSchema>;
}
/**
* Typed unified i18n instance with application-specific translation keys
*/
export interface TypedI18nInstance<TPath extends string = string> extends Omit<I18nInstance, 't'> {
t(key: TPath, params?: InterpolationParams): string;
setLocaleSync?: (locale: string) => void;
loadLanguageSync?: (locale: string, translations: any) => void;
}
/**
* Create a unified i18n configuration with optional type safety
* This function works identically for both packages and applications
*
* @example
* // Without type safety (simple usage)
* export const i18n = createI18n({
* namespace: 'app',
* isMain: true,
* translations
* });
*
* @example
* // With type safety (recommended)
* import type { I18nPath } from './types/i18n-generated';
*
* export const i18n = createI18n<I18nPath>({
* namespace: 'app',
* isMain: true,
* translations
* });
*
* @example
* // In a package
* import translations from './translations.js';
*
* export const i18n = createI18n({
* namespace: 'my-ui-lib',
* translations
* });
*/
export declare function createI18n<TPath extends string = string>(config?: UnifiedI18nConfig): TypedI18nInstance<TPath>;
/**
* Get an existing i18n instance by namespace with optional type safety
* @param namespace - The namespace to get, defaults to 'app'
*
* @example
* // Without type safety
* const i18n = getI18nInstance();
*
* @example
* // With type safety
* import type { I18nPath } from './types/i18n-generated';
* const i18n = getI18nInstance<I18nPath>();
*/
export declare function getI18nInstance<TPath extends string = string>(namespace?: string): TypedI18nInstance<TPath>;
/**
* Initialize i18n on the client side
* Call this in your root +layout.svelte
*
* @example
* // In +layout.svelte (both for packages and apps)
* <script>
* import { onMount } from 'svelte';
* import { initI18n } from '@shelchin/svelte-i18n';
* import { i18n } from './i18n';
*
* onMount(() => {
* initI18n(i18n);
* });
* </script>
*/
export declare function initI18n<TPath extends string = string>(instance: TypedI18nInstance<TPath> | I18nInstance): Promise<TypedI18nInstance<TPath> | I18nInstance>;
export type { I18nConfig, TranslationSchema } from './core/types.js';
export type I18n = ReturnType<typeof createI18n>;
export declare const createTypedUnifiedI18n: typeof createI18n;
export declare const getTypedUnifiedI18n: typeof getI18nInstance;
export declare const initTypedI18n: typeof initI18n;
export type TypedUnifiedI18nInstance<T extends string = string> = TypedI18nInstance<T>;