← Back to React Course | Chapter 13: Advanced React & Architecture | Lesson 13 of 14

react-i18next से i18n

react-i18next के साथ Internationalization (i18n) आपके app के बगल में एक translator खड़ा होने जैसा है, हर label को visitor की अपनी language में automatically swap करते हुए।
Syntax
markup
const { t, i18n } = useTranslation();

<p>{t("key")}</p>
<p>{t("keyWithVariable", { name: value })}</p>

i18n.changeLanguage("languageCode");

Hardcoded Text के बजाय Translation Keys क्यों Use करें

JSX में directly English text लिखना आपके component को permanently एक language से tie कर देता है। इसके बजाय एक translation key उपयोग करना (जैसे t('welcome.title')) एक language file से actual displayed text lookup करता है, इसलिए एक नई supported language जोड़ने का मतलब है एक नई translation file जोड़ना, component code को बिल्कुल न छूना।

Note: Keys को hierarchically organize करें, जैसे 'header.nav.home', बड़ी translation files को section के हिसाब से organized रखने के लिए।
Warning: यह एक npm package है जिसे setup चाहिए (translation files, provider initialization) — यह इस CDN-only sandbox में run नहीं हो सकता।

उदाहरण: Why Use Translation Keys Instead of Hardcoded Text

markup
// Run in your local React project (npm install required)
import { useTranslation } from 'react-i18next';

function Welcome() {
  const { t } = useTranslation();
  return <h1>{t('welcome.title')}</h1>;
}
// en.json: { "welcome": { "title": "Welcome!" } }
// fr.json: { "welcome": { "title": "Bienvenue!" } }

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Current Language Switch करना

useTranslation hook एक i18n instance भी return करता है जिसमें एक changeLanguage function होता है। इसे call करना app-wide active language update करता है, और t function उपयोग करने वाला हर component automatically newly translated text के साथ re-render होता है।

Note: Chosen language को persist करें (जैसे localStorage में) ताकि यह page reloads के across remembered रहे, theme persistence जैसा।
Warning: Target language की translation file actually configure करना भूल जाने का मतलब है changeLanguage succeed होता है, लेकिन t() calls missing-key placeholders या default language पर fall back करते हैं।

उदाहरण: Switching the Current Language

markup
// Run in your local React project (npm install required)
import { useTranslation } from 'react-i18next';

function LanguageSwitcher() {
  const { i18n } = useTranslation();
  return (
    <div>
      <button onClick={() => i18n.changeLanguage('en')}>English</button>
      <button onClick={() => i18n.changeLanguage('fr')}>Français</button>
    </div>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Pluralization और Variables Handle करना

i18next translated strings में variables interpolate करना (जैसे एक username) और एक count value के आधार पर automatically singular/plural translation forms के बीच चुनना support करता है, दोनों extra options के साथ उसी t() function के through handled।

Note: Pluralization के लिए t() को एक count option pass करें — i18next इसकी value के आधार पर automatically singular या plural translation key चुनता है।
Warning: Pluralization के लिए manually strings concatenate करना (जैसे count + ' item' + (count !== 1 ? s : '')) उन languages के लिए टूट जाता है जिनके English के simple singular/plural से ज़्यादा complex plural rules हैं — i18next को इसे handle करने दें।

उदाहरण: Handling Pluralization and Variables

markup
// Run in your local React project (npm install required)
import { useTranslation } from 'react-i18next';

function Cart({ itemCount }) {
  const { t } = useTranslation();
  return <p>{t('cart.items', { count: itemCount })}</p>;
}
// en.json: { "cart": { "items_one": "{{count}} item", "items_other": "{{count}} items" } }

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. JSX में directly English text hardcode करना translation keys उपयोग करने के बजाय, बाद में translation को painful बनाते हुए।
  2. App को i18next provider/initialization में wrap करना भूल जाना, इसलिए t() function के पास translate करने के लिए कुछ नहीं होता।
  3. Pluralization correctly handle न करना — '1 item' vs '2 items' को specific i18next plural key handling चाहिए, simple string concatenation नहीं।
चैप्टर सारांश
  • react-i18next React apps को कई languages में translate करने के लिए एक popular library है।
  • Text translation KEYS (जैसे 'welcome.title') के through referenced होता है, current language के actual string में resolved।
  • useTranslation hook एक t function देता है जो components में translated text lookup करने के लिए throughout उपयोग होता है।
  • Language switching i18next की language-changing API से handle होता है, translated text को automatically re-render करते हुए।
ब्राउज़र सपोर्ट

npm install react-i18next i18next चाहिए — इस sandbox में CDN के through available नहीं; React 16.8+ के साथ काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.