[Mapbender-commits] r1541 - trunk/mapbender/http/classes
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Thu Jul 26 09:30:34 EDT 2007
Author: christoph
Date: 2007-07-26 09:30:34 -0400 (Thu, 26 Jul 2007)
New Revision: 1541
Added:
trunk/mapbender/http/classes/class_locale.php
Log:
a new class setting the locale (with a optional language parameter)
Added: trunk/mapbender/http/classes/class_locale.php
===================================================================
--- trunk/mapbender/http/classes/class_locale.php (rev 0)
+++ trunk/mapbender/http/classes/class_locale.php 2007-07-26 13:30:34 UTC (rev 1541)
@@ -0,0 +1,178 @@
+<?php
+# $Id:$
+# http://www.mapbender.org/index.php/class_locale.php
+# Copyright (C) 2002 CCGIS
+#
+# 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+include_once(dirname(__FILE__)."/../../conf/mapbender.conf");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+
+class Mb_locale {
+ var $knownLanguages = null;
+ var $systemLocales = null;
+ var $browserLanguages = null;
+ var $os = null;
+ var $name = null;
+ var $defaultLanguage = "de";
+ var $status = "No locale set.";
+
+ function Mb_locale($languageId) {
+ if (!$this->setCurrentLocale($languageId)) {
+ $e = new Mb_exception("Locale with language ID " . $languageId . " could not be set.");
+ }
+ }
+
+ /**
+ * Get the current locale, evaluating GET/POST variables, browser languages
+ * and a default locale (in that preference)
+ *
+ * @returns current locale
+ */
+ function setCurrentLocale($languageId) {
+
+ // try to set the locale to $languageId
+ if (isset($languageId)) {
+ if ($this->checkAndSetLocale($languageId)) {
+ return true;
+ }
+ }
+
+ // TODO: try to set to user settings
+
+ // determine the browser setting and try to set locale according to that
+ if ($this->browserLanguage == null) {
+ $this->setBrowserLanguages();
+ }
+ foreach ($this->browserLanguages as $lang) {
+ if ($this->checkAndSetLocale($lang)) {
+ return true;
+ }
+ }
+
+ // set to default language
+ return $this->checkAndSetLocale($this->defaultLanguage);
+ }
+
+ /**
+ * checks if a locale is available; if yes, it is set via setlocale
+ *
+ * @returns true if the the locale is set successfully; otherwise false
+ */
+ function checkAndSetLocale($languageId) {
+ if ($this->os == null) {
+ $this->os = $this->guessHostOS();
+ echo $this->os;
+ }
+
+ if ($this->os != null) {
+ if ($this->isKnownLanguage($languageId)) {
+
+ if ($this->systemLocales == null) {
+ $this->setSystemLocales();
+ }
+
+ $locale = $this->systemLocales[$this->knownLanguages[$languageId]][$this->os];
+ if (setlocale(LC_ALL, $locale)) {
+ $this->name = $locale;
+ $e = new Mb_notice("locale " . $this->name . " ok on " . $this->os);
+ return true;
+ }
+ $e = new Mb_notice("locale " . $locale . " not found.");
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Guess the operating system which on which this code is running
+ * multiple methods are tested for reliably guessing the os
+ *
+ * @private
+ * @returns string with os name
+ */
+ function guessHostOS(){
+ if (strncasecmp(php_uname(), 'Windows', 7) == 0)
+ return 'windows';
+ else if (strncasecmp(php_uname(), 'Linux', 5) == 0)
+ return 'linux';
+ else if (strncasecmp(php_uname(), 'OpenBSD', 7) == 0)
+ return 'bsd';
+ else {
+ throw new Mb_exception('unknown platform: could not interpret uname. php_uname() returned '. php_uname().'. Please report to MB developers');
+ return null;
+ }
+ }
+
+ /**
+ * checks if a language is supported
+ *
+ * @returns true if the language is supported; otherwise false
+ */
+ function isKnownLanguage($languageId) {
+ if ($this->knownLocales == null) {
+ $this->setKnownLanguages();
+ }
+ return array_key_exists($languageId, $this->knownLanguages);
+ }
+
+
+
+ /**
+ * determines the available Locales on this system
+ */
+ function setSystemLocales() {
+ $this->systemLocales['it_IT'] = array('linux' => 'it_IT.utf8',
+ 'windows' => 'Italian_Italy.1252',
+ 'bsd' => 'it_IT',
+ 'posix' => 'it_IT');
+ $this->systemLocales['de_DE'] = array('linux' => 'de_DE.utf8',
+ 'windows' => 'German_Germany.1252',
+ 'bsd' => 'de_DE',
+ 'posix' => 'it_IT');
+ $this->systemLocales['en_US'] = array('linux' => 'en_US.utf8',
+ 'windows' => 'English_United States.1252',
+ 'bsd' => 'en_US',
+ 'posix' => 'it_IT');
+ }
+
+ /**
+ * set the known languages
+ */
+ function setKnownLanguages() {
+ $this->knownLanguages = array('en_US' => 'en_US',
+ 'en' => 'en_US',
+ 'de_DE' => 'de_DE',
+ 'de' => 'de_DE',
+ 'it_IT' => 'it_IT',
+ 'it' => 'it_IT');
+ }
+
+ /**
+ * sets the languages accepted by the client browser
+ */
+ function setBrowserLanguages () {
+ $this->browserLanguages = array();
+
+ $bLangs = split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
+ foreach ($bLangs as $lang) {
+ if (strpos($lang, ';') === false)
+ array_push($this->browserLanguages, $lang);
+ else
+ array_push($this->browserLanguages, substr($lang, 0, strpos($lang, ';')));
+ }
+ }
+}
+?>
\ No newline at end of file
More information about the Mapbender_commits
mailing list