2013-11-25 19:12:19 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-29 16:21:25 +02:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
|
2015-07-12 13:45:47 +02:00
|
|
|
/**
|
|
|
|
* Return currently active locale
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2015-07-12 13:45:47 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function locale()
|
|
|
|
{
|
2017-08-30 19:57:01 +02:00
|
|
|
return session()->get('locale');
|
2015-07-12 13:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns two letter language code from currently active locale
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2015-07-12 13:45:47 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function locale_short()
|
|
|
|
{
|
|
|
|
return substr(locale(), 0, 2);
|
2015-07-12 13:45:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:12:19 +01:00
|
|
|
/**
|
|
|
|
* Initializes gettext for internationalization and updates the sessions locale to use for translation.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function gettext_init()
|
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
$locales = config('locales');
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-08-30 19:57:01 +02:00
|
|
|
$session = session();
|
2017-01-01 19:49:59 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) {
|
2017-08-30 19:57:01 +02:00
|
|
|
$session->set('locale', $request->input('set_locale'));
|
|
|
|
} elseif (!$session->has('locale')) {
|
|
|
|
$session->set('locale', config('default_locale'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-01 19:49:59 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
gettext_locale();
|
|
|
|
bindtextdomain('default', realpath(__DIR__ . '/../../locale'));
|
|
|
|
bind_textdomain_codeset('default', 'UTF-8');
|
|
|
|
textdomain('default');
|
2013-11-25 19:12:19 +01:00
|
|
|
}
|
|
|
|
|
2013-12-26 13:34:48 +01:00
|
|
|
/**
|
|
|
|
* Swich gettext locale.
|
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $locale
|
2013-12-26 13:34:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function gettext_locale($locale = null)
|
|
|
|
{
|
|
|
|
if ($locale == null) {
|
2017-08-30 19:57:01 +02:00
|
|
|
$locale = session()->get('locale');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
putenv('LC_ALL=' . $locale);
|
|
|
|
setlocale(LC_ALL, $locale);
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:12:19 +01:00
|
|
|
/**
|
|
|
|
* Renders language selection.
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array
|
2013-11-25 19:12:19 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function make_langselect()
|
|
|
|
{
|
2017-08-29 16:21:25 +02:00
|
|
|
$request = Request::getInstance();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$items = [];
|
2017-01-21 23:07:20 +01:00
|
|
|
foreach (config('locales') as $locale => $name) {
|
2017-08-29 16:21:25 +02:00
|
|
|
$url = url($request->getPathInfo(), ['set_locale' => $locale]);
|
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
$items[] = toolbar_item_link(
|
2017-08-29 16:21:25 +02:00
|
|
|
htmlspecialchars($url),
|
2017-01-02 15:43:36 +01:00
|
|
|
'',
|
2017-08-29 16:21:25 +02:00
|
|
|
sprintf(
|
|
|
|
'<img src="%s" alt="%s" title="%2$s"> %2$s',
|
|
|
|
url('pic/flag/' . $locale . '.png'),
|
|
|
|
$name
|
|
|
|
)
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $items;
|
2013-11-25 19:12:19 +01:00
|
|
|
}
|