--- name: i18n (Internationalization) Setup description: Designing software applications to adapt to different languages and regions without requiring engineering changes, using translation keys, pluralization, and locale-aware formatting. --- # i18n (Internationalization) Setup > **Current Level:** Intermediate > **Domain:** Internationalization / Frontend --- ## Overview Internationalization (i18n) is the process of designing software applications to adapt to different languages and regions without requiring engineering changes. Effective i18n uses translation keys, pluralization rules, date/number formatting, and RTL support to create applications that work globally. ## What is i18n ### i18n vs l10n | Aspect | i18n | l10n | |--------|-------|-------| | **Full Name** | Internationalization | Localization | | **Focus** | Making app translatable | Translating to specific language | | **When** | During development | After i18n is complete | | **Who** | Developers | Translators | | **Output** | Code with translation keys | Translated content | ### i18n Architecture ``` ┌─────────────────────────────────────────────────────────────────┐ │ i18n Architecture │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Source │───▶│ Translation │───▶│ Display │ │ │ │ Code │ │ Files │ │ Layer │ │ │ │ │ │ │ │ │ │ │ │ t('key') │ │ { "key": │ │ Hello World │ │ │ │ │ │ "value" │ │ │ │ │ │ │ │ } │ │ │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘ ``` ## Locale ### Locale Format `language-COUNTRY` | Locale | Language | Country | |--------|----------|---------| | `en-US` | English | United States | | `en-GB` | English | United Kingdom | | `th-TH` | Thai | Thailand | | `pt-BR` | Portuguese | Brazil | | `ja-JP` | Japanese | Japan | ### Supported Locales ```javascript const SUPPORTED_LOCALES = [ 'en-US', 'th-TH', 'ja-JP', 'pt-BR', 'de-DE', 'fr-FR', 'es-ES', 'zh-CN' ]; ``` ## Translation File Structure ### JSON Format ```json { "common": { "welcome": "Welcome", "login": "Login", "logout": "Logout" }, "auth": { "email": "Email", "password": "Password", "forgotPassword": "Forgot Password?", "signup": "Sign Up" }, "errors": { "required": "This field is required", "invalidEmail": "Invalid email address", "wrongPassword": "Incorrect password" } } ``` ### Nested Keys ```json { "user": { "profile": { "title": "Profile", "name": "Name", "email": "Email" }, "settings": { "title": "Settings", "language": "Language", "theme": "Theme" } } } ``` ### Pluralization ```json { "items": { "zero": "No items", "one": "One item", "other": "{{count}} items" } } ``` ## i18n Libraries ### React | Library | Description | |---------|-------------| | **react-i18next** | Most popular, feature-rich | | **react-intl** | Format.js-based, good for complex formatting | | **react-intl-universal** | Lightweight, simple | ### Vue.js | Library | Description | |---------|-------------| | **vue-i18n** | Official Vue plugin | | **vue-intl** | Alternative plugin | ### Angular | Library | Description | |---------|-------------| | **@angular/localize** | Official Angular i18n | | **ngx-translate** | Community favorite | ### Node.js | Library | Description | |---------|-------------| | **i18next** | Framework-agnostic | | **i18n-node** | Simple Node.js i18n | ### Python | Library | Description | |---------|-------------| | **Babel** | Python i18n library | | **gettext** | GNU gettext for Python | ### Java | Library | Description | |---------|-------------| | **ResourceBundle** | Built-in Java i18n | | **MessageFormat** | ICU MessageFormat | ## react-i18next Setup ### Installation ```bash npm install i18next react-i18next ``` ### Configuration ```javascript import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; const resources = { en: { translation: { welcome: 'Welcome', login: 'Login' } }, th: { translation: { welcome: 'ยินดีต้อนรับ', login: 'เข้าสู่ระบบ' } } }; i18n.use(initReactI18next({ resources, lng: 'en', // Default language fallbackLng: 'en', interpolation: { escapeValue: false }, react: { useSuspense: false } })); export default i18n; ``` ### Using Translations ```javascript import { useTranslation } from 'react-i18next'; function Welcome() { const { t } = useTranslation(); return (
{t('greeting', { name })}
; } // Translation: "greeting": "Hello, {{name}}" ``` ### Pluralization ```javascript const { t } = useTranslation(); function ItemCount({ count }) { return{t('items', { count })}
; } // Translation: // "items_zero": "No items", // "items_one": "One item", // "items_other": "{{count}} items" ``` ## Translation Keys ### Naming Conventions | Convention | Example | Why | |-----------|----------|-----| | **Descriptive** | `user.profile.title` | Self-documenting | | **Hierarchical** | `auth.login.email` | Organized | | **Consistent** | `button.submit` | Predictable | | **Avoid** | `btn1`, `txt2` | Unclear | ### Namespace Organization ```json { "common": { "actions": { "save": "Save", "cancel": "Cancel", "delete": "Delete" } }, "auth": { "login": { "title": "Login", "email": "Email", "password": "Password", "button": "Login" }, "signup": { "title": "Sign Up", "name": "Name", "email": "Email", "password": "Password", "button": "Sign Up" } }, "dashboard": { "title": "Dashboard", "overview": "Overview", "analytics": "Analytics", "settings": "Settings" } } ``` ### Default Values ```javascript const { t } = useTranslation(); function Welcome() { return ({t('hello')} {name}!
// Good: Use interpolation{t('greeting', { name })}
``` ### Not Handling Pluralization ```javascript // Bad: No pluralizationYou have {count} items
// Good: With pluralization{t('items', { count })}
``` ### Not Supporting RTL ```javascript // Bad: No RTL supportHello, user!
``` ```jsx // ✅ Good - Translation keys{t('greeting', { name: user.name })}
``` ### ❌ Don't: No Pluralization ```jsx // ❌ Bad - No pluralization {t('item', { count: items.length })} // Always "item" ``` ```jsx // ✅ Good - Pluralization {t('item', { count: items.length })} // "item" or "items" ``` ### ❌ Don't: Ignore RTL ```css /* ❌ Bad - Left-aligned only */ .text { text-align: left; } ``` ```css /* ✅ Good - RTL-aware */ .text { text-align: start; /* Adapts to direction */ } ``` --- ## Integration Points - **Localization** (`25-internationalization/localization/`) - Content translation - **Multi-language** (`25-internationalization/multi-language/`) - Multi-language support - **RTL Support** (`25-internationalization/rtl-support/`) - Right-to-left languages --- ## Further Reading - [i18next Documentation](https://www.i18next.com/) - [React Intl](https://formatjs.io/docs/react-intl/) - [i18n Best Practices](https://www.w3.org/International/techniques/developing-specs) - [ ] Missing translation fallback - [ ] Translation logging ### Testing - [ ] All languages tested - [ ] Screenshot testing completed - [ ] Pseudo-localization tested - [ ] RTL layouts tested - [ ] SEO tags tested