You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/helper/internationalization_helper...

85 lines
1.7 KiB
PHTML

<?php
use Engelsystem\Http\Request;
/**
* Return currently active locale
*
* @return string
*/
8 years ago
function locale()
{
return $_SESSION['locale'];
}
/**
* Returns two letter language code from currently active locale
*
* @return string
*/
8 years ago
function locale_short()
{
return substr(locale(), 0, 2);
}
/**
* Initializes gettext for internationalization and updates the sessions locale to use for translation.
*/
8 years ago
function gettext_init()
{
$locales = config('locales');
$request = request();
if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) {
$_SESSION['locale'] = $request->input('set_locale');
8 years ago
} elseif (!isset($_SESSION['locale'])) {
$_SESSION['locale'] = config('default_locale');
8 years ago
}
8 years ago
gettext_locale();
bindtextdomain('default', realpath(__DIR__ . '/../../locale'));
bind_textdomain_codeset('default', 'UTF-8');
textdomain('default');
}
/**
* Swich gettext locale.
*
8 years ago
* @param string $locale
*/
8 years ago
function gettext_locale($locale = null)
{
if ($locale == null) {
$locale = $_SESSION['locale'];
}
8 years ago
8 years ago
putenv('LC_ALL=' . $locale);
setlocale(LC_ALL, $locale);
}
/**
* Renders language selection.
*
* @return array
*/
8 years ago
function make_langselect()
{
$request = Request::getInstance();
8 years ago
8 years ago
$items = [];
foreach (config('locales') as $locale => $name) {
$url = url($request->getPathInfo(), ['set_locale' => $locale]);
8 years ago
$items[] = toolbar_item_link(
htmlspecialchars($url),
8 years ago
'',
sprintf(
'<img src="%s" alt="%s" title="%2$s"> %2$s',
url('pic/flag/' . $locale . '.png'),
$name
)
8 years ago
);
8 years ago
}
return $items;
}