Christian Seiler: httpd.conf

Beitrag lesen

Hallo Christoph, Hallo Linker, Hallo andy,

Ein paar Infos könnt ihr aber (wenn ihr wollt) unter http://www.php4-forum.de/forum.php3?nr=122469 nachlesen.

Tja, da kann ich jetzt doch etwas sinnvolles beitragen. Ich habe vor einiger Zeit (Sommer, wenn ich mich richtig erinnere) eine PHP-Funktion geschrieben, die die Sprache, die der Browser mitsendet, gemäß RFC2616 parsed. (zumindest *hoffe* ich, dass das korrekt ist)

Die Funktion erwartet keine Parameter, erwartet aber, dass eine Variable $config existiert, die so initialisiert werden kann:

$config["default_language"] = "de"; // Standardsprache
$config["allowed_languages"] = array ("de-at", "de", "it"); // welche Sprachen sind erlaubt?

Die Funktion gibt die vom Benutzer präferierte Sprache zurück.

Ich stelle die jetzt mal einfach so in den Raum:

---------------------------------------------------------------------------------------
// detect browser language
function lang_getfrombrowser () {
        // import configuration
        global $config;

// did the browser send any information
        if (empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
                // no? => return default language
                return $config["default_language"];
        }

// split the array
        $accepted_languages = split(", ?", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);

// init the values to default values
        $current_lang = $config["default_language"];
        $current_q = 0;

// no go through all the languages specified
        foreach ($accepted_languages as $accepted_language) {
                // tryp to get all information about it
                $res = preg_match ("/([A-Za-z]{1,8})(-[A-Za-z\-]{1,8})?(;q=(.*))?/", $accepted_language, $matches);

// did it match
                if (!$res) {
                        // no? then ignore it
                        continue;
                }

// now separate the language codes
                $main_code = strtolower($matches[1]);
                $extra_code = strtolower($matches[2]);

// was a quality supplied?
                if (isset($matches[4])) {
                        // use this one
                        $lang_quality = (float)$matches[4];
                } else {
                        // compability mode: assume quality of 1
                        $lang_quality = 1.0;
                }
                // now see if the language is allowed
                if (in_array($main_code, $config["allowed_languages"])) {
                        // and if it also has a higher quality than a previous one
                        if ($lang_quality > $current_q) {
                                // use that language
                                $current_lang = $lang_main;
                                $current_q = $lang_quality;
                        }
                }

// now see if the combination is allowed
                if (in_array($main_code . "-" . $extra_code, $config["allowed_languages"])) {
                        // ad if the quality is higher...
                        if ($lang_quality > $current_q) {
                                // use that language
                                $current_lang = $lang_main . "-" . $lang_extra;
                                $current_q = $lang_quality;
                        }
                }
        }

// return the language found
        return $current_lang;
}
---------------------------------------------------------------------------------------

Grüße,

Christian

--
Ich bitte darum, dass ein Themenbereich (BARRIEREFREIHEIT) eingerichtet wird.
0 39

httpd.conf

andy
  • webserver
  1. 0
    Christoph Schnauß
    1. 0
      Christian Seiler
      1. 0
        Christoph Schnauß
        1. 0
          Christian Seiler
        2. 0
          Der Linker aus www.php4-forum.de
          1. 0
            Christoph Schnauß
            1. 0
              Christian Seiler
              1. 0

                RFC2616-konformes parsen von Accept-Language

                Christian Seiler
                • php
                1. 0
                  Orlando
                  1. 0
                    Christian Seiler
                    1. 0
                      Orlando
                      1. 0
                        Christian Seiler
                        • menschelei
                        1. 0

                          (OT) Kein spezifisches Thema ;-)

                          Orlando
                          1. 0

                            (OT) Nichts besonderes ;-)

                            Christian Seiler
                            1. 0

                              (OT) Chat-o-Rama ;-)

                              Orlando
                              1. 0

                                (OT) Ähm, ja, irgendein Titel halt ;-)

                                Christian Seiler
  2. 0
    Christian Seiler
  3. 0
    Sven Rautenberg
  4. 0
    andy
  5. 0
    andy
  6. 0
    andy
    1. 0

      ACCEPT_LANGUAGE

      Der Linker
      • php
      1. 0
        Christian Seiler
        1. 0
          Der Linker
          1. 0
            Linker
          2. 0
            Christian Seiler
            1. 0
              Der Linker
              1. 0
                Christian Seiler
                1. 0
                  Der Linker
                  1. 0
                    Christian Seiler
                    1. 0
                      Der Linker
                      1. 0
                        Christian Seiler
                        1. 0
                          Der Linker
                          1. 0
                            Christian Seiler
                            1. 0

                              Auf Wiedersehen

                              Der Linker
                              • menschelei
                        2. 0
                          Harry
                          1. 0
                            Harry
                          2. 0
                            Christian Seiler