A powerful internationalization (i18n) tool for React Native with full TypeScript support and automatic translation capabilities.
- π Full TypeScript Support - Complete type safety with proper type definitions and interfaces
- π¦ Automatic Key Extraction - Seamless extraction through custom Babel plugin
- π Multiple Language Support - Support for 16 major languages out of the box
- π± React Native Optimized - Built specifically for React Native applications
- β‘οΈ High Performance - Efficient caching system and memory optimization
- π Event System - Language change event listeners for real-time updates
- π Easy Configuration - Simple setup and intuitive API
- π― Proxy Support - Convenient dot notation access for translations
- πΎ Smart Caching - Automatic cache management with configurable duration
# Using npm
npm install --save-dev react-native-i18n-auto
npm install @react-native-async-storage/async-storage
# Using yarn
yarn add -D react-native-i18n-auto
yarn add @react-native-async-storage/async-storageAdd the following to your tsconfig.json:
{
"compilerOptions": {
"resolveJsonModule": true
}
}Create an i18n-auto.config.json file in your project root:
{
"defaultLanguage": "en",
"supportedLanguages": ["en", "ko", "ja"],
"localeDir": "locales",
"sourceDir": ["app", "src"],
"ignore": [
"node_modules/**",
"dist/**",
"build/**",
".git/**",
"tests/**",
"**/*.test.*",
"**/*.spec.*"
]
}Run the following command in your project root:
npx react-native-i18n-auto runThe system will automatically transform your code and generate necessary translation files.
import { Text } from 'react-native';
export default function App() {
return <Text>Hello</Text>;
}import { i18n } from 'react-native-i18n-auto'; //auto generate i18n instance
import { Text } from 'react-native';
export default function App() {
return <Text>{i18n.t('HELLO')}</Text>;
}A translation file (en.json) will be automatically created:
{
"HELLO": "Hello"
}π‘ Tip: You can use AI tools like ChatGPT to help translate these keys into different languages.
Set up your languages in your app's entry point (_layout.tsx or App.tsx):
import { i18n } from 'react-native-i18n-auto';
import en from './locales/en.json';
import ko from './locales/ko.json';
import ja from './locales/ja.json';
// Import other language files as needed
i18n.setLocales({
en,
ko,
ja,
// Add other languages as needed
});There are multiple ways to use translations in your code:
// 1. Function call
i18n.t('HELLO'); // Returns: "Hello"
// 2. Proxy notation (recommended)
i18n.t.HELLO; // Returns: "Hello"
// 3. Nested keys
i18n.t('GREETINGS.HELLO'); // Returns: "Hello" from nested structureTo properly manage language changes across your app, you should set up a global state management solution:
import React, { createContext, useContext, useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { i18n, LanguageCode } from 'react-native-i18n-auto';
type LanguageContextType = {
currentLanguage: LanguageCode;
changeLanguage: (lang: LanguageCode) => void;
};
const LanguageContext = createContext<LanguageContextType | undefined>(undefined);
const STORAGE_KEY = 'USER_LANGUAGE';
export const LanguageProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [currentLanguage, setCurrentLanguage] = useState<LanguageCode>(i18n.getLanguage());
useEffect(() => {
AsyncStorage.getItem(STORAGE_KEY).then((savedLang) => {
if (savedLang && i18n.setLanguage(savedLang as LanguageCode)) {
setCurrentLanguage(savedLang as LanguageCode);
}
});
}, []);
const changeLanguage = (lang: LanguageCode) => {
if (i18n.setLanguage(lang)) {
setCurrentLanguage(lang);
AsyncStorage.setItem(STORAGE_KEY, lang);
}
};
return <LanguageContext.Provider value={{ currentLanguage, changeLanguage }}>{children}</LanguageContext.Provider>;
};
export const useLanguage = () => {
const context = useContext(LanguageContext);
if (!context) {
throw new Error('useLanguage must be used within a LanguageProvider');
}
return context;
};Wrap your app with the LanguageProvider:
// App.tsx or equivalent
import { LanguageProvider } from './languageContext';
export default function App() {
return (
<LanguageProvider>
<YourAppContent />
</LanguageProvider>
);
}Now you can change languages anywhere in your app using the useLanguage hook:
import { useLanguage } from './languageContext';
function LanguageSelector() {
const { currentLanguage, changeLanguage } = useLanguage();
return (
<View>
<Button
title="English"
onPress={() => changeLanguage('en')}
disabled={currentLanguage === 'en'}
/>
<Button
title="νκ΅μ΄"
onPress={() => changeLanguage('ko')}
disabled={currentLanguage === 'ko'}
/>
<Button
title="ζ₯ζ¬θͺ"
onPress={() => changeLanguage('ja')}
disabled={currentLanguage === 'ja'}
/>
</View>
);
}Contributions are welcome! Feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
You can subscribe to language changes:
// Subscribe to language changes
const unsubscribe = i18n.onLanguageChange(() => {
console.log('Current language:', i18n.getLanguage());
});
// Cleanup when component unmounts
useEffect(() => {
return () => unsubscribe();
}, []);The library includes built-in performance optimization features:
// Clear translation cache
i18n.clearCache();
// Optimize memory usage
i18n.optimize();Additional utility functions for language management:
// Get all supported languages
const supported = i18n.getSupportedLanguages();
// Get languages that have translations loaded
const available = i18n.getAvailableLanguages();The library provides strong TypeScript support with proper type definitions:
import { i18n, LanguageCode, I18nType } from 'react-native-i18n-auto';
// Type-safe language codes
const setKorean = () => i18n.setLanguage('ko' as LanguageCode);
// Type-safe translation keys (when using with TypeScript)
type TranslationKeys = keyof typeof import('./locales/en.json');
const translate = (key: TranslationKeys) => i18n.t(key);π‘ Tip: You can also integrate this with your backend to sync user language preferences across devices.