$lines = file($this->configFile);
$key = "";
foreach ($lines as $line_num => $line) {
- if ( ereg("^msgid.*\"(.*)\"", $line, $reg) ) {
+ if ( preg_match("/^msgid.*\"(.*)\"/", $line, $reg) ) {
$key = $reg[1];
}
- if ( ereg("^msgstr.*\"(.*)\"", $line, $reg) ) {
+ if ( preg_match("/^msgstr.*\"(.*)\"/", $line, $reg) ) {
$value = $reg[1];
$this->config[$key] = $value;
}
class LanguageManager
{
- private static $instance;
- private $lang;
+ private static $instance=null;//will be instantiated by singleton()
+ private $lang="C";//fallback for unknown language, constructor overrides it
private $config;
private $templateFolder;
//make sure it ends with /
if(substr($this->templateFolder,-1,1) != "/")
$this->templateFolder .= "/";
-
+ //collect possible languages for this user
+ $langs=array();
// check if cookie is set
if (isset($_COOKIE[COOKIE_LANGUAGE])) {
- $this->lang = $_COOKIE[COOKIE_LANGUAGE];
- } else {
- $this->lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
+ $langs[]= $_COOKIE[COOKIE_LANGUAGE];
+ }
+ if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){
+ foreach(explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $l){
+ //the language itself
+ $langs[]=trim(strtolower($l));
+ //if it has a sub-category, get the parent language
+ $l2=explode("-",trim($l));
+ if(count($l2)<2)continue;
+ $langs[]=trim(strtolower($l2[0]));
+ }
}
- //sanity check for $lang -> must only contain letters; fallback is de
- if(ereg("^[a-zA-Z]+$",$this->lang)===false)
- $this->lang="de";
+ //language sieve: check which ones we know
+ foreach($langs as $l){
+ //sanity check for $lang -> must only contain letters and dashes; fallback is C
+ if(preg_match("/^[a-zA-Z-]+$/",$l)<1)continue;
+ //check that we have this directory
+ if(!file_exists($this->templateFolder . $l))continue;
+ if(!is_dir($this->templateFolder . $l))continue;
+ //found it!
+ $this->lang=$l;
+ break;
+ }
$this->setLanguageConfig();
}
/** returns the instance of the Language Manager */
public static function singleton()
{
- if(!self::$instance) {
+ if(self::$instance===null) {
self::$instance = new LanguageManager();
}
private function setLanguageConfig() {
global $template;
- $dir = $this->templateFolder.$this->lang."/";
+ $template = $this->templateFolder.$this->lang."/";
- if (is_dir($dir)) {
- // if language folder exists
- $template = $dir;
- } else {
- // set default value
- $template = $this->templateFolder."de/";
- }
$this->config = ConfigManager::singleton("lang.po");
}