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
// 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
// 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
"You have " + count + " items" breaks in languages where word order differs. Use parameter placeholders instead./fr/products) instead of query parameters for SEO β search engines index each language version separately.π Quick Reference β Localization
| Concept | PHP Implementation |
|---|---|
| setlocale() | Set locale for built-in functions |
| NumberFormatter | Format numbers and currencies per locale |
| IntlDateFormatter | Format dates per locale and calendar |
| gettext() | GNU gettext translation system |
| Accept-Language | HTTP 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.