code.fastix.org

Dateiansicht:

Datei:Projekte -> PHP:LanguageSelector -> LanguageSelector.php
md5:ef5bf0bb225035167f4b4005df7669f5
sha1:b31ae0c715e500cb8b2eea1fb86ca7d01e226472
Download-Link:Download
  1. <?php
  2.  
  3. /**
  4. ** file: LanguageSelector.php
  5. ** Author: Jörg Reinholz, fastix WebDesign & Consult, Kassel (Germany), http://www.fastix.org
  6. ** Description: This function helps to Select a Language
  7. ** License: https://code.fastix.org/lizenz.php
  8. **
  9. ** USAGE:
  10. ** include 'LanguageSelector.php'
  11.  
  12. ** Create the Objekt:
  13. ** $LanguageSelector = new LanguageSelector( array('fr', 'en', 'es', 'pt', 'de') );
  14. ## use the object:
  15. ** define ( 'LANG', $LanguageSelector -> getLang() );
  16.  
  17. ## furter you can use $LanguageSelector -> getLastSelection() to get the language faster.
  18. ##
  19.  
  20.  
  21. ** The method getLang:
  22. ** =================
  23. ** First this show what languages are given ($arLanguages)
  24. ** Example:
  25. **    FIRST SELECTION: ("site owners preselection")
  26. **      If you have called SelectOneOffAcceptedLanguages ( 'fr', 'en', 'es', 'pt', 'de'] )
  27. **            then is 'de' the selected language at this time
  28. **    SECOND SELECTION: ("user-agent preselection")
  29. **      If the User-Agent send the Accept-Language-Header and this is one of the accepted languages
  30. **            then this function overwrite the first selection with the most wanted language.
  31. **    THIRD SELECTION:
  32. **      If a cookie ($this -> storeName) stored (and resived) and this is one of the accepted languages
  33. **          then this function overwrite the second selection with this language.
  34. **    FOURTH SELECTION:
  35. **      If a hostname (eg 'DE.www.fastix.org', 'DE.fastix.org')  given and this is one of the accepted languages
  36. **          then this function overwrite the third selection with this.
  37. **    FIVE SELECTION:
  38. **      If a (virtual) directory ('fastix.org/DE/') given and this is one of the accepted languages
  39. **          then this function overwrite the fourth selection with this.
  40. **    LAST SELECTION:
  41. **      If a information into the URI-parameters given and this is one of the accepted languages
  42. **          then this function overwrite the fift selection with this.
  43.  
  44. **
  45. **    Show ISO 639-1 and  RFC 1766 for more informations.
  46. **    Read RFC2616, section 14, for the HTTP_ACCEPT_LANGUAGE-Header.
  47. **/
  48.  
  49. class LanguageSelector {
  50.  
  51.     private $arHasLanguages;
  52.     private $hashHasLanguages;
  53.     private $LastSelection = false;
  54.     public  $storeMethod = 'cookie';
  55.     public  $storeName = 'USER_SELECTED_LANGUAGE'; # used for: SESSION, COOKIE, GET, POST, HTML-Input-Name
  56.                                                   # must bee a string of numbers, letters and '_', first char a letter or '_'
  57.  
  58.     public function __construct ( $arHasLanguages ) {
  59.         $arTmp = array();
  60.         $arHash = array();
  61.         foreach ( $arHasLanguages as $s ) {
  62.             $s = trim( strtolower( $s ) );
  63.             if ( $s ) {
  64.                 $ar[] = $s;
  65.                 $hash[$s] = true;
  66.             }
  67.         }
  68.         $this -> arHasLanguages   = $ar;
  69.         $this -> hashHasLanguages = $hash;
  70.     }
  71.  
  72.     private function getLangFromString( $string ) {
  73.         $string = trim( strtolower( $string ) );
  74.         if ( isset( $this -> hashHasLanguages[$string] ) ) {
  75.             return $string;
  76.         } else {
  77.             return false;
  78.         }
  79.     }
  80.  
  81.     public function getLang() {
  82.  
  83.         ### FIRST SELECTION ###
  84.        $retLang = $this -> arHasLanguages[0];
  85.  
  86.         ### SECOND SELECTION ###
  87.        if ( isset ( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
  88.             $arTmp = explode( ',' , $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
  89.             foreach ( $arTmp as $item ) {
  90.                 $arLangQuote = array();
  91.                 $arLangQuote = explode( ';', $item );
  92.                 if ( ! isset($arLangQuote[1] ) ) {
  93.                     $arLangQuote[1] = 1;
  94.                 }
  95.                 $arLangQuote[0] = trim( $arLangQuote[0] );
  96.                 if ( $arLangQuote[0] ) {
  97.                     $arT['quote'] = floatval( trim( str_replace( 'q=', '', $arLangQuote[1] ) ) );
  98.                     $arT['lang']  = trim( strtolower( $arLangQuote[0] ) );
  99.                     $arAcceptedLanguages[] = $arT;
  100.                 }
  101.             }
  102.         }
  103.         $SelectedLanguage = $this -> arHasLanguages[0];
  104.         $quote = 0;
  105.         foreach ( $this -> arHasLanguages as $lang ) {
  106.             foreach ( $arAcceptedLanguages as $arTmp ) {
  107.                 if ( $lang == $arTmp['lang'] && $arTmp['quote'] > $quote ) {
  108.                     $retLang = $arTmp['lang'];
  109.                     $quote   = $arTmp['quote'];
  110.                 }
  111.             }
  112.         }
  113.  
  114.         ### THIRD SELECTION: ###
  115.        if ( 'cookie' == $this -> storeMethod ) {
  116.             if ( isset( $_COOKIE[$this -> storeName] ) ) {
  117.                 $tmp = $this -> getLangFromString ( $_COOKIE[$this -> storeName] );
  118.                 if ( $tmp ) {
  119.                     $retLang = $tmp;
  120.                 }
  121.             }
  122.         } elseif ( 'session' == $this -> storeMethod ) {
  123.             session_start();
  124.             if ( isset( $_GET[$this -> storeName] ) && 'unset' == $_GET[$this -> storeName] ) {
  125.                 unset( $_SESSION[ $this -> storeName] );
  126.                 header( 'Location: '. $_SERVER['SCRIPT_NAME'] );
  127.                 exit;
  128.             }
  129.             $tmp = $this -> getLangFromString ( $_SESSION[$this -> storeName] );
  130.             if ( $tmp ) {
  131.                 $retLang = $tmp;
  132.             }
  133.         } else {
  134.             trigger_error( "Fatal: storeMethod must be 'cookie' or 'session' given is: '" . $this -> storeMethod . "'", E_USER_ERROR );
  135.         }
  136.  
  137.         ### FOURTH PER-HOST-SELECTION: ###
  138.        $parts = explode(  '.' , strtolower( $_SERVER['HTTP_HOST'] ) );
  139.         $tmp = $this -> getLangFromString( $parts[0] );
  140.         if ( $tmp ) {
  141.             $retLang = $tmp;
  142.         }
  143.  
  144.         ### FIFT METHOD: PER-DIR-SELECTION: ###
  145.        $parts = explode( '/' , trim( strtolower( $_SERVER['REQUEST_URI'] ) , '/' ) );
  146.         $tmp = $this -> getLangFromString ( $parts[0] );
  147.         if ( $tmp ) {
  148.             $retLang = $tmp;
  149.         }
  150.         $this ->  LastSelection = $retLang;
  151.  
  152.         ### LAST METHOD: GET/POST-SELECTION: ###
  153.        if ( isset( $_REQUEST[$this -> storeName] ) ) {
  154.             $tmp = $this -> getLangFromString ( $_REQUEST[$this -> storeName] );
  155.             if ( $tmp ) {
  156.                 $retLang = $tmp;
  157.                 $this -> LastSelection = $retLang;
  158.                 $this -> storeSelection( $retLang );
  159.                 header( 'Location: ' . $_SERVER['SCRIPT_NAME'] );
  160.                 exit;
  161.             }
  162.  
  163.         }
  164.  
  165.         return $retLang;
  166.     }
  167.  
  168.     public function getLastSelection() {
  169.         return $this -> LastSelection;
  170.     }
  171.  
  172.     public function storeSelection( $lang ) {
  173.         if ( 'cookie' == $this -> storeMethod ) {
  174.             setcookie( $this -> storeName, $lang );
  175.         } elseif ('session' == $this -> storeMethod ) {
  176.             if ( ! session_status() == PHP_SESSION_ACTIVE ) {
  177.                 session_start();
  178.             }
  179.             $_SESSION[$this -> storeName] = $lang;
  180.         } else {
  181.             trigger_error( "Fatal: storeMethod must be 'cookie' or 'session'. given is: '" . $this -> storeMethod . "'", E_USER_ERROR );
  182.         }
  183.     }
  184. }