125 lines
3.0 KiB
PHP
125 lines
3.0 KiB
PHP
<?php
|
|
/** \file include\i18n.php
|
|
* \brief DI webinterface internationalization functions
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version 1.0
|
|
* \date 17-10-2008
|
|
*
|
|
* This file contains the internationalization. This to support multiple languages.
|
|
* This file is always included.
|
|
*/
|
|
|
|
/*
|
|
* Definitions
|
|
*/
|
|
define("DOMAIN" , "zkl-module-1");
|
|
define("LANG_DIR", $_PAGE_INFO['base_path'] . "locale/");
|
|
|
|
/**
|
|
* Determine prefered language (db or browser)
|
|
*
|
|
* Return: language
|
|
*/
|
|
function i18n_determine_language() {
|
|
global $_PAGE_INFO;
|
|
|
|
// Already logged on?
|
|
if (isset($_SESSION[$_PAGE_INFO['id']]['logged_on'])) {
|
|
// Retreive user i18n
|
|
$i18n_locale = db_fetch_i18n($_PAGE_INFO['login']['user']['id']);
|
|
}
|
|
else {
|
|
$i18n_locale = i18n_browser_preferred_language();
|
|
}
|
|
|
|
/*
|
|
* charset
|
|
*/
|
|
$_PAGE_INFO['charset'] = db_fetch_system_lang_charset($i18n_locale);
|
|
|
|
/*
|
|
* settext
|
|
*/
|
|
i18n_settext_language($i18n_locale);
|
|
|
|
return $i18n_locale;
|
|
}
|
|
|
|
|
|
/**
|
|
* Determine browser preffered language
|
|
*
|
|
* Return: preffered language
|
|
*/
|
|
function i18n_browser_preferred_language() {
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
|
// Default values
|
|
$locale = "en";
|
|
$q = 0.0;
|
|
$language_no_q = FALSE;
|
|
|
|
// Parse Accept languages
|
|
$user_languages = preg_split("/,\s*/", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
|
|
|
if (is_array($user_languages)) {
|
|
foreach($user_languages as $language) {
|
|
$lang_code = preg_split("/;|=/", $language);
|
|
$lang_code[0] = substr($lang_code[0], 0, 2);
|
|
|
|
// Double index without 'q' (mostly on telephone) => choose first
|
|
if (!$language_no_q) {
|
|
if (file_exists(LANG_DIR . $lang_code[0]) && ($lang_code[1] != 'q' || $q < $lang_code[2])) {
|
|
$locale = $lang_code[0];
|
|
if( $lang_code[1] != 'q') {
|
|
$q = 1.0;
|
|
$language_no_q = TRUE;
|
|
}
|
|
else {
|
|
$q = $lang_code[1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
$locale = "en";
|
|
}
|
|
|
|
return $locale;
|
|
}
|
|
|
|
|
|
/**
|
|
* Write language to environment
|
|
*/
|
|
function i18n_settext_language($i18n, $language_dir = LANG_DIR) {
|
|
global $_PAGE_INFO;
|
|
|
|
if (strlen($i18n)) {
|
|
putenv("LANGUAGE=" . $i18n);
|
|
putenv("LANG=" . $i18n);
|
|
putenv("LC_ALL=" . $i18n);
|
|
|
|
// Restore time and numeric, this to prevent javascript/mysql errors
|
|
setlocale(LC_ALL, $i18n);
|
|
setlocale(LC_TIME, 'C');
|
|
setlocale(LC_NUMERIC, 'C');
|
|
|
|
// Define charset
|
|
header("Content-Type: text/html; charset=" . $_PAGE_INFO['charset']);
|
|
}
|
|
|
|
if (strlen($language_dir)) {
|
|
$textdomain = DOMAIN;
|
|
if(
|
|
preg_match("|^.*/release/([^/]+)/.+$|", $language_dir, $release) ||
|
|
preg_match("|^/home/([^/]+)/.*$|", $language_dir, $release)
|
|
) {
|
|
$textdomain .= "-" . $release[1];
|
|
}
|
|
bindtextdomain($textdomain, $language_dir);
|
|
textdomain($textdomain);
|
|
}
|
|
}
|
|
?>
|