What if your app could speak every language without rewriting everything?
Why i18n matters in NextJS - The Real Reasons
Imagine building a website that only speaks English. Now, you want to add support for Spanish, French, and Chinese. You try to copy and paste text everywhere and manually swap it out based on user choice.
Manually changing text for each language means repeating work, risking mistakes, and making updates a nightmare. It's easy to miss some text or break layouts when text length changes.
i18n (internationalization) tools let you write your app once and easily add many languages. They handle swapping text, formatting dates, and more, so your app feels natural to users worldwide.
if (lang === 'es') { show('Hola'); } else { show('Hello'); }
t('greeting') // returns 'Hello' or 'Hola' based on language
i18n makes your app welcoming and usable for people everywhere, opening doors to global audiences without extra headaches.
A travel website that shows prices, dates, and messages in the visitor's language and format, making booking easy and friendly.
Manual text changes for languages are slow and error-prone.
i18n automates language switching and formatting.
This helps your app reach and delight users worldwide.
Practice
Solution
Step 1: Understand the purpose of i18n
i18n stands for internationalization, which means making an app usable for people from different languages and cultures.Step 2: Identify what i18n affects in an app
It handles language translations, date formats, number formats, and cultural differences to improve user experience worldwide.Final Answer:
It allows the app to support multiple languages and cultures. -> Option BQuick Check:
i18n = support multiple languages [OK]
- Confusing i18n with performance improvements
- Thinking i18n fixes bugs automatically
- Believing i18n reduces file size
Solution
Step 1: Recall Next.js i18n tools
Next.js provides a hook calleduseTranslationsto easily get translated strings in components.Step 2: Differentiate from other hooks
useRoutermanages routing,useStatemanages state, anduseEffecthandles side effects, none are for translations.Final Answer:
useTranslations -> Option DQuick Check:
Translation hook = useTranslations [OK]
- Choosing useRouter for translations
- Confusing useState or useEffect with i18n
- Not knowing Next.js i18n hooks
import { useTranslations } from 'next-intl';
export default function Greeting() {
const t = useTranslations('Home');
return <h1>{t('welcomeMessage')}</h1>;
}If the translation file for 'Home' has
{ "welcomeMessage": "Hello, world!" }, what will be rendered?Solution
Step 1: Understand useTranslations usage
The hookuseTranslations('Home')returns a functiontthat looks up keys in the 'Home' translation file.Step 2: Check the translation key value
The key 'welcomeMessage' maps to "Hello, world!" in the translation file, sot('welcomeMessage')returns "Hello, world!".Final Answer:
<h1>Hello, world!</h1> -> Option CQuick Check:
t('welcomeMessage') = 'Hello, world!' [OK]
- Thinking t returns the key name instead of value
- Assuming t is not a function
- Confusing component name with translation output
import { useTranslations } from 'next-intl';
export default function Header() {
const t = useTranslations('Header');
return <h2>t('title')</h2>;
}Solution
Step 1: Check JSX syntax for function calls
In JSX, JavaScript expressions must be inside curly braces. Here,t('title')is written as plain text, so it won't execute.Step 2: Identify the fix
Wrappingt('title')in curly braces like{t('title')}correctly calls the function and renders the translation.Final Answer:
The translation function call is missing curly braces inside JSX. -> Option AQuick Check:
JSX expressions need curly braces [OK]
- Forgetting curly braces around function calls in JSX
- Assuming case sensitivity causes error here
- Thinking import is wrong without checking
Solution
Step 1: Understand i18n date formatting needs
Dates should appear in formats familiar to users based on their locale (language and region).Step 2: Identify the best tool for locale-aware formatting
JavaScript'sIntl.DateTimeFormatformats dates according to locale, which can be obtained from Next.js router or i18n context.Step 3: Evaluate other options
Hardcoding or manual string slicing ignores locale differences and reduces usability. ISO format is not user-friendly for all locales.Final Answer:
Use JavaScript's Intl.DateTimeFormat with the user's locale from Next.js router. -> Option AQuick Check:
Locale-aware date formatting = Intl.DateTimeFormat [OK]
- Hardcoding date formats ignoring locale
- Manually slicing date strings incorrectly
- Showing ISO format without localization
Ready to prove it?
Pass the test and this pattern moves unknown
