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) {
/* Bug in php preg_match doesn't work correct with utf8 */
$language = explode(":", $_SESSION['language']);
$language2 = explode ('.', $language[0]);
$language = $_SESSION['language'];
$language2 = explode ('.', $language);
setlocale(LC_ALL, $language2[0]);
// 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');
}
// First we check "positive" cases
@ -581,9 +581,9 @@ function get_preg($argument, $regexp) {
if ($pregexpr!='')
if (preg_match($pregexpr, $argument)) {
/* 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
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');
}
return true;
@ -604,17 +604,17 @@ function get_preg($argument, $regexp) {
if ($pregexpr!='')
if (!preg_match($pregexpr, $argument)) {
/* 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
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');
}
return true;
}
/* 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
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');
}
return false;

View File

@ -55,21 +55,30 @@ function setSSLCaCert() {
* Sets language settings for automatic translation
*/
function setlanguage() {
$code = 'en_GB.utf8';
$encoding = 'UTF-8';
if (!isset($_SESSION['language'])) {
$_SESSION['language'] = "en_GB.utf8:UTF-8:English (Great Britain)";
$_SESSION['language'] = "en_GB.utf8";
}
$language = explode(":", trim($_SESSION['language']));
putenv("LANG=" . $language[0]); // e.g. LANG=de_DE
setlocale(LC_ALL, $language[0]); // set LC_ALL
$possibleLanguages = getLanguages();
foreach ($possibleLanguages as $lang) {
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
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');
}
$locdir = substr(__FILE__, 0, strlen(__FILE__) - 15) . "/locale"; // set path to translations
bindtextdomain("messages", $locdir);
bind_textdomain_codeset("messages", $language[1]);
bind_textdomain_codeset("messages", $encoding);
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);
}
/**
* 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.
*

View File

@ -3,7 +3,7 @@
$Id$
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
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
$languageSettingsContent = new htmlTable();
// read available languages
$languagefile = "../../config/language";
if(is_file($languagefile)) {
$file = fopen($languagefile, "r");
while(!feof($file)) {
$line = fgets($file, 1024);
if($line == "\n" || $line[0] == "#" || $line == "") continue; // ignore comment and empty lines
$line = chop($line);
$entry = explode(":", $line);
$languages[$entry[2]] = $line;
$possibleLanguages = getLanguages();
$defaultLanguage = array('en_GB.utf8');
if(!empty($possibleLanguages)) {
foreach ($possibleLanguages as $lang) {
$languages[$lang->description] = $lang->code;
if (strpos($conf->get_defaultLanguage(), $lang->code) === 0) {
$defaultLanguage = array($lang->code);
break;
}
}
fclose($file);
$languageSelect = new htmlTableExtendedSelect('lang', $languages, array($conf->get_defaultLanguage()), _("Default language"), '209');
$languageSelect = new htmlTableExtendedSelect('lang', $languages, $defaultLanguage, _("Default language"), '209');
$languageSelect->setHasDescriptiveElements(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/)
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
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.');
}
$possibleLanguages = getLanguages();
$encoding = 'UTF-8';
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"])) {
$_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 {
$_SESSION['language'] = 'en_GB.utf8:UTF-8:English (Great Britain)';
$_SESSION['language'] = 'en_GB.utf8';
}
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'] .= "<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\">";
/**
@ -148,32 +169,7 @@ function display_LoginPage($config_object, $cfgMain) {
setcookie("Key", base64_encode($key), 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();
setlanguage(); // setting correct language
@ -403,12 +399,13 @@ function display_LoginPage($config_object, $cfgMain) {
$languageLabel->alignment = htmlElement::ALIGN_RIGHT;
$table->addElement($languageLabel);
$table->addElement($gap);
$possibleLanguages = getLanguages();
$languageList = array();
$defaultLanguage = array();
for($i = 0; $i < count($languages); $i++) {
$languageList[$languages[$i]["descr"]] = $languages[$i]["link"] . ":" . $languages[$i]["descr"];
if($languages[$i]["default"] == "YES") {
$defaultLanguage[] = $languages[$i]["link"] . ":" . $languages[$i]["descr"];
foreach ($possibleLanguages as $lang) {
$languageList[$lang->description] = $lang->code;
if (strpos(trim($_SESSION["language"]), $lang->code) === 0) {
$defaultLanguage[] = $lang->code;
}
}
$languageSelect = new htmlSelect('language', $languageList, $defaultLanguage);

View File

@ -3,7 +3,7 @@
$Id$
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
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
* @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]);
}
// get encoding
$lang = explode(":",$_SESSION['language']);
$lang = $lang[1];
// display page to add suffixes, if needed
if ((sizeof($new_suffs) > 0) && checkIfWriteAccessIsAllowed()) {
metaRefresh("initsuff.php?suffs='" . implode(";", $new_suffs));