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>';
};