Przejdź do głównej zawartości

24. Internacjonalizacja aplikacji webowych

Internacjonalizacja aplikacji webowych

Internacjonalizacja (i18n) to projektowanie aplikacji tak, aby można je było łatwo dostosować do różnych języków i regionów. Lokalizacja (l10n) to właściwe dopasowanie do konkretnego miejsca: format daty, waluty, liczby i teksty. Bez i18n Twoja aplikacja jest tylko dla jednej kultury.

  1. Co różni i18n od l10n? — internacjonalizacja vs lokalizacja
  2. Jak PHP obsługuje locale?setlocale(), NumberFormatter, IntlDateFormatter
  3. Jak przetłumaczyć interfejs? — pliki tłumaczeń i funkcja __()
  1. Definicje: i18n, l10n, locale (np. pl_PL, en_US)
  2. Formatowanie liczb: 1 234,56 (PL) vs 1,234.56 (US)
  3. Formatowanie dat: 04.03.2026 (PL) vs 03/04/2026 (US)
  4. Formatowanie walut: 1 234,56 zł vs $1,234.56
  5. PHP: rozszerzenie intl, NumberFormatter, IntlDateFormatter
  6. JavaScript: Intl.NumberFormat, Intl.DateTimeFormat
  7. System tłumaczeń: tablice PHP lub pliki .po

Tabela formatów

Tabela: ta sama data i liczba w 4 różnych locale

Przykład kodu

PHP NumberFormatter vs Intl.NumberFormat w JS

Minimum:

  • Różnica i18n vs l10n
  • Przykład złego i dobrego formatowania daty
  • Prosty system tłumaczeń w PHP (tablica)

Forma: 10 slajdów, 10 minut

Ocena: 3.0
<?php
// PHP: NumberFormatter (wymaga ext-intl)
$fmt = new NumberFormatter('pl_PL', NumberFormatter::CURRENCY);
echo $fmt->formatCurrency(1234.56, 'PLN'); // 1 234,56 zł
$fmt2 = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $fmt2->formatCurrency(1234.56, 'USD'); // $1,234.56
// PHP: IntlDateFormatter
$df = new IntlDateFormatter('pl_PL', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $df->format(new DateTime('2026-03-04')); // 4 marca 2026
// Prosty system tłumaczeń
$lang = $_GET['lang'] ?? 'pl';
$translations = [
'pl' => ['hello' => 'Witaj', 'save' => 'Zapisz'],
'en' => ['hello' => 'Hello', 'save' => 'Save'],
];
function __($key, string $lang = 'pl'): string {
global $translations;
return $translations[$lang][$key] ?? $key;
}
echo __('hello', $lang); // Witaj lub Hello
// JavaScript: Intl API
const price = 1234.56;
const pl = new Intl.NumberFormat('pl-PL', { style: 'currency', currency: 'PLN' });
console.log(pl.format(price)); // 1 234,56 zł
const us = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(us.format(price)); // $1,234.56
const date = new Date('2026-03-04');
const plDate = new Intl.DateTimeFormat('pl-PL', { dateStyle: 'long' });
console.log(plDate.format(date)); // 4 marca 2026

Pokaż ten sam numer w 4 językach

1234.56 w pl_PL, en_US, de_DE, ar_SA — to robi wrażenie na slajdzie

Nawiąż do projektu

Wspomnij gdzie w projekcie semestralnym brakowało i18n (np. daty)