A language switching UI lets users pick their preferred language on a website. It helps make your site friendly for people who speak different languages.
Language switching UI in NextJS
import { useState } from 'react'; export default function LanguageSwitcher() { const [language, setLanguage] = useState('en'); function changeLanguage(lang) { setLanguage(lang); // Add code here to update app language } return ( <div> <button onClick={() => changeLanguage('en')}>English</button> <button onClick={() => changeLanguage('es')}>Spanish</button> <p>Current language: {language}</p> </div> ); }
This example uses React's useState hook to track the selected language.
Buttons call a function to update the language state when clicked.
const [language, setLanguage] = useState('en');
<button onClick={() => setLanguage('fr')}>French</button><p>Current language: {language}</p>This component shows three buttons for English, Spanish, and French. When you click a button, it updates the language state and shows the selected language in uppercase. It uses aria-pressed for accessibility to indicate which button is active. The selected language text updates live for screen readers using aria-live.
import { useState } from 'react'; export default function LanguageSwitcher() { const [language, setLanguage] = useState('en'); function changeLanguage(lang) { setLanguage(lang); } return ( <main> <h1>Choose your language</h1> <nav aria-label="Language selection"> <button onClick={() => changeLanguage('en')} aria-pressed={language === 'en'}>English</button> <button onClick={() => changeLanguage('es')} aria-pressed={language === 'es'}>Spanish</button> <button onClick={() => changeLanguage('fr')} aria-pressed={language === 'fr'}>French</button> </nav> <section aria-live="polite"> <p>You selected: <strong>{language.toUpperCase()}</strong></p> </section> </main> ); }
Use semantic HTML elements like <nav> and <main> for better structure.
Use ARIA attributes like aria-pressed and aria-live to improve accessibility.
Keep the UI simple and clear so users can easily switch languages.
A language switching UI helps users pick their preferred language easily.
Use React state to track and update the selected language.
Make the UI accessible with proper HTML and ARIA attributes.
Practice
Solution
Step 1: Understand the UI purpose
A language switching UI is designed to let users pick their language preference.Step 2: Match purpose to options
Only To let users choose their preferred language for the app content describes changing language, others are unrelated to language switching.Final Answer:
To let users choose their preferred language for the app content -> Option AQuick Check:
Language switching UI = Choose language [OK]
- Confusing language switch with theme or profile settings
- Thinking it logs out users automatically
Solution
Step 1: Recall React useState syntax
The correct syntax is: const [state, setState] = useState(initialValue).Step 2: Check each option
const [language, setLanguage] = useState('en') matches correct syntax with initial value 'en'. Others have syntax errors or missing initial value.Final Answer:
const [language, setLanguage] = useState('en') -> Option AQuick Check:
useState syntax = const [var, setVar] = useState(value) [OK]
- Not destructuring the array returned by useState
- Missing initial state value
- Incorrect assignment order
<pre>const LanguageSwitcher = () => { const [lang, setLang] = useState('en'); return ( <button onClick={() => setLang('fr')} aria-label="Switch to French">FR</button> ); }; What will be the value of
lang after clicking the button once?Solution
Step 1: Understand initial state
The state variablelangstarts as 'en'.Step 2: Analyze button click effect
Clicking the button callssetLang('fr'), updatinglangto 'fr'.Final Answer:
'fr' -> Option DQuick Check:
Click sets lang to 'fr' [OK]
- Thinking state stays the same after click
- Confusing initial value with updated value
<pre>const LanguageSwitcher = () => { const [lang, setLang] = useState('en'); return ( <button onClick={() => setLang('es')}>Switch to Spanish</button> ); }; Why might this code cause an accessibility issue?
Solution
Step 1: Check accessibility features
The button has no aria-label or descriptive text for screen readers.Step 2: Evaluate other options
State is initialized, onClick syntax is correct, React import is assumed; only missing aria-label is an accessibility issue.Final Answer:
The button lacks an aria-label describing its purpose -> Option BQuick Check:
Missing aria-label = accessibility problem [OK]
- Ignoring accessibility attributes
- Assuming syntax errors without checking
- Overlooking React import necessity
Solution
Step 1: Understand Next.js locale routing
Next.js supports locale routing with to change language without full reload.Step 2: Combine URL update and React state
Using <Link> with locale prop updates URL and React state tracks current language for UI updates.Step 3: Evaluate other options
Reloading page or using window.location causes full reload, losing SPA benefits. Ignoring URL breaks routing consistency.Final Answer:
Use Next.js <Link> with locale prop and React state to track language -> Option CQuick Check:
Next.js locale routing + state = smooth language switch [OK]
- Reloading page loses SPA benefits
- Ignoring URL locale causes routing issues
- Using localStorage alone misses URL sync
Ready to prove it?
Pass the test and this pattern moves unknown
