Changeset 60eb1400da19d19a353827a9e0a9b15188125174

Show
Ignore:
Timestamp:
10/03/11 21:14:41 (8 months ago)
Author:
Neutron Soutmun <neutron@…>
Children:
3a845dd46340cce0aff10685d72437f72570920f
Parents:
cf064010f0e32d0f9adb07901672fab7a8290666
git-committer:
Neutron Soutmun <neutron@…> (10/03/11 21:14:41)
Message:

Set language regarding Accept-Language HTTP header

  • weblogin/rahu_i18n.class.php (getAcceptLanguage):
    • Add private method to get/parse/manipulate the user accept language and get one of them that we have supported.
  • weblogin/rahu_i18n.class.php (localeSetup):
    • Using the info from getAcceptLanguage() to set the language for user on initial.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • weblogin/rahu_i18n.class.php

    r5c032f4 r60eb140  
    4848      $_SESSION['language'] = $_GET['language']; 
    4949    } else if (empty ($_SESSION['language'])) { 
    50       $_SESSION['language'] = "English"; 
     50      $accept_lang = $this->getAcceptLanguage (); 
     51      $_SESSION['language'] = empty ($accept_lang) ? "English" : $accept_lang; 
    5152    } 
    52  
    5353 
    5454    $selected_lang =& $this->langlist[$_SESSION['language']]; 
     
    7676 
    7777    return $languages; 
     78  } 
     79 
     80  private function getAcceptLanguage () { 
     81    if (!isset ($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
     82      return ""; 
     83    } 
     84 
     85    preg_match_all ('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); 
     86 
     87    if (count ($lang_parse[1])) { 
     88      $langs = array_combine ($lang_parse[1], $lang_parse[4]); 
     89 
     90      foreach ($langs as $lang => $val) { 
     91        if ($val === '') 
     92          $langs[$lang] = 1; 
     93      } 
     94 
     95      arsort ($langs, SORT_NUMERIC); 
     96 
     97      foreach ($langs as $lang => $val) { 
     98        foreach ($this->langlist as $supportlang_k => $supportlang) { 
     99          $code = strtolower ($supportlang['code']); 
     100          $code = str_replace ("_", "-", $code); 
     101 
     102          if (strstr ($code, $lang) !== FALSE) { 
     103            return $supportlang_k; 
     104          } 
     105           
     106        } 
     107      } 
     108    } 
     109 
     110    return ""; 
    78111  } 
    79112