Learn to implement i18n in your React dashboard. We cover managing language state and using context to provide localized UI strings to your components.
Previously in this course, we explored Managing Global Modals: A Scalable Pattern for React UI to decouple UI overlays from component logic. In this lesson, we apply similar architectural principles to solve a different challenge: globalizing your application through i18n (internationalization).
Making an application truly global isn't just about translating text; it's about building a system that can handle different languages, date formats, and layout directions. For our dashboard, we will implement a robust provider-based architecture that allows users to toggle their preferred language dynamically.
At its core, i18n is the process of preparing your software to be adapted to various languages and regions without engineering changes. The companion process, localization (often abbreviated as l10n), is the actual translation and formatting.
To implement this in React, we need three distinct layers:
We avoid hardcoding strings directly in our components. Instead, we reference keys, ensuring our UI remains clean and maintainable.
We’ll start by creating a dedicated provider. This keeps our language state centralized, similar to how we managed state in Advanced Context Patterns: Scalable State for React Dashboards.
JAVASCRIPT// src/context/I18nContext.jsx import React, { createContext, useContext, useState } from CE9178">'react'; const translations = { en: { welcome: "Welcome to your Dashboard" }, es: { welcome: "Bienvenido a su Panel" } }; const I18nContext = createContext(); export const I18nProvider = ({ children }) => { const [locale, setLocale] = useState(CE9178">'en'); const t = (key) => translations[locale][key] || key; return ( <I18nContext.Provider value={{ locale, setLocale, t }}> {children} </I18nContext.Provider> ); }; export const useTranslation = () => useContext(I18nContext);
By exposing a t function, any component can now access localized text without knowing the internal structure of our dictionary.
Now, integrate this into your dashboard. In your main layout or root component, wrap your application with the I18nProvider.
JAVASCRIPT// Example usage in a dashboard header const DashboardHeader = () => { const { t, setLocale } = useTranslation(); return ( <header> <h1>{t(CE9178">'welcome')}</h1> <select onChange={(e) => setLocale(e.target.value)}> <option value="en">English</option> <option value="es">Español</option> </select> </header> ); };
This approach allows your dashboard to react instantly to language changes. When setLocale is called, the context value updates, causing all components consuming useTranslation to re-render with the new language strings.
Your task is to extend the existing dashboard settings.
locales folder and move your translation dictionary into a separate JSON file.I18nProvider to persist the chosen language to localStorage. You can reference the logic we built in Building a useLocalStorage Hook to ensure the user's preference survives a page refresh.t function returns the key if the translation is missing. This makes it immediately obvious during development when a string hasn't been localized.I18nContext value changes every time the user switches languages, ensure your provider value is memoized if the dictionary grows large, or keep the dictionary outside the component tree to avoid unnecessary object recreation.<body> or root <div> to handle layout mirroring.We have successfully decoupled our UI text from our components using a custom I18nProvider. By centralizing language state and providing a helper function, we've created a scalable foundation for a multi-language dashboard. This pattern of using context to distribute configuration—like language or theme—is a standard approach in professional React applications.
Up next: We will integrate real-time communication into our dashboard by learning to handle WebSocket connections for live updates.
Enhance your React dashboard's accessibility and efficiency by implementing global keyboard shortcuts using useEffect, key mapping, and focus management.
Read moreStop prop-drilling modal visibility. Learn to build a global modal system using Context API and state to trigger UI overlays from anywhere in your app.
Internationalization Basics