<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Balikis Oyeleye on Medium]]></title>
        <description><![CDATA[Stories by Balikis Oyeleye on Medium]]></description>
        <link>https://medium.com/@balikis?source=rss-a5d8ab85d1eb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ey0N-vx06WpoXNKpijaozw.jpeg</url>
            <title>Stories by Balikis Oyeleye on Medium</title>
            <link>https://medium.com/@balikis?source=rss-a5d8ab85d1eb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:16:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@balikis/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing Internationalization (i18n) in a Vite + React Application]]></title>
            <link>https://balikis.medium.com/implementing-internationalization-i18n-in-a-vite-react-application-41cfc53b0ccf?source=rss-a5d8ab85d1eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/41cfc53b0ccf</guid>
            <category><![CDATA[vites]]></category>
            <category><![CDATA[i18next]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Balikis Oyeleye]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 05:56:04 GMT</pubDate>
            <atom:updated>2026-11-10T08:08:58.041Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CJw3U7WAggyCo31kyfK_rg.png" /><figcaption>Image generated with Chatgpt</figcaption></figure><h3>Introduction</h3><p>Imagine building an application that only supports English. What happens when a user who does not understand English tries to use it?</p><p>That is where internationalization (i18n) comes in. In this article, I’ll walk you through implementing internationalization in a React + Vite application using i18next and react-i18next. We’ll also explore how to organize translations and make them type-safe with TypeScript.</p><h3>What is Internationalization (i18n)?</h3><p><strong>Internationalization</strong> (often abbreviated as <strong>i18n</strong>, because there are 18 letters between the “i” and the “n”) is the process of designing and developing software to adapt to multiple countries, languages, and cultures without major redesign.</p><p>To implement this in our React application, we’ll use <strong>i18next</strong>, a JavaScript internationalization library that helps manage translations, switch between languages and provide fallback translations when needed. We’ll also use <strong>react-i18next</strong>, a library that integratesi18next with React.</p><h3>Setting Up Internationalization in React + Vite</h3><p>Now that we understand internationalization and how i18next manages translations, let’s implement it in a React + Vite application using TypeScript.</p><p>For this example, we will support two languages: English and French.</p><h4>Step-by-step integration guide</h4><p><strong>Step 1: Install the required dependencies</strong></p><p>Using npm:</p><pre>npm install i18next react-i18next</pre><p>Using yarn:</p><pre>yarn add i18next react-i18next</pre><ul><li><strong>i18next</strong>: The core internationalization library that manages translations, language switching and fallback translations.</li><li><strong>react-i18next</strong>: Integrates i18next with React and provides hooks such as useTranslation() for accessing translations within components.</li></ul><p><strong>Step 2: Organize the translation files</strong></p><p>Before configuring i18next, let’s establish the folder structure for the translation files.</p><p>Inside the src/lib directory, create an i18n folder with the following structure:</p><pre>src/<br>└── lib/<br>    └── i18n/<br>        ├── locales/<br>        │   ├── en/<br>        │   │   ├── auth.json<br>        │   │   └── common.json<br>        │   └── fr/<br>        │       ├── auth.json<br>        │       └── common.json<br>        └── index.ts</pre><p>Here’s what each folder and file represents:</p><ul><li><strong>i18n folder</strong>: Contains the i18n config and translation files</li><li><strong>locales folder</strong>: Stores the translation files the app supports</li><li><strong>en and fr folders</strong>: Language codes that represent English and French, respectively.</li><li><strong>auth.json:</strong> Contains translations related to user authentication, such as login and registration screens.</li><li><strong>common.json</strong>: Contains translations shared across multiple features, such as <strong>Save, Cancel, Delete and Continue.</strong></li><li>index.ts: Contains the i18n config</li></ul><p>You might be wondering why we’re using auth.json or common.json instead of a generic file like translation.json .</p><p>That brings us to an important concept in i18n: namespaces.</p><p><strong>Understanding Namespaces</strong></p><p>A namespace is a way of organizing translations into logical groups.</p><p>Imagine building an application with authentication, dashboard and settings modules. Keeping all translations in a single JSON file would become difficult to maintain as the application grows.</p><p>Instead, we can separate translations based on the features they belong to.</p><p>For example:</p><pre>locales/<br>├── en/<br>│   ├── auth.json<br>│   ├── dashboard.json<br>│   ├── settings.json<br>│   └── common.json<br>└── fr/<br>    ├── auth.json<br>    ├── dashboard.json<br>    ├── settings.json<br>    └── common.json</pre><p>Each JSON file represents a namespace.</p><p>This approach makes translations easier to organize and maintain, especially when working on applications with multiple features and languages.</p><p>For now, we’ll only work with auth and common namespaces.</p><p><strong>Step 3: Add Translation content</strong></p><p>Now let’s define our translation</p><p>Inside src/lib/i18n/locales/en/auth.json</p><pre><br>{<br>  &quot;login&quot;: {<br>    &quot;title&quot;: &quot;Sign in to your account&quot;,<br>    &quot;welcome&quot;: &quot;Welcome back, {{name}}!&quot;,<br>    &quot;submit&quot;: &quot;Sign in&quot;,<br>    &quot;sign-up-prompt&quot;: &quot;Don&#39;t have an account? &lt;link&gt;Sign up&lt;/link&gt;&quot;,<br>    &quot;validation&quot;: {<br>      &quot;remaining-attempt_zero&quot;: &quot;You have no login attempts remaining&quot;,<br>      &quot;remaining-attempt_one&quot;: &quot;You have {{count}} login attempt remaining&quot;,<br>      &quot;remaining-attempt_other&quot;: &quot;You have {{count}} login attempts remaining&quot;<br>    }<br>  }<br>}</pre><p>Inside src/lib/i18n/locales/en/common.json</p><pre>{<br>  &quot;email-label&quot;: &quot;Email&quot;,<br>  &quot;password-label&quot;: &quot;Password&quot;<br>}</pre><p>Inside src/lib/i18n/locales/fr/auth.json</p><pre><br>{<br>  &quot;login&quot;: {<br>    &quot;title&quot;: &quot;Connectez-vous à votre compte&quot;,<br>    &quot;welcome&quot;: &quot;Bon retour, {{name}} !&quot;,<br>    &quot;submit&quot;: &quot;Se connecter&quot;,<br>    &quot;sign-up-prompt&quot;: &quot;Vous n&#39;avez pas de compte ? &lt;link&gt;Inscrivez-vous&lt;/link&gt;&quot;,<br>    &quot;validation&quot;: {<br>     &quot;remaining-attempt_zero&quot;: &quot;Il ne vous reste aucune tentative de connexion&quot;,<br>      &quot;remaining-attempt_one&quot;: &quot;Il vous reste {{count}} tentative de connexion&quot;,<br>      &quot;remaining-attempt_other&quot;: &quot;Il vous reste {{count}} tentatives de connexion&quot;<br>    }<br>  }<br>}</pre><p>Inside src/lib/i18n/locales/fr/common.json</p><pre>{<br>  &quot;email-label&quot;: &quot;Adresse e-mail&quot;,<br>  &quot;password-label&quot;: &quot;Mot de passe&quot;<br>}</pre><p>Notice that both files have the same structure and translation keys. The key login.title remains the same across languages. Only the translated value changes.</p><p>This consistency allows i18next to retrieve the appropriate translation based on the currently selected language.</p><p><strong>Step 4: Initialize i18next</strong></p><p>Now that our translations and supported languages are defined, let’s initialize i18next.</p><p>Inside src/lib/i18n/index.ts, add:</p><pre>import i18n, { type Resource } from &quot;i18next&quot;;<br>import { initReactI18next } from &quot;react-i18next&quot;;<br><br>// Supported languages.<br>const DEFAULT_LANGUAGE = &quot;en&quot;;<br><br>const supportedLanguages = {<br>  en: &quot;English&quot;,<br>  fr: &quot;Français&quot;,<br>} as const;<br><br>const supportedLanguageCodes = Object.keys(supportedLanguages);<br><br>// Automatically discover translation files.<br>const modules = import.meta.glob&lt;{<br>  default: Record&lt;string, unknown&gt;;<br>}&gt;(&quot;./locales/*/*.json&quot;, {<br>  eager: true,<br>});<br><br>// Organize translations by language and namespace.<br>const resources: Resource = {};<br><br>for (const path in modules) {<br>  const match = path.match(<br>    /\.\/locales\/([^/]+)\/(.+)\.json$/<br>  );<br><br>  if (!match) continue;<br><br>  const [, language, namespace] = match;<br><br>  (resources[language] ??= {})[namespace] =<br>    modules[path].default;<br>}<br><br>// initialize i18next.<br>i18n.use(initReactI18next).init({<br>  resources,<br><br>  lng: DEFAULT_LANGUAGE,<br><br>  supportedLngs: supportedLanguageCodes,<br><br>  fallbackLng: DEFAULT_LANGUAGE,<br><br>  defaultNS: &quot;auth&quot;,<br><br>  interpolation: {<br>    escapeValue: false,<br>  },<br><br>  debug:<br>    import.meta.env.DEV &amp;&amp;<br>    import.meta.env.MODE !== &quot;test&quot;,<br>});<br><br>export default i18n;</pre><p>There’s quite a bit happening here, so let’s break it down.</p><p><em>Automatically Discovering Translation Files</em></p><p>One approach to registering translations is to import each JSON file manually:</p><pre>import userAuthEn from &quot;./locales/en/auth.json&quot;;<br>import userAuthFr from &quot;./locales/fr/auth.json&quot;;</pre><p>We could then register them like this:</p><pre>const resources = {<br>  en: {<br>    &quot;auth&quot;: userAuthEn,<br>  },<br>  fr: {<br>    &quot;auth&quot;: userAuthFr,<br>  },<br>};</pre><p>This works perfectly for a small application.</p><p>However, imagine supporting ten languages across twenty features. Every time we introduce a new language or namespace, we’d need to update our imports and resource configuration.</p><p>To avoid this repetitive process, we’ll use Vite’s import.meta.glob() feature.</p><pre>const modules = import.meta.glob&lt;{<br>  default: Record&lt;string, unknown&gt;;<br>}&gt;(&quot;./locales/*/*.json&quot;, {<br>  eager: true,<br>});</pre><p>The pattern:</p><pre>./locales/*/*.json</pre><p>tells Vite to find all JSON files located one directory below locales.</p><p>For example, it matches:</p><pre>./locales/en/auth.json<br>./locales/fr/auth.json<br>./locales/en/common.json<br>./locales/fr/common.json</pre><p>The eager: true option tells Vite to import all matching files immediately rather than loading them asynchronously when requested.</p><p>The resulting modules object looks approximately like this:</p><pre>{<br>  &quot;./locales/en/auth.json&quot;: {<br>    default: {<br>      login: {<br>        title: &quot;Sign in to your account&quot;<br>      }<br>    }<br>  },<br><br>  &quot;./locales/fr/auth.json&quot;: {<br>    default: {<br>      login: {<br>        title: &quot;Connectez-vous à votre compte&quot;<br>      }<br>    }<br>  }<br>}</pre><p>Now that Vite has discovered our translation files, we need to organize them into the structure i18next expects.</p><p><strong>Building the Translation Resources</strong></p><p>i18next expects translations to be grouped by language and namespace.</p><p>For example:</p><pre>  en: {<br>    &quot;auth&quot;: {<br>      login: {<br>        title: &quot;Sign in to your account&quot;<br>      }<br>    }<br>  },<br><br>  fr: {<br>    &quot;auth&quot;: {<br>      login: {<br>        title: &quot;Connectez-vous à votre compte&quot;<br>      }<br>    }<br>  }<br>}</pre><p>To construct this structure automatically, we loop through the discovered files:</p><pre>const resources: Resource = {};<br><br>for (const path in modules) {<br>  const match = path.match(<br>    /\.\/locales\/([^/]+)\/(.+)\.json$/<br>  );<br><br>  if (!match) continue;<br><br>  const [, language, namespace] = match;<br><br>  (resources[language] ??= {})[namespace] =<br>    modules[path].default;<br>}</pre><p>Let’s look at the important parts.</p><p>First, we extract the language and namespace from the file path.</p><p>Given:</p><pre>./locales/fr/auth.json</pre><p>this regular expression:</p><pre>/\.\/locales\/([^/]+)\/(.+)\.json$/</pre><p>extracts:</p><pre>language = &quot;fr&quot;;<br>namespace = &quot;auth&quot;;</pre><p>Next, we register the translations:</p><pre>(resources[language] ??= {})[namespace] =<br>  modules[path].default;</pre><p>The ??= operator initializes the language object if it doesn&#39;t already exist.</p><p>For example, if resources.fr is undefined, it creates an empty object before adding the namespace.</p><p>After processing all the files, we have the resources object that i18next needs.</p><p>The benefit is that <strong>we no longer need to manually import and register every translation file</strong>.</p><p>When we introduce a new namespace, we can simply create the corresponding JSON files inside our language folders.</p><h4>Initializing i18next</h4><p>Now let’s examine the initialization configuration.</p><pre>i18n.use(initReactI18next).init({<br>  // ...<br>});</pre><p>The initReactI18next plugin connects i18next to React, allowing us to access translations through hooks such as useTranslation.</p><p><strong>1. </strong><strong>resources</strong></p><pre>resources,</pre><p>This contains all the translations discovered and organized earlier.</p><p><strong>2. </strong><strong>lng</strong></p><pre>lng: DEFAULT_LANGUAGE,</pre><p>This specifies the language the application uses when it first initializes.</p><p>Since our default language is English, the application initially displays English translations.</p><p><strong>3. </strong><strong>supportedLngs</strong></p><pre>supportedLngs: supportedLanguageCodes,</pre><p>This tells i18next which languages are supported.</p><p>In our case:</p><pre>[&quot;en&quot;, &quot;fr&quot;]</pre><p><strong>4. </strong><strong>fallbackLng</strong></p><pre>fallbackLng: DEFAULT_LANGUAGE,</pre><p>What happens if a translation is missing in French?</p><p>Rather than displaying an untranslated key, i18next can fall back to the corresponding English translation.</p><p>For example, suppose the English file contains:</p><pre>{<br>  &quot;login&quot;: {<br>    &quot;forgotPassword&quot;: &quot;Forgot password?&quot;<br>  }<br>}</pre><p>but the French file doesn’t contain login.forgotPassword.</p><p>When the active language is French, i18next can display:</p><pre>Forgot password?</pre><p>because English is our fallback language.</p><p><strong>5. </strong><strong>defaultNS</strong></p><pre>defaultNS: &quot;auth&quot;,</pre><p>This sets auth as our default namespace.</p><p>A namespace tells i18next which group of translations to look in.</p><p>With auth as the default namespace, we can access its translation keys without explicitly specifying the namespace every time.</p><p>However, when working with multiple features, explicitly specifying the namespace can make the code easier to understand.</p><p><strong>6. </strong><strong>interpolation</strong></p><pre>interpolation: {<br>  escapeValue: false,<br>},</pre><p>Interpolation allows us to insert dynamic values into translations.</p><p>For example:</p><pre>{<br>  &quot;greeting&quot;: &quot;Welcome, {{name}}!&quot;<br>}</pre><p>We can provide a name dynamically:</p><pre>t(&quot;greeting&quot;, { name: &quot;Jane&quot; });</pre><p>Which produces:</p><pre>Welcome, Jane!</pre><p>By default, i18next can escape interpolated values to prevent unsafe HTML from being inserted.</p><p>However, React already escapes text values when rendering them, so we set escapeValue to false to avoid unnecessary double escaping.</p><p>This assumes we’re rendering translations normally through React, rather than inserting them as raw HTML.</p><p><strong>7. </strong><strong>debug</strong></p><pre>debug:<br>  import.meta.env.DEV &amp;&amp;<br>  import.meta.env.MODE !== &quot;test&quot;,</pre><p>This enables i18next debugging logs during development and disables them in production and test mode.</p><p>These logs can help identify missing translations and configuration issues.</p><p><strong>Step 5: Initialize i18next in React</strong></p><p>Now that i18next is configured, we need to ensure the configuration is loaded when our application starts.<br>Open src/index.tsx and import the configuration:</p><pre>import React from &quot;react&quot;;<br>import ReactDOM from &quot;react-dom/client&quot;;<br><br>import App from &quot;./App&quot;;<br>import &quot;./lib/i18n&quot;;<br><br>ReactDOM.createRoot(<br>  document.getElementById(&quot;root&quot;)!<br>).render(<br>  &lt;React.StrictMode&gt;<br>    &lt;App /&gt;<br>  &lt;/React.StrictMode&gt;<br>);</pre><p>Notice this import:</p><pre>import &quot;./lib/i18n&quot;;</pre><p>We aren’t importing a value from the module.</p><p>Instead, we’re importing it for its side effect: initializing i18next.</p><p>Once the module executes, our translation configuration becomes available throughout the React application.</p><p><strong>Step 6: Use Translations in a React Component</strong></p><p>Now let’s put everything together. Assuming this is our login UI:</p><pre><br><br>import { useState } from &quot;react&quot;;<br><br>const Login = () =&gt; {<br>  const [language, setLanguage] = useState(&quot;en&quot;);<br><br>  const name = &quot;Jane&quot;;<br>  const remainingAttempts = 3;<br><br>  return (<br>    &lt;div&gt;<br>      &lt;header&gt;<br>        &lt;select<br>          value={language}<br>          onChange={(e) =&gt; setLanguage(e.target.value)}<br>          aria-label=&quot;Select language&quot;<br>        &gt;<br>          &lt;option value=&quot;en&quot;&gt;English&lt;/option&gt;<br>          &lt;option value=&quot;fr&quot;&gt;Français&lt;/option&gt;<br>        &lt;/select&gt;<br>      &lt;/header&gt;<br><br>      &lt;h1&gt;Sign in to your account&lt;/h1&gt;<br>      &lt;p&gt;Welcome back, {name}!&lt;/p&gt;<br><br>      &lt;form onSubmit={(e) =&gt; e.preventDefault()}&gt;<br>        &lt;label htmlFor=&quot;email&quot;&gt;Email&lt;/label&gt;<br>        &lt;input id=&quot;email&quot; type=&quot;email&quot; /&gt;<br><br>        &lt;label htmlFor=&quot;password&quot;&gt;Password&lt;/label&gt;<br>        &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;<br><br>        &lt;button type=&quot;submit&quot;&gt;Sign in&lt;/button&gt;<br>      &lt;/form&gt;<br><br>      &lt;p&gt;<br>        You have {remainingAttempts}{&quot; &quot;}<br>        {remainingAttempts === 1<br>          ? &quot;login attempt&quot;<br>          : &quot;login attempts&quot;}{&quot; &quot;}<br>        remaining<br>      &lt;/p&gt;<br><br>      &lt;p&gt;<br>        Don&#39;t have an account?{&quot; &quot;}<br>        &lt;a href=&quot;/signup&quot;&gt;Sign up&lt;/a&gt;<br>      &lt;/p&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default Login;</pre><p>Updated React component with internationalization:</p><pre><br><br>import { Trans, useTranslation } from &quot;react-i18next&quot;;<br><br>const Login = () =&gt; {<br>  const { t, i18n } = useTranslation([&quot;auth&quot;, &quot;common&quot;]);<br><br>  const name = &quot;Jane&quot;;<br>  const remainingAttempts = 3;<br><br>  return (<br>    &lt;div&gt;<br>      &lt;header&gt;<br>        &lt;select<br>          value={i18n.resolvedLanguage ?? &quot;en&quot;}<br>          onChange={(e) =&gt; i18n.changeLanguage(e.target.value)}<br>          aria-label=&quot;Select language&quot;<br>        &gt;<br>          &lt;option value=&quot;en&quot;&gt;English&lt;/option&gt;<br>          &lt;option value=&quot;fr&quot;&gt;Français&lt;/option&gt;<br>        &lt;/select&gt;<br>      &lt;/header&gt;<br><br>      &lt;h1&gt;{t(&quot;login.title&quot;)}&lt;/h1&gt;<br><br>      {/* Interpolation */}<br>      &lt;p&gt;{t(&quot;login.welcome&quot;, { name })}&lt;/p&gt;<br><br>      &lt;form onSubmit={(e) =&gt; e.preventDefault()}&gt;<br>        &lt;label htmlFor=&quot;email&quot;&gt;<br>          {t(&quot;common:email-label&quot;)}<br>        &lt;/label&gt;<br>        &lt;input id=&quot;email&quot; type=&quot;email&quot; /&gt;<br><br>        &lt;label htmlFor=&quot;password&quot;&gt;<br>          {t(&quot;common:password-label&quot;)}<br>        &lt;/label&gt;<br>        &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;<br><br>        &lt;button type=&quot;submit&quot;&gt;<br>          {t(&quot;login.submit&quot;)}<br>        &lt;/button&gt;<br>      &lt;/form&gt;<br><br>      {/* Pluralization */}<br>      &lt;p&gt;<br>        {t(&quot;login.validation.remaining-attempt&quot;, {<br>          count: remainingAttempts,<br>        })}<br>      &lt;/p&gt;<br><br>      {/* React component inside a translation */}<br>      &lt;p&gt;<br>        &lt;Trans<br>          i18nKey=&quot;login.sign-up-prompt&quot;<br>          ns=&quot;auth&quot;<br>          components={{<br>            link: &lt;a href=&quot;/signup&quot; /&gt;,<br>          }}<br>        /&gt;<br>      &lt;/p&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default Login;</pre><p><strong>Understanding the Updated Component</strong></p><p>Now that we’ve updated our login component to support internationalization, let’s look at how the different parts work.</p><p><strong>1. Accessing Translations with </strong><strong>useTranslation</strong></p><pre>const { t, i18n } = useTranslation([&quot;auth&quot;, &quot;common&quot;]);</pre><p>The useTranslation hook, provided by react-i18next, gives us access to two important things:</p><ul><li>t: A function used to retrieve translations based on the active language.</li><li>i18n: An instance that allows us to manage the active language.</li></ul><p>We’re passing auth and common because our component uses translations from both namespaces.</p><p>Since auth is the first namespace, we can access its translations directly:</p><pre>t(&quot;login.title&quot;);</pre><p>For translations in the common namespace, we explicitly specify the namespace:</p><pre>t(&quot;common:email-label&quot;);</pre><p><strong>2. Switching Between Languages</strong></p><pre>&lt;select<br>  value={i18n.resolvedLanguage ?? &quot;en&quot;}<br>  onChange={(e) =&gt; i18n.changeLanguage(e.target.value)}<br>  aria-label=&quot;Select language&quot;<br>&gt;<br>  &lt;option value=&quot;en&quot;&gt;English&lt;/option&gt;<br>  &lt;option value=&quot;fr&quot;&gt;Français&lt;/option&gt;<br>&lt;/select&gt;</pre><p>Previously, our language dropdown only updated a local state variable without changing the displayed text.</p><p>Now, we’re using i18n.changeLanguage() to update the application&#39;s active language.</p><p>When a user selects French, i18next switches to the French translations, and components using useTranslation automatically update to display the translated content.</p><p><strong>3. Handling Dynamic Values with Interpolation</strong></p><pre>&lt;p&gt;{t(&quot;login.welcome&quot;, { name })}&lt;/p&gt;</pre><p>Remember the {{name}} placeholder we defined in our translation files?</p><p>By passing name to the t() function, i18next replaces the placeholder with its actual value.</p><p>For example, if name is &quot;Jane&quot;:</p><ul><li><strong>English:</strong> Welcome back, Jane!</li><li><strong>French:</strong> Bon retour, Jane!</li></ul><p>This allows us to insert dynamic values into translated text without manually combining strings.</p><p><strong>4. Handling Pluralization</strong></p><pre>t(&quot;login.validation.remaining-attempt&quot;, {<br>  count: remainingAttempts,<br>});</pre><p>Different languages have different rules for singular and plural forms.</p><p>Instead of manually checking whether remainingAttempts is 0, 1, or greater than 1, we pass the value using the count option.</p><p>i18next uses this value to select the appropriate translation.</p><p>For example, with our English translations:</p><ul><li><strong>0:</strong> You have no login attempts remaining</li><li><strong>1:</strong> You have 1 login attempt remaining</li><li><strong>3:</strong> You have 3 login attempts remaining</li></ul><p>This makes pluralization easier to manage, especially when supporting languages with more complex pluralization rules.</p><p><strong>5. Rendering React Elements with </strong><strong>Trans</strong></p><pre>&lt;Trans<br>  i18nKey=&quot;login.sign-up-prompt&quot;<br>  ns=&quot;auth&quot;<br>  components={{<br>    link: &lt;a href=&quot;/signup&quot; /&gt;,<br>  }}<br>/&gt;</pre><p>Sometimes, our translated text needs to contain React elements such as links.</p><p>For example, our English translation contains:</p><pre>{<br>  &quot;sign-up-prompt&quot;: &quot;Don&#39;t have an account? &lt;link&gt;Sign up&lt;/link&gt;&quot;<br>}</pre><p>The Trans component allows us to replace the &lt;link&gt; placeholder with an actual React element.</p><p>In our example, it renders the sign-up text as a clickable link while allowing the surrounding text to change based on the selected language.</p><p>With these changes, our login component can display translations dynamically without creating separate components for English and French.</p><h3>Making Translations Type-Safe with TypeScript</h3><p>So far, we’ve configured i18next to manage translations and used the useTranslation hook to display translated content in our React components.</p><p>However, there’s still one problem: how do we know we’re using valid translation keys?</p><p>Consider this example:</p><pre>const { t } = useTranslation(&quot;auth&quot;);</pre><pre>return &lt;h1&gt;{t(&quot;login.titel&quot;)}&lt;/h1&gt;;</pre><p>Notice the typo in login.titel. The correct key is login.title.</p><p>Without proper TypeScript configuration, this mistake might go unnoticed until runtime. Instead of displaying the expected translation, i18next may display the missing translation key.</p><p>We can improve this by extending i18next’s TypeScript definitions so that TypeScript understands the translation keys available in our application.</p><p><strong>Step 1: Create a TypeScript Declaration File</strong></p><p>Inside our i18n directory, create a file called i18next.d.ts.</p><p>Our folder structure now looks like this:</p><pre>src/<br>└── lib/<br>    └── i18n/<br>        ├── locales/<br>        │   ├── en/<br>        │   │   ├── auth.json<br>        │   │   └── common.json<br>        │   └── fr/<br>        │       ├── auth.json<br>        │       └── common.json<br>        ├── index.ts<br>        └── i18next.d.ts</pre><p>We’ll use our English translation files as the reference for our TypeScript definitions.</p><p><strong>Step 2: Extend i18next’s Type Definitions</strong></p><p>Inside i18next.d.ts, add:</p><pre>import &quot;i18next&quot;;</pre><pre>import auth from &quot;./locales/en/auth.json&quot;;<br>import common from &quot;./locales/en/common.json&quot;;</pre><pre>declare module &quot;i18next&quot; {<br>  interface CustomTypeOptions {<br>    defaultNS: &quot;auth&quot;;</pre><pre>    resources: {<br>      auth: typeof auth;<br>      common: typeof common;<br>    };<br>  }<br>}</pre><p>Let’s break this down.</p><p>The declare module &quot;i18next&quot; statement allows us to extend the TypeScript definitions provided by i18next.</p><p>The CustomTypeOptions interface tells i18next about our application&#39;s translation structure.</p><p><strong>defaultNS</strong> specifies our default namespace:</p><pre>defaultNS: &quot;auth&quot;;</pre><p><strong>resources</strong> defines the namespaces and their corresponding translation structures:</p><pre>resources: {<br>  auth: typeof auth;<br>  common: typeof common;<br>};</pre><p>By using typeof, TypeScript derives the translation keys directly from our English JSON files.</p><p>This means we don’t have to manually create TypeScript interfaces for every translation key.</p><p><strong>Step 3: Using Type-Safe Translations</strong></p><p>With our TypeScript declarations configured, let’s revisit our login component.</p><pre>import { useTranslation } from &quot;react-i18next&quot;;</pre><pre>export function Login() {<br>  const { t } = useTranslation(&quot;auth&quot;);</pre><pre>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;{t(&quot;login.title&quot;)}&lt;/h1&gt;<br>      &lt;button&gt;{t(&quot;login.submit&quot;)}&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>TypeScript now understands that login.title and login.submit are valid translation keys in the auth namespace.</p><p>If we accidentally write:</p><pre>t(&quot;login.titel&quot;);</pre><p>TypeScript reports an error because the key doesn’t exist.</p><p>Another benefit is <strong>autocomplete</strong>. When we start typing a translation key, our editor can suggest available keys based on our JSON files.</p><p>This makes working with translations easier as the application grows.</p><p><strong>Step 4: Working with Multiple Namespaces</strong></p><p>Remember that we also created a common.json file for shared translations.</p><p>Our common.json contains:</p><pre>{<br>  &quot;email-label&quot;: &quot;Email&quot;,<br>  &quot;password-label&quot;: &quot;Password&quot;<br>}</pre><p>We can access these translations by specifying the common namespace:</p><pre>import { useTranslation } from &quot;react-i18next&quot;;</pre><pre>export function LoginFields() {<br>  const { t } = useTranslation(&quot;common&quot;);</pre><pre>  return (<br>    &lt;div&gt;<br>      &lt;label htmlFor=&quot;email&quot;&gt;<br>        {t(&quot;email-label&quot;)}<br>      &lt;/label&gt;</pre><pre>      &lt;input id=&quot;email&quot; type=&quot;email&quot; /&gt;</pre><pre>      &lt;label htmlFor=&quot;password&quot;&gt;<br>        {t(&quot;password-label&quot;)}<br>      &lt;/label&gt;</pre><pre>      &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>We can also use multiple namespaces in the same component:</p><pre>const { t } = useTranslation([&quot;auth&quot;, &quot;common&quot;]);</pre><pre>t(&quot;login.title&quot;);</pre><pre>t(&quot;common:email-label&quot;);</pre><p>Here, auth is the first namespace, so it becomes the default namespace for the hook. We explicitly specify common when accessing shared translations.</p><h3>What About import.meta.glob()?</h3><p>Earlier, we used Vite’s import.meta.glob() to automatically discover our translation files.</p><p>You might be wondering: if Vite already discovers our translation files, why are we importing JSON files again in i18next.d.ts?</p><p>The reason is that <strong>automatic resource discovery and TypeScript type safety solve different problems</strong>.</p><p>import.meta.glob() discovers translation files and registers them with i18next.</p><p>However, TypeScript doesn’t automatically infer every translation key from the dynamically constructed resources object.</p><p>That’s why we’re using our English translation files as the reference for our TypeScript definitions.</p><p>There is a tradeoff here: when introducing a new namespace, we still need to update i18next.d.ts to include it.</p><p>For larger applications, we could automate the generation of translation types from our JSON files. For now, this approach gives us type safety without introducing additional tooling.</p><p>It’s also important to understand that TypeScript checks the translation keys defined in our reference language. It doesn’t automatically verify that every French translation exists.</p><h3>Enforcing Internationalization with ESLint</h3><p>We’ve made our translation keys type-safe, but there’s another problem we haven’t addressed.</p><p><strong>What happens when a developer forgets to use translations altogether?</strong></p><p>Consider this component:</p><pre>export function Login() {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Sign in to your account&lt;/h1&gt;<br>      &lt;button&gt;Sign in&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>This works, but the text is hardcoded in English.</p><p>Even if a user switches to French, the heading and button will still display English text.</p><p>As an application grows, it’s easy for developers to introduce hardcoded strings accidentally.</p><p>One way to prevent this is by using ESLint to detect untranslated strings.</p><p><strong>Step 1: Install an ESLint Plugin</strong></p><p>We can use eslint-plugin-i18next to identify hardcoded strings in our React components.</p><p>Using npm:</p><pre>npm install -D eslint-plugin-i18next</pre><p>Using yarn:</p><pre>yarn add -D eslint-plugin-i18next</pre><p>The plugin provides a rule called no-literal-string, which helps detect text that should be translated.</p><p><strong>Step 2: Configure ESLint</strong></p><p>If your Vite project uses ESLint’s flat configuration, you can add the following configuration to your existing eslint.config.js or eslint.config.mjs:</p><pre>import i18next from &quot;eslint-plugin-i18next&quot;;</pre><pre>export default [<br>  {<br>    files: [&quot;src/**/*.{js,jsx,ts,tsx}&quot;],</pre><pre>    plugins: {<br>      i18next,<br>    },</pre><pre>    rules: {<br>      &quot;i18next/no-literal-string&quot;: [<br>        &quot;warn&quot;,<br>        {<br>          markupOnly: true,<br>        },<br>      ],<br>    },<br>  },<br>];</pre><p>Here, we’re enabling the no-literal-string rule with two important options.</p><p><strong>warn</strong> tells ESLint to report hardcoded strings as warnings rather than errors.</p><p><strong>markupOnly: true</strong> focuses the rule on JSX markup, helping reduce false positives from strings used in application logic.</p><p>This is a configuration fragment to merge into an existing ESLint setup, not a replacement for the TypeScript and React rules that Vite may already provide.</p><p><strong>Step 3: Detect Hardcoded Text</strong></p><p>Consider this component:</p><pre>export function Login() {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Sign in to your account&lt;/h1&gt;<br>      &lt;button&gt;Sign in&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>With the rule enabled, ESLint can warn us about hardcoded JSX text.</p><p>We can fix this by using our translations:</p><pre>import { useTranslation } from &quot;react-i18next&quot;;</pre><pre>export function Login() {<br>  const { t } = useTranslation(&quot;auth&quot;);</pre><pre>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;{t(&quot;login.title&quot;)}&lt;/h1&gt;<br>      &lt;button&gt;{t(&quot;login.submit&quot;)}&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>Now, our component retrieves its text from the translation files.</p><p>When the active language is English, the heading displays:</p><pre>Sign in to your account</pre><p>When the active language is French, it displays:</p><pre>Connectez-vous à votre compte</pre><h3>Best Practices for Managing Translations</h3><p>Now that we’ve configured internationalization, added TypeScript support, and introduced ESLint checks, let’s look at some practices that can help us maintain translations as our application grows.</p><p><strong>1. Use Meaningful Translation Keys</strong></p><p>Translation keys should describe their purpose rather than repeat the translated text.</p><p>For example, our authentication translations are organized like this:</p><pre>{<br>  &quot;login&quot;: {<br>    &quot;title&quot;: &quot;Sign in to your account&quot;,<br>    &quot;submit&quot;: &quot;Sign in&quot;<br>  }<br>}</pre><p>This gives us meaningful keys:</p><pre>t(&quot;login.title&quot;);<br>t(&quot;login.submit&quot;);</pre><p>These keys are easier to understand than generic names like text1 or button2.</p><p><strong>2. Keep Translation Keys Consistent Across Languages</strong></p><p>Our English and French translation files should follow the same structure.</p><p>For example:</p><p><strong>English</strong></p><pre>{<br>  &quot;email-label&quot;: &quot;Email&quot;,<br>  &quot;password-label&quot;: &quot;Password&quot;<br>}</pre><p><strong>French</strong></p><pre>{<br>  &quot;email-label&quot;: &quot;Adresse e-mail&quot;,<br>  &quot;password-label&quot;: &quot;Mot de passe&quot;<br>}</pre><p>The keys remain the same while the translated values change.</p><p>This consistency makes translations easier to maintain and reduces the likelihood of missing strings.</p><p>As the application grows, we can introduce automated checks to compare translation keys across languages.</p><p><strong>3. Consider Lazy Loading for Larger Applications</strong></p><p>Our current implementation uses:</p><pre>import.meta.glob(&quot;./locales/*/*.json&quot;, {<br>  eager: true,<br>});</pre><p>This eagerly imports all matching translation files.</p><p>For a small application, this is straightforward and convenient.</p><p>However, as we add more languages and namespaces, the translation resources can increase the JavaScript bundle size.</p><p>For larger applications, we can explore lazy loading so that translations are loaded only when needed.</p><p>This is an optimization worth considering when bundle size becomes a concern.</p><h3>Conclusion</h3><p>In this article, we explored how to implement internationalization in a React + Vite application using i18next and react-i18next.</p><p>We covered how to organize translations using namespaces, automatically load translation files with Vite’s import.meta.glob(), switch between languages and handle interpolation and pluralization. We also explored how to make translation keys type-safe with TypeScript and use ESLint to detect hardcoded strings.</p><p>With this setup, you have a scalable foundation for supporting multiple languages in your React application. As your application grows, you can further improve performance by exploring lazy loading for translation files.</p><p><strong>The key takeaway is that internationalization should be considered early in application development, not treated as an afterthought.</strong></p><p>Thanks for reading! I hope this guide helps you build applications that are accessible to users across different languages.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41cfc53b0ccf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating a Production React App from Create React App (CRA) to Vite]]></title>
            <link>https://balikis.medium.com/migrating-a-production-react-app-from-create-react-app-cra-to-vite-f28f6fd2fec4?source=rss-a5d8ab85d1eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/f28f6fd2fec4</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[vites]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[perfomance]]></category>
            <dc:creator><![CDATA[Balikis Oyeleye]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 09:29:07 GMT</pubDate>
            <atom:updated>2026-03-01T09:29:41.654Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pu_n_1QT5aH62Io7" /><figcaption>Photo by <a href="https://unsplash.com/@rahuulmiishra?utm_source=medium&amp;utm_medium=referral">Rahul Mishra</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>I’m excited to share that I recently successfully migrated a production application from <strong>Create React App (CRA)</strong> to <strong>Vite</strong>! 🎉</p><p>In this article, I’ll walk through the specific steps I took to achieve this migration and share some insights for others looking to do the same.</p><h4>The Problem with CRA</h4><p>As our application grew, the development speed got slower. The startup time increased, even small code changes often trigged full rebuilds rather than fast <a href="https://vite.dev/guide/features#hot-module-replacement">hot module replacements</a> and some typescript validations weren’t behaving as expected. Additionally, Create react app has been deprecated and <a href="https://react.dev/learn/creating-a-react-app">no longer recommend for modern react application</a></p><h4>Why Vite</h4><p>While the move to Vite seemed like the best option, we evaluated it against several option before deciding that it was the best fit. Here are the reasons:<br>1. It maintains a similar project structure to CRA, making the transition smother.<br>2. It has an active ecosystem.<br>3. It offers a significantly faster build and reloads times.</p><h3>Migration Strategy</h3><p>Here are the steps I took to migrate the application to Vite from CRA:</p><ol><li><strong>Update Dependencies</strong></li></ol><p>The first step is to remove<em> </em>react-scripts<em> </em>and install vite and @vitejs/plugin-react</p><pre>yarn remove react-scripts<br>yarn add -D vite @vitejs/plugin-react</pre><p>and update the package.json scripts to:</p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;vite&quot;,<br>  &quot;build&quot;: &quot;vite build&quot;,<br>  &quot;preview&quot;: &quot;vite preview&quot;<br>}</pre><p>2. <strong>Create </strong><strong>vite.config.ts file</strong></p><pre>import { defineConfig } from &quot;vite&quot;;<br>import react from &quot;@vitejs/plugin-react&quot;;<br>import path from &quot;path&quot;;<br><br>// https://vitejs.dev/config/<br>export default defineConfig({<br>  plugins: [react()],<br>  resolve: {<br>    alias: {<br>      &quot;@&quot;: path.resolve(__dirname, &quot;src&quot;)<br>    }<br>  },<br>  server: {<br>    open: true,<br>    port: 3000<br>  }<br>});</pre><p>3. <strong>Move </strong><strong>index.html to Project Root</strong></p><p>Vite expects index.html at the root of the project. Move the index.html file /public from the public folder to the project root. The explanation on why that is done is <a href="https://vite.dev/guide/#index-html-and-project-root">here</a>.</p><p>4. <strong>Update </strong><strong>index.html file</strong></p><p>CRA uses %PUBLIC_URL% and Vite does not. Remove the %PUBLIC_URL% from the links in the index.html file. For example:</p><pre>   ✅ &lt;link rel=&quot;icon&quot; href=&quot;%PUBLIC_URL%/favicon.ico&quot; /&gt;<br><br>   ❌ &lt;link rel=&quot;icon&quot; href=&quot;/favicon.ico&quot; /&gt;</pre><p>Add the vite entry script in theindex.html file</p><pre>  &lt;body&gt;<br>    &lt;noscript&gt;You need to enable JavaScript to run this app.&lt;/noscript&gt;<br>    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;<br><br>    &lt;!-- Vite entry script --&gt;<br>    &lt;script type=&quot;module&quot; src=&quot;/src/index.tsx&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;</pre><p>5. <strong>Configure the Environmental Variables</strong></p><p>Rename the env variable in the .env file.</p><p>From:</p><pre>REACT_APP_*</pre><p>To:</p><pre>VITE_*</pre><p>In your codebase, replace process.env references with import.meta.env.</p><p>6. <strong>Clean Install</strong></p><pre>rm -rf node_modules yarn.lock<br>yarn install</pre><p>7. <strong>Start the server</strong></p><pre>yarn start</pre><p>8. <strong>Removing Legacy CRA Artifacts</strong></p><p>With everything working as it should, I removed:</p><ul><li>react-app-env.d.ts and replace it with vite-env.d.ts</li><li>reportWebVitals in the index.ts file</li><li>src/reportWebVitals.ts file</li></ul><p>vite-env.d.ts :</p><pre>/// &lt;reference types=&quot;vite/client&quot; /&gt;<br><br>// intellisense for environment variables<br>interface ImportMetaEnv {<br>  readonly VITE_API_URL: string;<br>}<br><br>interface ImportMeta {<br>  readonly env: ImportMetaEnv;<br>}</pre><h3>Performance Results</h3><p>Below is a direct comparison between the CRA build and the Vite build after migration.</p><ol><li>Build time:</li></ol><ul><li>CRA: ~274s</li><li>Vite: ~46s</li></ul><p>2. Bundle size:</p><ul><li>CRA: 1.34 mb</li><li>Vite: 1.30 mb</li></ul><h3>Conclusion</h3><p>Migrating the application from create-react-app to vite has been a rewarding experience for me. While the final bundle size remained similar, the <strong>developer experience</strong> improved drastically. Our team no longer spends minutes waiting for the dev server to catch up with their code.</p><p>If you’re maintaining a growing React application and experiencing slower iteration cycles, migrating to Vite may be worth considering.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f28f6fd2fec4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>