2021-07-28 17:13:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// Gettext text domain
|
|
|
|
define('TEXT_DOMAIN', 'DEFAULT');
|
|
|
|
|
2022-04-24 16:46:38 +02:00
|
|
|
/**
|
2021-07-28 17:13:10 +02:00
|
|
|
* List available translation languages
|
|
|
|
*
|
2022-04-24 16:46:38 +02:00
|
|
|
* @param $as_locales boolean If true, locale names will be return instead
|
|
|
|
* of primary languages (optional, default: false)
|
2021-07-28 17:13:10 +02:00
|
|
|
*
|
2022-04-24 16:46:38 +02:00
|
|
|
* @return array Array of available translation languages (or locales)
|
2021-07-28 17:13:10 +02:00
|
|
|
*/
|
|
|
|
function get_available_langs($as_locales=false) {
|
|
|
|
global $root_lang_dir;
|
|
|
|
if (!is_dir($root_lang_dir))
|
|
|
|
logging('FATAL', "Root land directory not found ($root_lang_dir)");
|
|
|
|
$langs = array(($as_locales?'en_US.UTF8':'en'));
|
|
|
|
if ($dh = opendir($root_lang_dir)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if (!is_dir($root_lang_dir . '/' . $file) || in_array($file, array('.', '..')))
|
|
|
|
continue;
|
|
|
|
if ($as_locales) {
|
|
|
|
$langs[] = $file;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$lang = Locale::getPrimaryLanguage($file);
|
|
|
|
if (!in_array($lang, $langs))
|
|
|
|
$langs[] = $lang;
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
$langs = array_unique($langs);
|
|
|
|
logging('TRACE', 'Available '.($as_locales?'locales':'languages').': '.implode(', ', $langs));
|
|
|
|
return $langs;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:46:38 +02:00
|
|
|
/**
|
2021-07-28 17:13:10 +02:00
|
|
|
* Get locale name corresponding to specified translation language
|
|
|
|
*
|
2022-04-24 16:46:38 +02:00
|
|
|
* @param $lang string The translation language
|
|
|
|
* @param $default string|null Default locale name to return if any available translation
|
|
|
|
* locales matched with the specified language
|
|
|
|
* (optional, default: $default_locale)
|
|
|
|
* @return string Corresponding locale
|
2021-07-28 17:13:10 +02:00
|
|
|
*/
|
|
|
|
function lang2locale($lang, $default=null) {
|
|
|
|
global $default_locale;
|
|
|
|
if (is_null($default))
|
|
|
|
$default = $default_locale;
|
|
|
|
foreach (get_available_langs(true) as $locale) {
|
|
|
|
if (strpos($locale, $lang) === false)
|
|
|
|
continue;
|
|
|
|
return $locale;
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:46:38 +02:00
|
|
|
/**
|
|
|
|
* Helper function: just mark message for translation
|
|
|
|
*
|
|
|
|
* @param string $msg The message to translate
|
|
|
|
*
|
2022-04-24 17:43:44 +02:00
|
|
|
* @return string The message without transformation
|
2022-04-24 16:46:38 +02:00
|
|
|
*/
|
2021-07-28 17:13:10 +02:00
|
|
|
function ___($msg) {
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:46:38 +02:00
|
|
|
/**
|
2021-07-28 17:13:10 +02:00
|
|
|
* Initialize translation system
|
|
|
|
*
|
|
|
|
* Detect best translation language and configure the translation
|
|
|
|
* system.
|
|
|
|
*
|
2022-04-24 16:46:38 +02:00
|
|
|
* @return void
|
2021-07-28 17:13:10 +02:00
|
|
|
*/
|
|
|
|
function init_translation() {
|
2022-04-24 16:46:38 +02:00
|
|
|
global $root_dir_path, $root_lang_dir, $default_locale, $smarty;
|
2021-07-28 17:13:10 +02:00
|
|
|
$root_lang_dir = "$root_dir_path/lang";
|
|
|
|
|
|
|
|
if (!class_exists('Locale')) {
|
|
|
|
logging('ERROR', 'Locale PHP class does not exist. May be php-intl is not installed?');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$available_langs = get_available_langs();
|
|
|
|
if (php_sapi_name() != "cli") {
|
2021-10-07 20:46:13 +02:00
|
|
|
if (isset($_REQUEST['lang']) && in_array($_REQUEST['lang'], $available_langs)) {
|
|
|
|
$lang = $_REQUEST['lang'];
|
|
|
|
logging('TRACE', "Select lang from request parameter: '$lang'");
|
|
|
|
}
|
|
|
|
elseif (isset($_SESSION['lang']) && in_array($_SESSION['lang'], $available_langs) && !isset($_REQUEST['reset_lang'])) {
|
|
|
|
$lang = $_SESSION['lang'];
|
|
|
|
logging('TRACE', "Restore lang from session: '$lang'");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$lang = Locale::lookup(
|
|
|
|
get_available_langs(),
|
|
|
|
Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']),
|
|
|
|
true,
|
|
|
|
Locale::getPrimaryLanguage($default_locale)
|
|
|
|
);
|
|
|
|
logging('TRACE', "Best lang found is '$lang'");
|
|
|
|
}
|
2021-07-28 17:13:10 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$lang = null;
|
|
|
|
$sys_current = getenv('LC_ALL');
|
|
|
|
if (!$sys_current)
|
|
|
|
$sys_current = getenv('LANG');
|
|
|
|
if ($sys_current)
|
|
|
|
$lang = Locale::getPrimaryLanguage($sys_current);
|
|
|
|
if (is_null($lang)) {
|
|
|
|
logging('TRACE', 'No configured lang detected from CLI env, use default.');
|
|
|
|
$lang = Locale::getPrimaryLanguage($default_locale);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logging('TRACE', "Lang detected from CLI env : '$lang'");
|
|
|
|
}
|
2021-10-07 20:46:13 +02:00
|
|
|
|
|
|
|
// Keep selected lang in session
|
|
|
|
$_SESSION['lang'] = $lang;
|
2021-07-28 17:13:10 +02:00
|
|
|
|
|
|
|
$locale = lang2locale($lang);
|
|
|
|
logging('TRACE', "Matching locale found with language '$lang' is '$locale'");
|
|
|
|
|
|
|
|
// Gettext firstly look the LANGUAGE env variable, so set it
|
2021-10-07 20:46:13 +02:00
|
|
|
if (!putenv("LANGUAGE=$locale"))
|
|
|
|
logging('ERROR', "Fail to set LANGUAGE variable in environnement to '$locale'");
|
2021-07-28 17:13:10 +02:00
|
|
|
|
|
|
|
// Set the locale
|
|
|
|
if (setlocale(LC_ALL, $locale) === false)
|
|
|
|
logging('ERROR', "An error occured setting locale to '$locale'");
|
|
|
|
|
|
|
|
// Configure and set the text domain
|
|
|
|
$fullpath = bindtextdomain(TEXT_DOMAIN, $root_lang_dir);
|
|
|
|
logging('TRACE', "Text domain fullpath is '$fullpath'.");
|
|
|
|
logging('TRACE', "Text domain is '".textdomain(TEXT_DOMAIN)."'.");
|
2021-10-07 20:46:13 +02:00
|
|
|
logging('TRACE', "Test: "._('Hello world !'));
|
2022-04-24 16:46:38 +02:00
|
|
|
|
|
|
|
// JS translation file
|
2022-04-24 20:17:48 +02:00
|
|
|
$js_translation_file = "translations/$lang.js";
|
|
|
|
if (php_sapi_name() != "cli" && is_file("$root_dir_path/public_html/$js_translation_file")) {
|
|
|
|
add_js_file(array("lib/babel.js", "js/translation.js", $js_translation_file));
|
2022-04-24 16:46:38 +02:00
|
|
|
}
|
2021-07-28 17:13:10 +02:00
|
|
|
}
|