| 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'; |
| | 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 | } |