Questions
This component has hardcoded text. Refactor it to support multiple languages.
A developer has built a new Welcome component. It works perfectly, but all the text is hardcoded in English. The product team now requires the application to support both English and German.
The current implementation is not scalable for adding new languages.
You’ve been given the component with the hardcoded text. Your task is to refactor it to support internationalization (i18n).
- Create a simple translation dictionary to hold the strings for English (
en) and German (de). - Implement a way to manage the application’s current language (locale).
- Connect the
LanguageSwitcherbuttons to change the locale. - Update the
Welcomecomponent to display the correct text based on the selected locale.
For this challenge, you don’t need a full library like react-i18next; implementing a simple system using React state and context is sufficient to demonstrate the concept.
import React, { useState, useContext, createContext } from 'react';
// --- Challenge: Refactor this section ---
// 1. The text is hardcoded in English
function Welcome() {
return (
<div>
<h2>Welcome!</h2>
<p>Thank you for visiting our application.</p>
</div>
);
}
// 2. The language switcher is not connected to any logic
function LanguageSwitcher() {
return (
<div>
<button>English</button>
<button>German</button>
</div>
);
}
export default function App() {
return (
<div>
<LanguageSwitcher />
<hr />
<Welcome />
</div>
);
} How Different Experience Levels Approach This
import React, { useState, useContext, createContext } from 'react'; // 1. The text is hardcoded in English function Welcome() { return ( <div> <h2>Welcome!</h2> <p>Thank you for visiting our application.</p> </div> ); } // 2. The language switcher is not connected to any logic function LanguageSwitcher() { return ( <div> <button>English</button> <button>German</button> </div> ); } export default function App() { return ( <div> <LanguageSwitcher /> <hr /> <Welcome /> </div> ); }
import React, { useState, useContext, createContext } from 'react'; // 1. Create a dictionary for translations const translations = { en: { title: 'Welcome!', body: 'Thank you for visiting our application.', }, de: { title: 'Willkommen!', body: 'Danke für Ihren Besuch auf unserer Anwendung.', }, }; // 2. Create a context to manage the locale const LocaleContext = createContext(); // 3. Refactor Welcome to use the context and dictionary function Welcome() { const { locale } = useContext(LocaleContext); const { title, body } = translations[locale]; return ( <div> <h2>{title}</h2> <p>{body}</p> </div> ); } // 4. Refactor LanguageSwitcher to change the locale function LanguageSwitcher() { const { setLocale } = useContext(LocaleContext); return ( <div> <button onClick={() => setLocale('en')}>English</button> <button onClick={() => setLocale('de')}>German</button> </div> ); } // 5. The App component now provides the context export default function App() { const [locale, setLocale] = useState('en'); return ( <LocaleContext.Provider value={{ locale, setLocale }}> <div> <LanguageSwitcher /> <hr /> <Welcome /> </div> </LocaleContext.Provider> ); }
- Context over facts: Explains when and why, not just what
- Real examples: Provides specific use cases from production experience
- Trade-offs: Acknowledges pros, cons, and decision factors
Practice Question
For an application that needs to be translated, what is the main problem with hardcoding text strings directly into your components?