<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: rahul-haveri</title>
    <description>The latest articles on Forem by rahul-haveri (@rahulrhdev).</description>
    <link>https://forem.com/rahulrhdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1260051%2Fa5abb438-49fe-4761-9723-631348981b5b.jpeg</url>
      <title>Forem: rahul-haveri</title>
      <link>https://forem.com/rahulrhdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rahulrhdev"/>
    <language>en</language>
    <item>
      <title>Localize Your React Application: A Pragmatic guide</title>
      <dc:creator>rahul-haveri</dc:creator>
      <pubDate>Sun, 19 Jan 2025 13:18:44 +0000</pubDate>
      <link>https://forem.com/rahulrhdev/localize-your-react-application-a-pragmatic-guide-2n6m</link>
      <guid>https://forem.com/rahulrhdev/localize-your-react-application-a-pragmatic-guide-2n6m</guid>
      <description>&lt;p&gt;Having localized applications to 10+ languages, I understand that localizing your React application can be overwhelming. It feels like you have to make many code changes all over the application, which is true, but it can be easily done if we understand the i18next package. Without further delay, let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation:
&lt;/h2&gt;

&lt;p&gt;We can install i18next using npm/yarn as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# npm
$ npm install i18next --save

# yarn
$ yarn add i18next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initializing i18Next:
&lt;/h2&gt;

&lt;p&gt;Create a file 18n.js under your 'src' folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  .use(LanguageDetector) // Detects user language.
  .use(initReactI18next) // Initializes the react-i18next plugin.
  .init({
    supportedLngs: ['en', 'es', 'fr'], // Languages we're supporting.
    fallbackLng: "en", // Fallback language if user's language isn't supported.
    detection: {
      order: ['cookie', 'localStorage'], // Order of language detection.
      caches: ['cookie'], // Cache the detected language in cookies.
    },
  });

export default i18n;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we are specifying which plugins to use and configure different options. The above configuration is critical for switching between the languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a translation file for each language:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In en/translation.json
{
  "welcomeText": "Welcome to the Website!"
}

// In es/translation.json
{
  "welcomeText": "Bienvenido al sitio Web!"
}

// In fr/translation.json
{
  "welcomeText": "Bienvenue sur le site."
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrating i18next in Your Application
&lt;/h2&gt;

&lt;p&gt;In our &lt;code&gt;src/index.js&lt;/code&gt;, import the i18n configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './i18n';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our components, we can access translations by using useTranslation, which is a hook from react-i18next that gives access to the t function (for translation) and the i18n instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { useTranslation } from 'react-i18next';

function App() {
  const { t, i18n } = useTranslation();

  const changeLanguage = (lng) =&amp;gt; {
    i18n.changeLanguage(lng);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;{t('welcomeText')}&amp;lt;/h2&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; changeLanguage('en')}&amp;gt;English&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; changeLanguage('es')}&amp;gt;Espanol&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; changeLanguage('fr')}&amp;gt;French&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we are using t(‘welcomeText’) which fetches the translated text for the ‘welcomeText’ key based on the current language and changeLanguage is a function that changes the app’s current language when the corresponding button is clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;This setup can be used to scale to more languages and features, making your application linguistically accessible.&lt;/p&gt;

&lt;p&gt;Let me know if you have any questions, Let's connect &lt;a href="//linkedin.com/in/rahul-haveri"&gt;Rahul Haveri LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>learning</category>
      <category>frontend</category>
      <category>react</category>
    </item>
  </channel>
</rss>
