Quick Answer
To localize a React app:
- Install react-i18next:
npm install react-i18next i18next - Create translation JSON files for each language
- Configure i18next with your translations
- Use the
useTranslationhook in components - Add a language switcher
Prerequisites
- React 16.8+ (for hooks)
- Node.js 14+
- Basic React knowledge
Step 1: Install Dependencies
Terminalnpm install react-i18next i18next i18next-browser-languagedetector
| Package | Purpose |
|---|---|
| react-i18next | React bindings for i18next |
| i18next | Core internationalization framework |
| i18next-browser-languagedetector | Auto-detect user's language |
Step 2: Create Translation Files
Create a locales folder with JSON files for each language:
src/
locales/
en/
translation.json
es/
translation.json
fr/
translation.json
src/locales/en/translation.json:
JSON1{ 2 "welcome": { 3 "title": "Welcome to Our App", 4 "subtitle": "The best solution for your needs" 5 }, 6 "nav": { 7 "home": "Home", 8 "about": "About", 9 "contact": "Contact" 10 }, 11 "auth": { 12 "login": "Log In", 13 "signup": "Sign Up", 14 "logout": "Log Out" 15 } 16}
src/locales/es/translation.json:
JSON1{ 2 "welcome": { 3 "title": "Bienvenido a Nuestra App", 4 "subtitle": "La mejor solucion para tus necesidades" 5 }, 6 "nav": { 7 "home": "Inicio", 8 "about": "Acerca de", 9 "contact": "Contacto" 10 }, 11 "auth": { 12 "login": "Iniciar Sesion", 13 "signup": "Registrarse", 14 "logout": "Cerrar Sesion" 15 } 16}
Step 3: Configure i18next
Create src/i18n.js:
JavaScript1import i18n from 'i18next'; 2import { initReactI18next } from 'react-i18next'; 3import LanguageDetector from 'i18next-browser-languagedetector'; 4 5import enTranslation from './locales/en/translation.json'; 6import esTranslation from './locales/es/translation.json'; 7import frTranslation from './locales/fr/translation.json'; 8 9i18n 10 .use(LanguageDetector) 11 .use(initReactI18next) 12 .init({ 13 resources: { 14 en: { translation: enTranslation }, 15 es: { translation: esTranslation }, 16 fr: { translation: frTranslation }, 17 }, 18 fallbackLng: 'en', 19 interpolation: { 20 escapeValue: false, // React already escapes 21 }, 22 }); 23 24export default i18n;
Step 4: Import in App Entry
src/index.js:
JavaScript1import React from 'react'; 2import ReactDOM from 'react-dom/client'; 3import './i18n'; // Import i18n configuration 4import App from './App'; 5 6const root = ReactDOM.createRoot(document.getElementById('root')); 7root.render( 8 <React.StrictMode> 9 <App /> 10 </React.StrictMode> 11);
Step 5: Use Translations in Components
src/App.js:
JSX1import { useTranslation } from 'react-i18next'; 2 3function App() { 4 const { t } = useTranslation(); 5 6 return ( 7 <div> 8 <nav> 9 <a href="/">{t('nav.home')}</a> 10 <a href="/about">{t('nav.about')}</a> 11 <a href="/contact">{t('nav.contact')}</a> 12 </nav> 13 14 <main> 15 <h1>{t('welcome.title')}</h1> 16 <p>{t('welcome.subtitle')}</p> 17 </main> 18 19 <button>{t('auth.login')}</button> 20 </div> 21 ); 22} 23 24export default App;
Step 6: Add Language Switcher
JSX1import { useTranslation } from 'react-i18next'; 2 3function LanguageSwitcher() { 4 const { i18n } = useTranslation(); 5 6 const changeLanguage = (lng) => { 7 i18n.changeLanguage(lng); 8 }; 9 10 return ( 11 <div> 12 <button onClick={() => changeLanguage('en')}>English</button> 13 <button onClick={() => changeLanguage('es')}>Espanol</button> 14 <button onClick={() => changeLanguage('fr')}>Francais</button> 15 </div> 16 ); 17} 18 19export default LanguageSwitcher;
Advanced Features
Variables/Interpolation
Translation:
JSON{ "greeting": "Hello, {{name}}!" }
Component:
JSXt('greeting', { name: 'John' }) // "Hello, John!"
Pluralization
Translation:
JSON1{ 2 "items": "{{count}} item", 3 "items_plural": "{{count}} items" 4}
Component:
JSXt('items', { count: 1 }) // "1 item" t('items', { count: 5 }) // "5 items"
Nested Keys
JSX// Access nested keys with dot notation t('welcome.title') t('nav.home')
Trans Component (JSX in translations)
Translation:
JSON{ "terms": "I agree to the <link>Terms of Service</link>" }
Component:
JSX1import { Trans } from 'react-i18next'; 2 3<Trans i18nKey="terms" components={{ link: <a href="/terms" /> }}> 4 I agree to the <a href="/terms">Terms of Service</a> 5</Trans>
Managing Translations at Scale
As your app grows, managing JSON files becomes difficult:
- Multiple developers editing same files
- No translation memory
- Hard to track missing translations
- Manual export/import with translators
Solution: Use a Translation Management System
IntlPull automates React localization:
Terminal1# Pull latest translations 2npx @intlpullhq/cli download --output ./src/locales 3 4# Push new strings 5npx @intlpullhq/cli upload 6 7# Check translation status 8npx @intlpullhq/cli status
Benefits:
- AI-powered translation
- Collaboration with translators
- Translation memory
- Automatic sync with your repo
Common Issues and Solutions
Issue: Translations not updating
Solution: Make sure you imported i18n before App:
JavaScriptimport './i18n'; import App from './App';
Issue: Blank content on first load
Solution: Add Suspense fallback:
JSX1import { Suspense } from 'react'; 2 3<Suspense fallback="Loading..."> 4 <App /> 5</Suspense>
Issue: TypeScript errors
Solution: Install types and configure:
Terminalnpm install --save-dev @types/react-i18next
Best Practices
- Use namespaces for large apps to organize translations
- Keep keys semantic (e.g.,
auth.loginnotbutton1) - Don't concatenate strings - use interpolation
- Test with pseudo-localization before real translations
- Use a TMS like IntlPull for collaboration
Frequently Asked Questions
How do I add a new language?
- Create a new JSON file (e.g.,
locales/de/translation.json) - Add it to i18n config resources
- Add button to language switcher
How do I handle RTL languages?
Add dir attribute based on language:
JSX<html dir={i18n.language === 'ar' ? 'rtl' : 'ltr'}>
How do I lazy-load translations?
Use i18next-http-backend:
JavaScript1import Backend from 'i18next-http-backend'; 2 3i18n.use(Backend).init({ 4 backend: { 5 loadPath: '/locales/{{lng}}/{{ns}}.json', 6 }, 7});
Summary
To localize a React app:
- Install react-i18next and i18next
- Create translation JSON files
- Configure i18next
- Use
useTranslationhook - Add language switcher
- Use a TMS for scale
Ready to scale your localization? IntlPull automates React i18n with AI translation and seamless CLI integration.
