| 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)) { |
| | 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 | } |