From Hardcoded Strings to Full Localization: A Quickstart with react-i18next
Below is a practical, step-by-step outline of how a single developer can go from zero to having translatable strings in a React + react-i18next setup, along with notes on the extra tools or packages they’ll need to install and configure.
1. Prerequisites & Basic Tools
Most React developers will already have:
Beyond those, you’ll need:
2. Install Required Packages
From your project root, install the i18n libraries:
# Using npm:
npm install i18next react-i18next
# If you want automated key extraction:
npm install --save-dev i18next-parser
3. Initialize i18next
You’ll create an i18n configuration file—often named i18n.js or i18next.js—where you set up:
Example i18n.js:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
// Import or define your translation resources
import en from './locales/en.json';
import de from './locales/de.json';
i18n
??.use(initReactI18next) // Tell i18n to use react-i18next
??.init({
????resources: {
??????en: { translation: en },
??????de: { translation: de },
????},
????lng: 'en', // Default language
????fallbackLng: 'en',
????interpolation: {
??????escapeValue: false, // React already protects from XSS
????},
????debug: true, // Enable console logs for debugging (disable in production)
??});
export default i18n;
Directory structure might look like this:
src/
??i18n.js
??locales/
????en.json
????de.json
??...
Within en.json and de.json, you’ll store your translation keys:
// en.json
{
??"welcome": "Hello, welcome to our app!"
}
// de.json
{
??"welcome": "Hallo, willkommen in unserer App!"
}
4. Wrap Your App with the I18next Provider
In your top-level React entry point (e.g., App.js or index.js), make sure you import your i18n.js so it initializes. Then wrap your app with the I18nextProvider or, more commonly, rely on the suspense handling from react-i18next:
// index.js or App.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './i18n'; // Just importing will run the init code
ReactDOM.render(<App />, document.getElementById('root'));
For many setups, just importing the i18n.js is enough to initialize i18n globally. In some older versions or advanced setups, you might explicitly do:
import { I18nextProvider } from 'react-i18next';
import i18n from './i18n';
领英推荐
ReactDOM.render(
??<I18nextProvider i18n={i18n}>
????<App />
??</I18nextProvider>,
??document.getElementById('root')
);
5. Replace Hardcoded Strings with t() Calls
Wherever you have user-facing text, you’ll import the translation hook from react-i18next:
import React from 'react';
import { useTranslation } from 'react-i18next';
function Greeting() {
??const { t } = useTranslation();
??return <h1>{t('welcome')}</h1>;
}
export default Greeting;
No more hardcoded strings—they go into en.json, de.json, etc.
6. Automate Key Extraction with i18next-parser
If you don’t want to manually hunt for strings, you can use i18next-parser. It scans your code for t('someKey') usage and updates/creates your JSON resource files. Basic steps:
module.exports = {
??locales: ['en', 'de'],
??output: 'src/locales/$LOCALE.json',
??defaultNamespace: 'translation',
??keySeparator: false,
??namespaceSeparator: false,
??// Additional options...
};
3. Add an npm script in your package.json:
{
??"scripts": {
????"extract-i18n": "i18next 'src/**/*.{js,jsx}'"
??}
}
4. Run npm run extract-i18n to parse your source files and populate or update src/locales/en.json, src/locales/de.json, etc.
7. Switch Locales at Runtime
If your app allows the user to select a language, you can do so like this:
import React from 'react';
import { useTranslation } from 'react-i18next';
function LanguageSwitcher() {
??const { i18n } = useTranslation();
??const changeLanguage = (lang) => {
????i18n.changeLanguage(lang); // e.g., 'en' or 'de'
??};
??return (
????<div>
??????<button onClick={() => changeLanguage('en')}>English</button>
??????<button onClick={() => changeLanguage('de')}>Deutsch</button>
????</div>
??);
}
export default LanguageSwitcher;
Under the hood, i18n.changeLanguage('de') reloads the appropriate translations and re-renders components.
8. Handle Dates, Numbers, and Other Formatting
For advanced date/number formatting, you can either:
But the key idea is to keep the logic about how to display data in a localized fashion. For example, if you do:
const date = new Date();
const formatted = new Intl.DateTimeFormat(i18n.language).format(date);
It will automatically format the date according to the user’s current locale (e.g., en, de, etc.).
9. Test & Verify
Once these steps are complete, you’ll have a functional i18n setup in your React app using react-i18next. All further development can follow this pattern—no more hardcoded strings, and everything is primed for easy translation.