Show
Ignore:
Timestamp:
09/14/06 10:19:14 (2 years ago)
Author:
mbonetti
Message:

locale negotiation

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/gregarius/cls/l10n.php

    r1572 r1579  
    2828rss_require('extlib/l10n/streams.php'); 
    2929rss_require('extlib/l10n/gettext.php'); 
    30  
     30define('RSS_LOCALE_COOKIE','rss_preferred_locale'); 
    3131class RSSl10n { 
    3232     
     
    3636     
    3737    function RSSl10n($locale) { 
    38         $locale = preg_replace('#[^a-zA-Z_]#','',$locale); 
    39         $this -> locale = $locale; 
    40         if (function_exists('version_compare') && version_compare("4.3.0",PHP_VERSION, "<=") && preg_match('#([a-z]{2})_([A-Z]{2})#',$locale,$m)) { 
     38        $this -> locale = preg_replace('#[^a-zA-Z_]#','',$this -> __detectUserLang()); 
     39        if (function_exists('version_compare') && version_compare("4.3.0",PHP_VERSION, "<=") && preg_match('#([a-z]{2})_([A-Z]{2})#',$this -> locale,$m)) { 
    4140            $locales=array( 
    4241                $m[0].'UTF-8', 
     
    4948            setlocale(LC_ALL, $locales);     
    5049        } else { 
    51             setlocale(LC_ALL, $locale); 
     50            setlocale(LC_ALL, $this -> locale); 
    5251        } 
    5352     
    54         $path = GREGARIUS_HOME .'/intl/' . $locale . '/LC_MESSAGES/messages.mo'; 
     53        $path = GREGARIUS_HOME .'/intl/' . $this -> locale . '/LC_MESSAGES/messages.mo'; 
    5554        $streamer = new FileReader($path); 
    5655        $this -> l10n = new gettext_reader($streamer); 
     
    6665        return $ret; 
    6766    } 
     67     
     68    function getLocale() { 
     69        return $this -> locale; 
     70    } 
     71    /** 
     72     * Detect users preferred language. Losely based on http://grep.be/data/accept-to-gettext.inc 
     73     */ 
     74    function __detectUserLang() { 
     75       if (isset($_REQUEST['lang']) && preg_match('#^[a-z]{2}_[A-Z]{2}$#',$_REQUEST['lang']) && file_exists(GREGARIUS_HOME .'intl/'.$_REQUEST['lang'])) { 
     76            return  $_REQUEST['lang']; 
     77        } elseif (isset($_COOKIE[RSS_LOCALE_COOKIE])) { 
     78            return trim($_COOKIE[RSS_LOCALE_COOKIE]); 
     79       } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
     80            $alparts=@preg_split("/,/",$_SERVER['HTTP_ACCEPT_LANGUAGE']); 
     81            foreach($alparts as $part) { 
     82                $part=trim($part); 
     83                if(preg_match("/;/", $part)) { 
     84                    $lang=@preg_split("/;/",$part); 
     85                    $ll = $lang[0]; 
     86                } else { 
     87                    $ll = $part; 
     88                } 
     89 
     90                if (preg_match('#^([a-z]{2})[\-_]?([a-z]{2})?$#i',$ll,$pm)) { 
     91                    $ret =null; 
     92                    if (isset($pm[2])){ 
     93                        if (file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[2]))) { 
     94                            // xx-yy -> xx_YY 
     95                            $ret= $pm[1] ."_".strtoupper($pm[2]); 
     96                        } elseif(file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[1]))) { 
     97                            // xx-yy -> xx_XX 
     98                            $ret= $pm[1] ."_".strtoupper($pm[1]); 
     99                        } 
     100                    } elseif(file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[1]))) { 
     101                        // xx  -> xx_XX 
     102                        $ret= $pm[1] ."_".strtoupper($pm[1]); 
     103                    } 
     104                    if ($ret) { 
     105                        // remember the detected locale for a couple hours 
     106                        setcookie(RSS_LOCALE_COOKIE,$ret,time()+3600*6,getPath()); 
     107                        return $ret; 
     108                    } 
     109                } 
     110                 
     111            } 
     112        } 
     113        // If everything fails, return the user selected language 
     114        return getConfig('rss.output.lang'); 
     115    } 
    68116} 
     117     
     118 
    69119 
    70120function __($msg, $cnt = null) {