updated language handling

This commit is contained in:
Roland Gruber 2014-02-02 12:36:12 +00:00
parent 7cebc19276
commit afc9b6e1a5
5 changed files with 122 additions and 71 deletions

View File

@ -447,11 +447,11 @@ class samba3domain {
*/ */
function get_preg($argument, $regexp) { function get_preg($argument, $regexp) {
/* Bug in php preg_match doesn't work correct with utf8 */ /* Bug in php preg_match doesn't work correct with utf8 */
$language = explode(":", $_SESSION['language']); $language = $_SESSION['language'];
$language2 = explode ('.', $language[0]); $language2 = explode ('.', $language);
setlocale(LC_ALL, $language2[0]); setlocale(LC_ALL, $language2[0]);
// workaround for buggy PHP with Turkish // workaround for buggy PHP with Turkish
if (($language[0] == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) { if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
setlocale(LC_CTYPE, 'en_GB'); setlocale(LC_CTYPE, 'en_GB');
} }
// First we check "positive" cases // First we check "positive" cases
@ -581,9 +581,9 @@ function get_preg($argument, $regexp) {
if ($pregexpr!='') if ($pregexpr!='')
if (preg_match($pregexpr, $argument)) { if (preg_match($pregexpr, $argument)) {
/* Bug in php preg_match doesn't work correct with utf8 */ /* Bug in php preg_match doesn't work correct with utf8 */
setlocale(LC_ALL, $language[0]); setlocale(LC_ALL, $language);
// workaround for buggy PHP with Turkish // workaround for buggy PHP with Turkish
if (($language[0] == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) { if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
setlocale(LC_CTYPE, 'en_GB'); setlocale(LC_CTYPE, 'en_GB');
} }
return true; return true;
@ -604,17 +604,17 @@ function get_preg($argument, $regexp) {
if ($pregexpr!='') if ($pregexpr!='')
if (!preg_match($pregexpr, $argument)) { if (!preg_match($pregexpr, $argument)) {
/* Bug in php preg_match doesn't work correct with utf8 */ /* Bug in php preg_match doesn't work correct with utf8 */
setlocale(LC_ALL, $language[0]); setlocale(LC_ALL, $language);
// workaround for buggy PHP with Turkish // workaround for buggy PHP with Turkish
if (($language[0] == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) { if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
setlocale(LC_CTYPE, 'en_GB'); setlocale(LC_CTYPE, 'en_GB');
} }
return true; return true;
} }
/* Bug in php preg_match doesn't work correct with utf8 */ /* Bug in php preg_match doesn't work correct with utf8 */
setlocale(LC_ALL, $language[0]); setlocale(LC_ALL, $language);
// workaround for buggy PHP with Turkish // workaround for buggy PHP with Turkish
if (($language[0] == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) { if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
setlocale(LC_CTYPE, 'en_GB'); setlocale(LC_CTYPE, 'en_GB');
} }
return false; return false;

View File

@ -55,21 +55,30 @@ function setSSLCaCert() {
* Sets language settings for automatic translation * Sets language settings for automatic translation
*/ */
function setlanguage() { function setlanguage() {
$code = 'en_GB.utf8';
$encoding = 'UTF-8';
if (!isset($_SESSION['language'])) { if (!isset($_SESSION['language'])) {
$_SESSION['language'] = "en_GB.utf8:UTF-8:English (Great Britain)"; $_SESSION['language'] = "en_GB.utf8";
} }
$language = explode(":", trim($_SESSION['language'])); $possibleLanguages = getLanguages();
putenv("LANG=" . $language[0]); // e.g. LANG=de_DE foreach ($possibleLanguages as $lang) {
setlocale(LC_ALL, $language[0]); // set LC_ALL if ($lang->code == $_SESSION['language']) {
$code = $lang->code;
$encoding = $lang->encoding;
break;
}
}
putenv("LANG=" . $code); // e.g. LANG=de_DE
setlocale(LC_ALL, $code); // set LC_ALL
// workaround for buggy PHP with Turkish // workaround for buggy PHP with Turkish
if (($language[0] == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) { if (($code == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
setlocale(LC_CTYPE, 'en_GB'); setlocale(LC_CTYPE, 'en_GB');
} }
$locdir = substr(__FILE__, 0, strlen(__FILE__) - 15) . "/locale"; // set path to translations $locdir = substr(__FILE__, 0, strlen(__FILE__) - 15) . "/locale"; // set path to translations
bindtextdomain("messages", $locdir); bindtextdomain("messages", $locdir);
bind_textdomain_codeset("messages", $language[1]); bind_textdomain_codeset("messages", $encoding);
textdomain("messages"); textdomain("messages");
header("Content-type: text/html; charset=" . $language[1], true); header("Content-type: text/html; charset=" . $encoding, true);
} }
/** /**
@ -229,6 +238,56 @@ function isAccountTypeHidden($type) {
return isset($typeSettings['hidden_' . $type]) && ($typeSettings['hidden_' . $type] == true); return isset($typeSettings['hidden_' . $type]) && ($typeSettings['hidden_' . $type] == true);
} }
/**
* Returns a list of all supported languages.
*
* @return array languages
*/
function getLanguages() {
$languages = array();
// loading available languages from language.conf file
$languagefile = dirname(__FILE__) . "/../config/language";
if(is_file($languagefile) == true) {
$file = fopen($languagefile, "r");
while(!feof($file)) {
$line = fgets($file, 1024);
if($line == "" || $line == "\n" || $line[0] == "#") continue; // ignore comment and empty lines
$value = explode(":", $line);
$languages[] = new LAMLanguage($value[0], $value[1], $value[2]);
}
fclose($file);
}
return $languages;
}
/**
* Represents a supported language.
*
* @package configuration
*/
class LAMLanguage {
/** language code (e.g. en_GB.utf8) */
public $code;
/** character encoding (e.g. UTF-8) */
public $encoding;
/** description for GUI */
public $description;
/**
* Constructor
*
* @param String $code language code (e.g. en_GB.utf8)
* @param String $encoding character encoding (e.g. UTF-8)
* @param String $description description for GUI
*/
public function __construct($code, $encoding, $description) {
$this->code = $code;
$this->encoding = $encoding;
$this->description = $description;
}
}
/** /**
* This class manages .conf files. * This class manages .conf files.
* *

View File

@ -3,7 +3,7 @@
$Id$ $Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2012 Roland Gruber Copyright (C) 2003 - 2014 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -284,18 +284,17 @@ $container->addElement(new htmlSpacer(null, '10px'), true);
// language // language
$languageSettingsContent = new htmlTable(); $languageSettingsContent = new htmlTable();
// read available languages // read available languages
$languagefile = "../../config/language"; $possibleLanguages = getLanguages();
if(is_file($languagefile)) { $defaultLanguage = array('en_GB.utf8');
$file = fopen($languagefile, "r"); if(!empty($possibleLanguages)) {
while(!feof($file)) { foreach ($possibleLanguages as $lang) {
$line = fgets($file, 1024); $languages[$lang->description] = $lang->code;
if($line == "\n" || $line[0] == "#" || $line == "") continue; // ignore comment and empty lines if (strpos($conf->get_defaultLanguage(), $lang->code) === 0) {
$line = chop($line); $defaultLanguage = array($lang->code);
$entry = explode(":", $line); break;
$languages[$entry[2]] = $line; }
} }
fclose($file); $languageSelect = new htmlTableExtendedSelect('lang', $languages, $defaultLanguage, _("Default language"), '209');
$languageSelect = new htmlTableExtendedSelect('lang', $languages, array($conf->get_defaultLanguage()), _("Default language"), '209');
$languageSelect->setHasDescriptiveElements(true); $languageSelect->setHasDescriptiveElements(true);
$languageSettingsContent->addElement($languageSelect, true); $languageSettingsContent->addElement($languageSelect, true);
} }

View File

@ -4,7 +4,7 @@ $Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner Copyright (C) 2003 - 2006 Michael Duergner
2005 - 2013 Roland Gruber 2005 - 2014 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -104,22 +104,43 @@ if (!isset($default_Config->default) || !in_array($default_Config->default, $pro
$error_message = _('No default profile set. Please set it in the server profile configuration.'); $error_message = _('No default profile set. Please set it in the server profile configuration.');
} }
$possibleLanguages = getLanguages();
$encoding = 'UTF-8';
if (isset($_COOKIE['lam_last_language'])) { if (isset($_COOKIE['lam_last_language'])) {
$_SESSION['language'] = $_COOKIE['lam_last_language']; foreach ($possibleLanguages as $lang) {
if (strpos($_COOKIE['lam_last_language'], $lang->code) === 0) {
$_SESSION['language'] = $lang->code;
$encoding = $lang->encoding;
break;
}
}
} }
elseif (!empty($_SESSION["config"])) { elseif (!empty($_SESSION["config"])) {
$_SESSION['language'] = $_SESSION["config"]->get_defaultLanguage(); $defaultLang = $_SESSION["config"]->get_defaultLanguage();
foreach ($possibleLanguages as $lang) {
if (strpos($defaultLang, $lang->code) === 0) {
$_SESSION['language'] = $lang->code;
$encoding = $lang->encoding;
break;
}
}
} }
else { else {
$_SESSION['language'] = 'en_GB.utf8:UTF-8:English (Great Britain)'; $_SESSION['language'] = 'en_GB.utf8';
} }
if (isset($_POST['language'])) { if (isset($_POST['language'])) {
$_SESSION['language'] = htmlspecialchars($_POST['language']); // Write selected language in session foreach ($possibleLanguages as $lang) {
if (strpos($_POST['language'], $lang->code) === 0) {
$_SESSION['language'] = $lang->code;
$encoding = $lang->encoding;
break;
}
}
} }
$current_language = explode(":",$_SESSION['language']);
$_SESSION['header'] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\n"; $_SESSION['header'] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\n";
$_SESSION['header'] .= "<html>\n<head>\n"; $_SESSION['header'] .= "<html>\n<head>\n";
$_SESSION['header'] .= "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . $current_language[1] . "\">\n"; $_SESSION['header'] .= "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . $encoding . "\">\n";
$_SESSION['header'] .= "<meta http-equiv=\"pragma\" content=\"no-cache\">\n <meta http-equiv=\"cache-control\" content=\"no-cache\">"; $_SESSION['header'] .= "<meta http-equiv=\"pragma\" content=\"no-cache\">\n <meta http-equiv=\"cache-control\" content=\"no-cache\">";
/** /**
@ -148,32 +169,7 @@ function display_LoginPage($config_object, $cfgMain) {
setcookie("Key", base64_encode($key), 0, "/"); setcookie("Key", base64_encode($key), 0, "/");
setcookie("IV", base64_encode($iv), 0, "/"); setcookie("IV", base64_encode($iv), 0, "/");
} }
// loading available languages from language.conf file
$languagefile = "../config/language";
if(is_file($languagefile) == True)
{
$file = fopen($languagefile, "r");
$i = 0;
while(!feof($file))
{
$line = fgets($file, 1024);
if($line == "" || $line == "\n" || $line[0] == "#") continue; // ignore comment and empty lines
$value = explode(":", $line);
$languages[$i]["link"] = $value[0] . ":" . $value[1];
$languages[$i]["descr"] = $value[2];
if(trim($line) == trim($_SESSION["language"]))
{
$languages[$i]["default"] = "YES";
}
else
{
$languages[$i]["default"] = "NO";
}
$i++;
}
fclose($file);
}
$profiles = getConfigProfiles(); $profiles = getConfigProfiles();
setlanguage(); // setting correct language setlanguage(); // setting correct language
@ -403,12 +399,13 @@ function display_LoginPage($config_object, $cfgMain) {
$languageLabel->alignment = htmlElement::ALIGN_RIGHT; $languageLabel->alignment = htmlElement::ALIGN_RIGHT;
$table->addElement($languageLabel); $table->addElement($languageLabel);
$table->addElement($gap); $table->addElement($gap);
$possibleLanguages = getLanguages();
$languageList = array(); $languageList = array();
$defaultLanguage = array(); $defaultLanguage = array();
for($i = 0; $i < count($languages); $i++) { foreach ($possibleLanguages as $lang) {
$languageList[$languages[$i]["descr"]] = $languages[$i]["link"] . ":" . $languages[$i]["descr"]; $languageList[$lang->description] = $lang->code;
if($languages[$i]["default"] == "YES") { if (strpos(trim($_SESSION["language"]), $lang->code) === 0) {
$defaultLanguage[] = $languages[$i]["link"] . ":" . $languages[$i]["descr"]; $defaultLanguage[] = $lang->code;
} }
} }
$languageSelect = new htmlSelect('language', $languageList, $defaultLanguage); $languageSelect = new htmlSelect('language', $languageList, $defaultLanguage);

View File

@ -3,7 +3,7 @@
$Id$ $Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2012 Roland Gruber Copyright (C) 2003 - 2014 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -22,7 +22,7 @@ $Id$
*/ */
/** /**
* This is the main window. The user and group lists will be shown in this frameset. * This page redirects to the correct start page after login.
* *
* @package main * @package main
* @author Roland Gruber * @author Roland Gruber
@ -47,10 +47,6 @@ for ($i = 0; $i < sizeof($types); $i++) {
if (!$res && !in_array($conf->get_Suffix($types[$i]), $new_suffs)) $new_suffs[] = $conf->get_Suffix($types[$i]); if (!$res && !in_array($conf->get_Suffix($types[$i]), $new_suffs)) $new_suffs[] = $conf->get_Suffix($types[$i]);
} }
// get encoding
$lang = explode(":",$_SESSION['language']);
$lang = $lang[1];
// display page to add suffixes, if needed // display page to add suffixes, if needed
if ((sizeof($new_suffs) > 0) && checkIfWriteAccessIsAllowed()) { if ((sizeof($new_suffs) > 0) && checkIfWriteAccessIsAllowed()) {
metaRefresh("initsuff.php?suffs='" . implode(";", $new_suffs)); metaRefresh("initsuff.php?suffs='" . implode(";", $new_suffs));