locale/i18n
< >

return function () {
    $self = url('/@object/@action/' . urlencode('locale/i18n'));
    $sample = _class('docs')->_sample('i18n');
    $site_lang = (string) _class('i18n')->_get_current_lang();
    $p = $sample->params($_GET, $site_lang);
    $lang = $sample->lang($p['locale']);

    $intro = '<p class="docs-demo-intro">Translations with <code>t()</code>: variables like <code>%name</code>, plural forms picked by a
        number, and the fallback to the source text; then numbers, money, dates and time zones formatted for a locale. The locales are the
        regions of the two languages the framework ships (English and Ukrainian); every other input is checked against a strict format.
        The sample catalog is added in memory for this request only, as a <code>share/langs/<lang>/*.php</code> file would add it,
        and the language goes to <code>t()</code> as its third argument: the site language stays <b>' . _prepare_html($site_lang, 0) . '</b>,
        nothing is saved. Numbers and dates use PHP <code>intl</code> with the picked locale.
        Classes: <code>yf_i18n</code>, <code>yf_common_num2string</code>, <code>yf_utils</code>.</p>';

    $options = function (array $values, $selected) {
        $html = '';
        foreach ($values as $value => $label) {
            $html .= '<option value="' . _prepare_html((string) $value, 0) . '"' . ((string) $value === (string) $selected ? ' selected' : '') . '>' . _prepare_html((string) $label, 0) . '</option>';
        }
        return $html;
    };
    $locales = [];
    foreach ($sample->LOCALES as $code => $info) {
        $locales[$code] = $info[1] . ' [' . $code . ']';
    }
    $form = '<form method="get" action="' . $self . '" class="docs-demo-i18n-form">'
        . '<div class="docs-demo-i18n-row"><label>Locale <select name="locale" class="form-control form-select docs-w-locale" onchange="this.form.submit()">' . $options($locales, $p['locale']) . '</select></label>'
        . '<label>Name <input type="text" name="name" class="form-control docs-w-name" maxlength="' . $sample->MAX_NAME_LEN . '" value="' . _prepare_html($p['name'], 0) . '"></label>'
        . '<label>Count <input type="number" name="count" class="form-control docs-w-num" min="0" max="1000000" value="' . $p['count'] . '"></label></div>'
        . '<div class="docs-demo-i18n-row"><label>Amount <input type="text" name="amount" class="form-control docs-w-amount" maxlength="13" inputmode="decimal" value="' . _prepare_html($p['amount'], 0) . '"></label>'
        . '<select name="currency" class="form-control form-select" aria-label="Currency">' . $options(array_combine($sample->CURRENCIES, $sample->CURRENCIES), $p['currency']) . '</select>'
        . '<label>Date <input type="date" name="date" class="form-control" min="1900-01-01" max="2100-12-31" value="' . $p['date'] . '"></label>'
        . '<label>Time <input type="time" name="time" class="form-control" value="' . $p['time'] . '"></label>'
        . '</div><div class="docs-demo-i18n-row"><label>Time zone <select name="tz" class="form-control form-select docs-w-tz">' . $options($sample->zones(), $p['tz']) . '</select></label>'
        . '<button type="submit" class="btn btn-primary">Show</button> <a class="btn btn-default btn-outline-secondary" href="' . $self . '">Reset</a></div></form>';

    $table = function (array $rows) {
        $html = '';
        foreach ($rows as $row) {
            $html .= '<tr><td><code>' . _prepare_html($row[0], 0) . '</code></td><td>' . _prepare_html((string) $row[1], 0) . '</td></tr>';
        }
        return '<table class="table table-condensed table-sm table-bordered docs-demo-result docs-demo-i18n-table"><thead><tr><th>PHP</th><th>Output</th></tr></thead><tbody>' . $html . '</tbody></table>';
    };

    $plural_rows = '';
    foreach ($sample->plurals() as $n => $texts) {
        $plural_rows .= '<tr' . ($n === $p['count'] ? ' class="info table-info"' : '') . '><td>' . $n . '</td>';
        foreach ($texts as $text) {
            $plural_rows .= '<td>' . _prepare_html($text, 0) . '</td>';
        }
        $plural_rows .= '</tr>';
    }
    $plural_head = '';
    foreach (array_keys($sample->CATALOG) as $l) {
        $plural_head .= '<th>' . $l . '</th>';
    }
    $catalog = $sample->catalog_code('uk') . PHP_EOL . PHP_EOL . $sample->catalog_code('en');

    $zones = '';
    foreach ($sample->zone_table($p) as $zone => $info) {
        $zones .= '<tr' . ($zone === $p['tz'] ? ' class="info table-info"' : '') . '><td>' . _prepare_html($zone, 0) . '</td><td>' . _prepare_html($info[0], 0) . '</td><td>' . $info[1] . '</td></tr>';
    }
    $at_code = '$at = new DateTimeImmutable(' . var_export($p['date'] . ' ' . $p['time'], true) . ', new DateTimeZone(' . var_export($p['tz'], true) . '));';
    $zone_code = $at_code . PHP_EOL . '$tokyo = $at->setTimezone(new DateTimeZone(\'Asia/Tokyo\'));' . PHP_EOL
        . '// the site zone: $PROJECT_CONF / conf(\'timezone\'), set with date_default_timezone_set() at start';

    $no_intl = class_exists('NumberFormatter') ? '' : '<div class="alert alert-warning">The PHP <code>intl</code> extension is not loaded here, so only the framework helpers are shown.</div>';

    css('.docs-demo-i18n-form .docs-demo-i18n-row { display:flex; flex-wrap:wrap; align-items:center; gap:8px 12px; margin-bottom:8px; }
        .docs-demo-i18n-form label { font-weight:normal; margin:0; white-space:nowrap; }
        .docs-demo-i18n-form .form-control { display:inline-block; width:auto; vertical-align:middle; }
        .docs-demo-i18n-form .docs-w-name { width:9em; } .docs-demo-i18n-form .docs-w-num { width:7em; } .docs-demo-i18n-form .docs-w-amount { width:9em; }
        .docs-demo-i18n-form .docs-w-locale { width:20em; max-width:100%; } .docs-demo-i18n-form .docs-w-tz { width:17em; max-width:100%; }
        .docs-demo-i18n h4 { margin-top:22px; } .docs-demo-i18n h5 { font-weight:bold; margin-top:16px; }
        .docs-demo-i18n-plurals th:first-child, .docs-demo-i18n-plurals td:first-child { width:5em; text-align:right; }
        .docs-demo-i18n-table td:first-child { width:58%; } .docs-demo-i18n-table code { white-space:pre-wrap; word-break:break-word; }
        pre.docs-demo-code code { white-space:pre-wrap; word-break:break-word; }');
    return $intro . $form . $no_intl . '<div class="docs-demo-i18n">'
        . '<h4>Translations with t() <small class="text-muted">language ' . $lang . '</small></h4>' . $table($sample->translations($p))
        . '<h5>Plural forms of one source string, by language</h5>'
        . '<table class="table table-condensed table-sm table-bordered docs-demo-result docs-demo-i18n-plurals"><thead><tr><th>%num</th>' . $plural_head . '</tr></thead><tbody>' . $plural_rows . '</tbody></table>'
        . '<h5>The sample catalog, as lang files</h5><pre class="docs-demo-code"><code class="php">' . _prepare_html($catalog, 0) . '</code></pre>'
        . '<h4>Numbers and money <small class="text-muted">' . $p['locale'] . '</small></h4>' . $table($sample->numbers($p))
        . '<h4>Dates <small class="text-muted">' . $p['locale'] . ', ' . _prepare_html($p['tz'], 0) . '</small></h4><pre class="docs-demo-code"><code class="php">' . _prepare_html($at_code, 0) . '</code></pre>' . $table($sample->dates($p))
        . '<h4>Time zones <small class="text-muted">the same moment, ' . $p['locale'] . '</small></h4><pre class="docs-demo-code"><code class="php">' . _prepare_html($zone_code, 0) . '</code></pre>'
        . '<table class="table table-condensed table-sm table-bordered docs-demo-result"><thead><tr><th>Zone</th><th>Local time</th><th>UTC offset</th></tr></thead><tbody>' . $zones . '</tbody></table></div>';
};

Translations with t(): variables like %name, plural forms picked by a number, and the fallback to the source text; then numbers, money, dates and time zones formatted for a locale. The locales are the regions of the two languages the framework ships (English and Ukrainian); every other input is checked against a strict format. The sample catalog is added in memory for this request only, as a share/langs/<lang>/*.php file would add it, and the language goes to t() as its third argument: the site language stays uk, nothing is saved. Numbers and dates use PHP intl with the picked locale. Classes: yf_i18n, yf_common_num2string, yf_utils.

Reset

Translations with t() language uk

PHPOutput
t('Hello, %name!', ['%name' => 'Olena'], 'uk')Привіт, Olena!
t('You have %num new messages', ['%num' => 5], 'uk')У вас 5 нових повідомлень
t('Your order ships on %date', ['%date' => $date], 'uk')Ваше замовлення буде відправлено 16 листопада 2026 р.
t('Not in the catalog yet', [], 'uk')Not in the catalog yet
Plural forms of one source string, by language
%numenuk
0You have no new messagesУ вас немає нових повідомлень
1You have one new messageУ вас 1 нове повідомлення
2You have 2 new messagesУ вас 2 нові повідомлення
4You have 4 new messagesУ вас 4 нові повідомлення
5You have 5 new messagesУ вас 5 нових повідомлень
11You have 11 new messagesУ вас 11 нових повідомлень
12You have 12 new messagesУ вас 12 нових повідомлень
21You have 21 new messagesУ вас 21 нове повідомлення
22You have 22 new messagesУ вас 22 нові повідомлення
25You have 25 new messagesУ вас 25 нових повідомлень
101You have 101 new messagesУ вас 101 нове повідомлення
111You have 111 new messagesУ вас 111 нових повідомлень
The sample catalog, as lang files
<?php
// share/langs/uk/uk_messages.php
return [
    'Hello, %name!' => 'Привіт, %name!',
    'You have %num new messages' => 'У вас {%num нових повідомлень|0:немає нових повідомлень|1:%num нове повідомлення|2,3,4:%num нові повідомлення|11-14:%num нових повідомлень|%num нових повідомлень}',
    'Your order ships on %date' => 'Ваше замовлення буде відправлено %date',
];

<?php
// share/langs/en/en_messages.php
return [
    'You have %num new messages' => 'You have {%num new messages|#0:no new messages|#1:one new message|%num new messages}',
];

Numbers and money uk_UA

PHPOutput
(new NumberFormatter('uk_UA', NumberFormatter::DECIMAL))->format(1234567.89)1 234 567,89
(new NumberFormatter('uk_UA', NumberFormatter::CURRENCY))->formatCurrency(1234567.89, 'UAH')1 234 567,89 ₴
(new NumberFormatter('uk_UA', NumberFormatter::PERCENT))->format(0.256)26%
(new NumberFormatter('uk_UA', NumberFormatter::SPELLOUT))->format(5)пʼять
common()->num2str(1234567.89, 'UAH', 'UA')один мільйон двісті тридцять чотири тисячі п`ятьсот шістдесят сім гривень 89 копійок

Dates uk_UA, Europe/Kyiv

$at = new DateTimeImmutable('2026-11-16 02:11', new DateTimeZone('Europe/Kyiv'));
PHPOutput
(new IntlDateFormatter('uk_UA', IntlDateFormatter::FULL, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)понеділок, 16 листопада 2026 р. о 02:11
(new IntlDateFormatter('uk_UA', IntlDateFormatter::LONG, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)16 листопада 2026 р. о 02:11
(new IntlDateFormatter('uk_UA', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)16 лист. 2026 р., 02:11
(new IntlDateFormatter('uk_UA', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)16.11.26, 02:11
(new IntlDateFormatter('uk_UA', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'Europe/Kyiv', null, 'LLLL y'))->format($at)листопад 2026
_format_date('2026-11-16 02:11', 'for_profile')November 16, 2026

Time zones the same moment, uk_UA

$at = new DateTimeImmutable('2026-11-16 02:11', new DateTimeZone('Europe/Kyiv'));
$tokyo = $at->setTimezone(new DateTimeZone('Asia/Tokyo'));
// the site zone: $PROJECT_CONF / conf('timezone'), set with date_default_timezone_set() at start
ZoneLocal timeUTC offset
Europe/Kyiv16 лист. 2026 р., 02:11+02:00
UTC16 лист. 2026 р., 00:11+00:00
America/New_York15 лист. 2026 р., 19:11-05:00
Europe/London16 лист. 2026 р., 00:11+00:00
Asia/Tokyo16 лист. 2026 р., 09:11+09:00