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']);

    // %name is part of the text here (the t() variable it describes), so the site language goes in as @site_lang
    $intro = '<p class="docs-demo-intro">' . t('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>@site_lang</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>.', ['@site_lang' => _prepare_html($site_lang, 0)]) . '</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>' . t('Locale') . ' <select name="locale" class="form-control form-select docs-w-locale" onchange="this.form.submit()">' . $options($locales, $p['locale']) . '</select></label>'
        . '<label>' . t('First 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>' . t('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>' . t('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="' . t('Currency') . '">' . $options(array_combine($sample->CURRENCIES, $sample->CURRENCIES), $p['currency']) . '</select>'
        . '<label>' . t('Date') . ' <input type="date" name="date" class="form-control" min="1900-01-01" max="2100-12-31" value="' . $p['date'] . '"></label>'
        . '<label>' . t('Time') . ' <input type="time" name="time" class="form-control" value="' . $p['time'] . '"></label>'
        . '</div><div class="docs-demo-i18n-row"><label>' . t('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">' . t('Show') . '</button> <a class="btn btn-default btn-outline-secondary" href="' . $self . '">' . t('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>' . t('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">' . t('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>' . t('Translations with t()') . ' <small class="text-muted">' . t('language @lang', ['@lang' => $lang]) . '</small></h4>' . $table($sample->translations($p))
        . '<h5>' . t('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>' . t('The sample catalog, as lang files') . '</h5><pre class="docs-demo-code"><code class="php">' . _prepare_html($catalog, 0) . '</code></pre>'
        . '<h4>' . t('Numbers and money') . ' <small class="text-muted">' . $p['locale'] . '</small></h4>' . $table($sample->numbers($p))
        . '<h4>' . t('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>' . t('Time zones') . ' <small class="text-muted">' . t('the same moment, @locale', ['@locale' => $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>' . t('Zone') . '</th><th>' . t('Local time') . '</th><th>' . t('UTC offset') . '</th></tr></thead><tbody>' . $zones . '</tbody></table></div>';
};

Переклади через t(): змінні на кшталт %name, форми множини, обрані за числом, і повернення до вихідного тексту; далі числа, гроші, дати й часові пояси, відформатовані для локалі. Локалі — це регіони двох мов, які постачає фреймворк (англійської та української); кожне інше введення перевіряється за суворим форматом. Каталог-приклад додається в пам'ять лише на цей запит, як його додав би файл share/langs/<lang>/*.php, а мова передається в t() третім аргументом: мова сайту лишається uk, нічого не зберігається. Числа й дати використовують PHP intl з обраною локаллю. Класи: yf_i18n, yf_common_num2string, yf_utils.

Скинути

Переклади через t() мова uk

PHPВивід
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')Ваше замовлення буде відправлено 24 листопада 2026 р.
t('Not in the catalog yet', [], 'uk')Not in the catalog yet
Форми множини одного вихідного рядка, за мовами
%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 нових повідомлень
Каталог-приклад у вигляді мовних файлів
<?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}',
];

Числа й гроші uk_UA

PHPВивід
(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 копійок

Дати uk_UA, Europe/Kyiv

$at = new DateTimeImmutable('2026-11-24 03:34', new DateTimeZone('Europe/Kyiv'));
PHPВивід
(new IntlDateFormatter('uk_UA', IntlDateFormatter::FULL, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)вівторок, 24 листопада 2026 р. о 03:34
(new IntlDateFormatter('uk_UA', IntlDateFormatter::LONG, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)24 листопада 2026 р. о 03:34
(new IntlDateFormatter('uk_UA', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)24 лист. 2026 р., 03:34
(new IntlDateFormatter('uk_UA', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'Europe/Kyiv'))->format($at)24.11.26, 03:34
(new IntlDateFormatter('uk_UA', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'Europe/Kyiv', null, 'LLLL y'))->format($at)листопад 2026
_format_date('2026-11-24 03:34', 'for_profile')November 24, 2026

Часові пояси той самий момент, uk_UA

$at = new DateTimeImmutable('2026-11-24 03:34', 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
ПоясМісцевий часЗсув від UTC
Europe/Kyiv24 лист. 2026 р., 03:34+02:00
UTC24 лист. 2026 р., 01:34+00:00
America/New_York23 лист. 2026 р., 20:34-05:00
Europe/London24 лист. 2026 р., 01:34+00:00
Asia/Tokyo24 лист. 2026 р., 10:34+09:00