Lesson 34 β€’ Advanced

    Localization & i18n 🌍

    Build multilingual PHP applications with translation systems, locale-aware formatting, and automatic language detection.

    What You'll Learn in This Lesson

    • β€’ Difference between internationalization (i18n) and localization (l10n)
    • β€’ Build a translation system with parameter interpolation
    • β€’ Format dates, numbers, and currencies for any locale
    • β€’ Detect user language from URL, cookies, and headers
    • β€’ Use PHP's intl extension for professional formatting

    Translation Systems

    The key to i18n is separating text from code. Instead of hardcoding "Welcome!", you use a key like t("welcome") that looks up the correct translation for the current locale. Parameters like :name get replaced dynamically, so "Hello, :name!" becomes "Β‘Hola, Alice!" in Spanish.

    Try It: Translation System

    Build a translator with English, Spanish, and French

    Try it Yourself Β»
    JavaScript
    // Internationalization (i18n) & Localization (l10n) in PHP
    console.log("=== i18n vs l10n β€” What's the Difference? ===");
    console.log();
    console.log("  i18n (Internationalization):");
    console.log("    Making your code CAPABLE of supporting multiple languages.");
    console.log("    β†’ Extract strings, use placeholders, handle encodings.");
    console.log();
    console.log("  l10n (Localization):");
    console.log("    Adapting your app for a SPECIFIC locale.");
    console.log("    β†’ Translate strings, format da
    ...

    Locale-Aware Formatting

    Different locales format the same number completely differently: 1,234,567.89 in the US becomes 1.234.567,89 in Germany and 1,234,568 (no decimals) in Japan. PHP's intl extension and NumberFormatter handle this automatically.

    Try It: Locale Formatting

    Format numbers, dates, and currencies for US, German, Japanese, and Arabic locales

    Try it Yourself Β»
    JavaScript
    // Locale-Aware Formatting: Dates, Numbers, Currencies
    console.log("=== Number & Currency Formatting ===");
    console.log();
    
    class LocaleFormatter {
      constructor(locale) { this.locale = locale; }
    
      formatNumber(num) {
        return new Intl.NumberFormat(this.locale).format(num);
      }
    
      formatCurrency(amount, currency) {
        return new Intl.NumberFormat(this.locale, { style: "currency", currency }).format(amount);
      }
    
      formatDate(date, style) {
        let options = style === "long"
          ? { weekday
    ...

    ⚠️ Common Mistakes

    ⚠️
    Concatenating translated strings β€” "You have " + count + " items" breaks in languages where word order differs. Use parameter placeholders instead.
    ⚠️
    Forgetting pluralization β€” "1 item" vs "2 items" varies wildly between languages. Arabic has 6 plural forms! Use proper plural rules.
    πŸ’‘
    Pro Tip: Use URL prefixes (/fr/products) instead of query parameters for SEO β€” search engines index each language version separately.

    πŸ“‹ Quick Reference β€” Localization

    ConceptPHP Implementation
    setlocale()Set locale for built-in functions
    NumberFormatterFormat numbers and currencies per locale
    IntlDateFormatterFormat dates per locale and calendar
    gettext()GNU gettext translation system
    Accept-LanguageHTTP header for browser language preference

    πŸŽ‰ Lesson Complete!

    You can now build multilingual PHP apps! Next, learn to add search features with full-text search and Elasticsearch.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy β€’ Terms of Service