engelsystem/includes/helper/internationalization_helper...

71 lines
1.6 KiB
PHP
Raw Normal View History

2013-11-25 19:12:19 +01:00
<?php
2016-09-30 17:08:20 +02:00
$locales = [
2013-11-25 19:12:19 +01:00
'de_DE.UTF-8' => "Deutsch",
'en_US.UTF-8' => "English"
2016-09-30 17:08:20 +02:00
];
2013-11-25 19:12:19 +01:00
$default_locale = 'en_US.UTF-8';
2015-07-12 13:45:47 +02:00
/**
* Return currently active locale
*/
function locale() {
return $_SESSION['locale'];
}
/**
* Returns two letter language code from currently active locale
*/
function locale_short() {
return substr(locale(), 0, 2);
}
2013-11-25 19:12:19 +01:00
/**
* Initializes gettext for internationalization and updates the sessions locale to use for translation.
*/
function gettext_init() {
global $locales, $default_locale;
if (isset($_REQUEST['set_locale']) && in_array($_REQUEST['set_locale'], array_keys($locales))) {
2013-11-25 19:12:19 +01:00
$_SESSION['locale'] = $_REQUEST['set_locale'];
} elseif (! isset($_SESSION['locale'])) {
2013-11-25 19:12:19 +01:00
$_SESSION['locale'] = $default_locale;
}
2013-11-25 19:12:19 +01:00
2013-12-26 13:34:48 +01:00
gettext_locale();
2017-01-01 15:25:21 +01:00
bindtextdomain('default', __DIR__ . '../../locale');
2013-11-25 19:12:19 +01:00
bind_textdomain_codeset('default', 'UTF-8');
textdomain('default');
}
2013-12-26 13:34:48 +01:00
/**
* Swich gettext locale.
*
* @param string $locale
*/
function gettext_locale($locale = null) {
if ($locale == null) {
2013-12-26 13:34:48 +01:00
$locale = $_SESSION['locale'];
}
2013-12-26 13:34:48 +01:00
putenv('LC_ALL=' . $locale);
setlocale(LC_ALL, $locale);
}
2013-11-25 19:12:19 +01:00
/**
* Renders language selection.
*
* @return string
*/
function make_langselect() {
global $locales;
$URL = $_SERVER["REQUEST_URI"] . (strpos($_SERVER["REQUEST_URI"], "?") > 0 ? '&' : '?') . "set_locale=";
2016-09-30 17:08:20 +02:00
$items = [];
foreach ($locales as $locale => $name) {
2014-08-23 02:16:12 +02:00
$items[] = toolbar_item_link(htmlspecialchars($URL) . $locale, '', '<img src="pic/flag/' . $locale . '.png" alt="' . $name . '" title="' . $name . '"> ' . $name);
}
2014-09-24 15:11:50 +02:00
return $items;
2013-11-25 19:12:19 +01:00
}
?>