235 lines
6.5 KiB
PHP
235 lines
6.5 KiB
PHP
<?php
|
|
/** \file include\i18n.php
|
|
* \brief DI webinterface internationalization functions
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version $Revision: 26247 $
|
|
* \date $Date: 2016-02-29 10:40:22 +0100 (Mon, 29 Feb 2016) $
|
|
*
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Double underscore: Interface to 'ngettext()', using 'sprintf()'
|
|
*/
|
|
function __($msgid1, $msgid2, $n)
|
|
{
|
|
$args = func_get_args();
|
|
// delete 'msgid1' and 'msgid2'
|
|
array_splice($args, 0, 2);
|
|
// format the string
|
|
return vsprintf(ngettext($msgid1, $msgid2, $n), $args);
|
|
}
|
|
|
|
/**
|
|
* Triple underscore: Format a string in the style of Microsoft's String::Format
|
|
*
|
|
* NB: Using the Microsoft style is somewhat more convenient than 'sprintf'
|
|
* as PHP does not implement positional parameters.
|
|
*/
|
|
function ___($fmt)
|
|
{
|
|
// Build argument list
|
|
$args = array_slice(func_get_args(), 1);
|
|
|
|
// if( PHP_VERSION_ID >= 50300 ) {
|
|
// return preg_replace_callback(
|
|
// "/({[0-9]+})/",
|
|
// function($match) use ($args) { return $args[$match[0]]; },
|
|
// _($fmt)
|
|
// );
|
|
// }
|
|
// else {
|
|
return preg_replace(
|
|
"/\{([0-9]+)\}/e",
|
|
'$args[\1]',
|
|
_($fmt)
|
|
);
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* Read translation from the database table 'vertaling'
|
|
*
|
|
* Inputs:
|
|
* - id Database id _or_ array of strings (all translations)
|
|
* _or_ string to translate via 'gettext()'
|
|
* when 'field' is not set (for symmetry with, erm, well
|
|
* the function name :-)
|
|
* - field Table and field in the table 'vertaling'
|
|
* - i18n Target language (auto-detect if not set)
|
|
*
|
|
* Returns:
|
|
* - Translation from the table 'vertaling' or via 'gettext()' when 'field' is
|
|
* not set.
|
|
* - FALSE on error
|
|
*/
|
|
function i18n_translate($id, $field = NULL, $i18n = NULL)
|
|
{
|
|
// Determine language to use if not passed as a parameter
|
|
if( !$i18n ) {
|
|
$i18n = $_SESSION[$_PAGE_INFO['id']]['i18n'];
|
|
if( !$i18n ) $i18n = i18n_browser_preferred_language();
|
|
}
|
|
|
|
// default return value to indicate that we cannot translate 'id'
|
|
$str = FALSE;
|
|
|
|
if( is_array($id) ) {
|
|
// prefetched array with all translations
|
|
// try the specified target language
|
|
if( $i18n ) $str = $id[$i18n];
|
|
|
|
// search other languages
|
|
if( !$str && function_exists("db_fetch_system_lang") ) {
|
|
// Get all languages, in the user's preference
|
|
$i18n_languages = db_fetch_system_lang();
|
|
|
|
foreach ($i18n_languages as $i18n => $lang) {
|
|
$str = $id[$i18n];
|
|
|
|
if( $str ) return $str;
|
|
}
|
|
}
|
|
else if( $str ) return $str;
|
|
}
|
|
else if( !$field ) {
|
|
// database field is not set: simply translate via 'gettext()'
|
|
$str = _($id);
|
|
}
|
|
else if( $i18n && function_exists("db_fetch_item") ) {
|
|
// try the specified target language
|
|
$str = db_fetch_item("SELECT tekst FROM vertaling WHERE id=" . $id . " AND field='" . $field . "' AND i18n='" . $i18n . "'");
|
|
}
|
|
|
|
// search the database for other languages when not found above
|
|
if( !$str && is_numeric($id) && $field && function_exists("db_fetch_system_lang") && function_exists("db_fetch_item") ) {
|
|
// Get all languages, in the user's preference
|
|
$i18n_languages = db_fetch_system_lang();
|
|
|
|
foreach ($i18n_languages as $i18n => $lang) {
|
|
$str = db_fetch_item("SELECT tekst FROM vertaling WHERE id=" . $id . " AND field='" . $field . "' AND i18n='" . $i18n . "'");
|
|
|
|
if( $str ) return $str;
|
|
}
|
|
}
|
|
|
|
// return translated string, or FALSE on error
|
|
return $str;
|
|
}
|
|
|
|
?>
|