davadmin/admin/i18n.inc.php
Jan Dittberner a978fb5ef0 addresses #17
* Makefile creates/updates po/davadmin.pot, .po, and .mo files
 * admin/i18n.inc.php sets the locale and puts it in the session
 * admin/common.inc.php and admin/getgroups.php use admin/i18n.inc.php
 * po/pot.sed replaces generated header of po/davadmin.pot
2007-11-30 21:25:52 +00:00

144 lines
No EOL
4.4 KiB
PHP

<?php
/**
* Internationalization code for DAVAdmin.
*
* @author Jan Dittberner <jan@dittberner.info>
* @version $Id$
* @license GPL
* @package DAVAdmin
*
* Copyright (c) 2007 Jan Dittberner
*
* This file is part of DAVAdmin.
*
* 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 the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
/**
* Gets the language data.
*/
function get_language_data() {
static $supportedLanguages = array();
static $defaultCountry = array();
if (empty($supportedLanguages)) {
/* English */
$supportedLanguages['en']['US']['description'] = 'English (US)';
$supportedLanguages['en']['GB']['description'] = 'English (UK)';
$defaultCountry['en'] = 'US';
/* German */
$supportedLanguages['de']['DE']['description'] = 'Deutsch';
$defaultCountry['de'] = 'DE';
}
return array($supportedLanguages, $defaultCountry);
}
function _setlocale($category, $locale) {
if (($ret = setlocale($category, $locale)) !== false) {
return $ret;
}
/* Try just selecting the language */
if (($i = strpos($locale, '_')) !== false
&& ($ret = setlocale($category, substr($locale, 0, $i))) !== false) {
return $ret;
} else if (($i = strpos($locale, '_')) === false) {
list($languages, $defcountries) = get_language_data();
$locale = $locale . "_" . $defcountries[$locale];
if (($ret = setlocale($category, substr($locale, 0, $i))) !== false) {
return $ret;
}
}
/*
* Try appending some character set names; some systems (like FreeBSD)
* need this. Some require a format with hyphen (eg. Gentoo) and others
* without (eg. FreeBSD).
*/
foreach (array('UTF-8', 'UTF8', 'utf8',
'ISO8859-1', 'ISO8859-2', 'ISO8859-5', 'ISO8859-7',
'ISO8859-9', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-5',
'ISO-8859-7', 'ISO-8859-9', 'ISO-8859-15',
'EUC', 'Big5') as $charset) {
if (($ret = setlocale($category, $locale . '.' . $charset)) !== false) {
return $ret;
}
}
echo "unable to select a valid locale code";
return false;
}
function _get_supported_language_code($code, $fallback = true) {
static $supportedLanguages;
static $defaultCountry;
if (!isset($supportedLanguages)) {
list($supportedLanguages, $defaultCountry) = get_language_data();
}
list ($language, $country) = preg_split('/[-_]/', "${code}_");
$country = strtoupper($country);
if ((empty($country) || !isset($supportedLanguages[$language][$country]))
&& isset($defaultCountry[$language])) {
/* Use default country if none specified or particular country not
* supported */
$country = $defaultCountry[$language];
}
if (isset($supportedLanguages[$language][$country])) {
return "${language}_${country}";
}
if ($fallback) {
return 'en_US';
} else {
return null;
}
}
function _http_negotiate_locale() {
$accepted = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (!empty($accepted)) {
foreach(explode(",", $accepted) as $code) {
$languageCode = _get_supported_language_code($code, false);
if (isset($languageCode)) {
return $languageCode;
}
}
}
return null;
}
session_start();
$project = "davadmin";
if (isset($_GET["language"])) {
$languageCode = _get_supported_language_code($_GET["language"], false);
if (isset($languageCode)) {
$_SESSION["language"] = $languageCode;
}
}
if ($_SESSION["language"]) {
$language = $_SESSION["language"];
} else {
$language = _http_negotiate_locale();
$_SESSION["language"] = $language;
}
putenv(sprintf("LANG=%s", $_SESSION["language"]));
putenv(sprintf("LANGUAGE=%s", $_SESSION["language"]));
$_SESSION["locale"] = _setlocale(LC_ALL, $_SESSION["language"]);
bindtextdomain($project, realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR .
".." . DIRECTORY_SEPARATOR . "po"));
textdomain($project);
?>