Show
Ignore:
Timestamp:
10/16/06 06:31:35 (2 years ago)
Author:
mdodoo
Message:

Sync with trunk up to (but not including changeset [1585]). I should *definitely* do this more often, because this was really annoying.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/multiuser/cls/l10n.php

    r1479 r1604  
    2828rss_require('extlib/l10n/streams.php'); 
    2929rss_require('extlib/l10n/gettext.php'); 
    30  
     30define('RSS_LOCALE_COOKIE','rss_preferred_locale'); 
    3131class RSSl10n { 
    3232     
    3333    var $l10n; 
    3434    var $cache; 
     35    var $locale; 
     36    var $isolang; 
    3537     
    36     function RSSl10n($locale) { 
    37         $path = GREGARIUS_HOME .'/intl/' . $locale . '/LC_MESSAGES/messages.mo'; 
     38    function RSSl10n() { 
     39        $this -> locale = preg_replace('#[^a-zA-Z_]#','',$this -> __detectUserLang()); 
     40         
     41        $ll=explode('_',$this -> locale); 
     42        $this->isloang=$ll[0].'-'.strtoupper($ll[1]); 
     43         
     44        if (function_exists('version_compare') && version_compare("4.3.0",PHP_VERSION, "<=") && preg_match('#([a-z]{2})_([A-Z]{2})#',$this -> locale,$m)) { 
     45            $locales=array( 
     46                $m[0].'UTF-8', 
     47                $m[0].'utf-8', 
     48                $m[0], 
     49                $m[1].'_'.strtoupper($m[1]), 
     50                $m[1], 
     51                $m[2] 
     52            ); 
     53            setlocale(LC_ALL, $locales);     
     54        } else { 
     55            setlocale(LC_ALL, $this -> locale); 
     56        } 
     57     
     58        $path = GREGARIUS_HOME .'/intl/' . $this -> locale . '/LC_MESSAGES/messages.mo'; 
    3859        $streamer = new FileReader($path); 
    3960        $this -> l10n = new gettext_reader($streamer); 
     
    4162    } 
    4263     
    43     function translate($msg) { 
    44         if (isset($this -> cache[$msg])) { 
    45             return $this -> cache[$msg]; 
     64    function translate($msg, $cnt = null) { 
     65        if (isset($this -> cache[$msg . $cnt])) { 
     66            return $this -> cache[$msg . $cnt]; 
    4667        }  
    47         $ret = $this -> l10n -> translate($msg); 
    48         $this -> cache[$msg] = $ret; 
     68        $ret = $this -> l10n -> translate($msg, $cnt); 
     69        $this -> cache[$msg . $cnt] = $ret; 
    4970        return $ret; 
    50          
    5171    } 
     72     
     73    function getLocale() { 
     74        return $this -> locale; 
     75    } 
     76    function getISOLang() { 
     77        return $this ->isloang; 
     78    } 
     79    /** 
     80     * Detect users preferred language. Losely based on http://grep.be/data/accept-to-gettext.inc 
     81     */ 
     82    function __detectUserLang() { 
     83       if (isset($_REQUEST['lang']) && preg_match('#^[a-z]{2}_[A-Z]{2}$#',$_REQUEST['lang']) && file_exists(GREGARIUS_HOME .'intl/'.$_REQUEST['lang'])) { 
     84            setcookie(RSS_LOCALE_COOKIE,$_REQUEST['lang'],time()+3600*6,getPath()); 
     85            return  $_REQUEST['lang']; 
     86        } elseif (isset($_COOKIE[RSS_LOCALE_COOKIE])) { 
     87            return trim($_COOKIE[RSS_LOCALE_COOKIE]); 
     88       } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
     89            $alparts=@preg_split("/,/",$_SERVER['HTTP_ACCEPT_LANGUAGE']); 
     90            foreach($alparts as $part) { 
     91                $part=trim($part); 
     92                if(preg_match("/;/", $part)) { 
     93                    $lang=@preg_split("/;/",$part); 
     94                    $ll = $lang[0]; 
     95                } else { 
     96                    $ll = $part; 
     97                } 
     98 
     99                if (preg_match('#^([a-z]{2})[\-_]?([a-z]{2})?$#i',$ll,$pm)) { 
     100                    $ret =null; 
     101                    if (isset($pm[2])){ 
     102                        if (file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[2]))) { 
     103                            // xx-yy -> xx_YY 
     104                            $ret= $pm[1] ."_".strtoupper($pm[2]); 
     105                        } elseif(file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[1]))) { 
     106                            // xx-yy -> xx_XX 
     107                            $ret= $pm[1] ."_".strtoupper($pm[1]); 
     108                        } 
     109                    } elseif(file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[1]))) { 
     110                        // xx  -> xx_XX 
     111                        $ret= $pm[1] ."_".strtoupper($pm[1]); 
     112                    } elseif($pm[1] == 'en') { 
     113                        // ugly: a better way would be to look up all the available locales 
     114                        // and match against that list 
     115                        $ret='en_US'; 
     116                    } 
     117                    if ($ret) { 
     118                        // remember the detected locale for a couple hours 
     119                        setcookie(RSS_LOCALE_COOKIE,$ret,time()+3600*6,getPath()); 
     120                        return $ret; 
     121                    } 
     122                } 
     123                 
     124            } 
     125        } 
     126        // If everything fails, return the user selected language 
     127        return getConfig('rss.output.lang'); 
     128    } 
    52129} 
     130     
    53131 
    54 function __($msg) { 
    55     return $GLOBALS['rssl10n'] -> translate($msg); 
     132 
     133function __($msg, $cnt = null) { 
     134    return $GLOBALS['rssl10n'] -> translate($msg, $cnt); 
    56135} 
    57136?>