UNPKG

react-native-i18n-auto

Version:

Auto i18n tool for React Native with full TypeScript support

184 lines 5.81 kB
export declare const SUPPORTED_LANGUAGES: { readonly ar: "Arabic"; readonly zh: "Chinese"; readonly en: "English"; readonly fr: "French"; readonly de: "German"; readonly hi: "Hindi"; readonly id: "Indonesian"; readonly it: "Italian"; readonly ja: "Japanese"; readonly ko: "Korean"; readonly ms: "Malay"; readonly pt: "Portuguese"; readonly ru: "Russian"; readonly es: "Spanish"; readonly th: "Thai"; readonly vi: "Vietnamese"; readonly cn: "Chinese"; }; export type LanguageCode = keyof typeof SUPPORTED_LANGUAGES; /** * React Native Internationalization (i18n) Manager * * A powerful and efficient i18n manager for React Native applications. * Features include caching, memory optimization, and event handling. * * @example * // 1. Basic Usage * import { i18n } from 'react-native-i18n-auto'; * * * // Use translations * console.log(i18n.t('HELLO')); // 'Hello' * * // Change language * i18n.setLanguage('ko'); * console.log(i18n.t('HELLO')); // '안녕하세요' * * // 2. Event Listener Usage * const unsubscribe = i18n.onLanguageChange(() => { * console.log('Language changed to:', i18n.getLanguage()); * }); * * // 3. Performance Optimization * // Clear cache * i18n.clearCache(); * * // Optimize memory usage * i18n.optimize(); * * // 4. Language Support * const supported = i18n.getSupportedLanguages(); * const available = i18n.getAvailableLanguages(); */ declare class I18nManager { private static instance; private currentLang; private translations; private listeners; private translationProxy; private cache; private loadedFiles; private readonly CACHE_DURATION; private readonly MAX_CACHE_SIZE; private constructor(); private setupProxy; static getInstance(): I18nManager; private isValidLanguageCode; private manageCache; /** * Get translation for a given key in the current language * @param key - Translation key * @returns Translated string or the key if translation not found * @example * i18n.t('HELLO'); // Returns 'Hello' if language is English * // or using proxy * i18n.t.HELLO // Returns 'Hello' if language is English */ translate(key: string): string; get t(): Record<string, string> & ((key: string) => string); /** * Change the current language * @param lang - Target language code * @returns Boolean indicating if the language change was successful * @example * i18n.setLanguage('en'); // Changes language to English */ setLanguage(lang: LanguageCode): boolean; /** * Get the current language code * @returns Current language code * @example * const currentLang = i18n.getLanguage(); // Returns 'en' for English */ getLanguage(): LanguageCode; private availableLanguagesCache; /** * Get list of languages that have translations loaded * @returns Array of available languages with their codes and names * @example * const languages = i18n.getAvailableLanguages(); * // Returns [{ code: 'en', name: 'English' }, { code: 'ko', name: 'Korean' }] */ getAvailableLanguages(): Array<{ code: LanguageCode; name: string; }>; private static readonly SUPPORTED_LANGUAGES_LIST; /** * Get list of all supported languages * @returns Array of all supported languages with their codes and names * @example * const languages = i18n.getSupportedLanguages(); * // Returns [{ code: 'en', name: 'English' }, ...] */ getSupportedLanguages(): Array<{ code: LanguageCode; name: string; }>; /** * Register a callback for language change events * @param listener - Callback function to be called when language changes * @returns Unsubscribe function to remove the listener * @example * const unsubscribe = i18n.onLanguageChange(() => { * console.log('Language changed to:', i18n.getLanguage()); * }); */ onLanguageChange(listener: () => void): () => void; /** * Clear the translation cache * @example * i18n.clearCache(); // Clears all cached translations */ clearCache(): void; /** * Optimize memory usage by clearing cache and cleaning up listeners * @example * i18n.optimize(); // Optimizes memory usage */ optimize(): void; /** * Get list of loaded locale files * @returns Array of loaded locale file names */ getLoadedFiles(): string[]; /** * Check if a locale file is loaded * @param lang - Language code to check * @returns boolean indicating if the locale file is loaded */ isLocaleLoaded(lang: LanguageCode): boolean; /** * Set translations for multiple languages * @param locales - Object containing translations for each language * @example * i18n.setLocales({ * en: { HELLO: 'Hello' }, * ko: { HELLO: '안녕하세요' } * }); */ setLocales(locales: Partial<Record<LanguageCode, Record<string, unknown>>>): void; private processLocaleData; } export declare const i18n: I18nManager; export type I18nType = { t: (key: string) => string; setLanguage: (lang: LanguageCode) => boolean; getLanguage: () => LanguageCode; getSupportedLanguages: () => Array<{ code: LanguageCode; name: string; }>; getAvailableLanguages: () => Array<{ code: LanguageCode; name: string; }>; onLanguageChange: (listener: () => void) => () => void; setLocales: (locales: Partial<Record<LanguageCode, Record<string, unknown>>>) => void; clearCache: () => void; optimize: () => void; }; export {}; //# sourceMappingURL=setup.d.ts.map