X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2FI18n.class.php;h=15799c23ee909a47685094f731eb2a3d2337e260;hb=817c14c6c2098d85ef912fb95898c857bf533b4f;hp=96c374381f6160aff7fe3706d84d6d33cd02ac89;hpb=9a23a8c0a51b7ec38a96f525484134f3cb85dc7e;p=timetracker.git diff --git a/WEB-INF/lib/I18n.class.php b/WEB-INF/lib/I18n.class.php index 96c37438..15799c23 100644 --- a/WEB-INF/lib/I18n.class.php +++ b/WEB-INF/lib/I18n.class.php @@ -35,26 +35,26 @@ class I18n { var $holidays; var $keys = array(); // These are our localized strings. - // The getKey obtains localized keyword value. - function getKey($kword) { + // get - obtains a localized value from $keys array. + function get($key) { $value = ''; - $pos = strpos($kword, '.'); // Keywords can have separating dots such as in form.login.about. + $pos = strpos($key, '.'); // Keywords can have separating dots such as in form.login.about. if (!($pos === false)) { - $words = explode('.', $kword); + $words = explode('.', $key); $str = ''; foreach ($words as $word) { $str .= "['".$word."']"; } eval("\$value = \$this->keys".$str.";"); } else { - $value = $this->keys[$kword]; + $value = $this->keys[$key]; } return $value; } // TODO: refactoring ongoing down from here... function getWeekDayName($id) { - $id = intval($id); + $id = (int) $id; return $this->weekdayNames[$id]; } @@ -68,25 +68,22 @@ class I18n { $this->monthNames = $i18n_months; $this->weekdayNames = $i18n_weekdays; - + $this->weekdayShortNames = $i18n_weekdays_short; - if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) { $this->holidays = $i18n_holidays; - } - + foreach ($i18n_key_words as $kword=>$value) { - $pos = strpos($kword, "."); - if (!($pos === false)) { - $p = explode(".", $kword); - $str = ""; - foreach ($p as $w) { - $str .= "[\"".$w."\"]"; - } - //$value = addslashes($value); - eval("\$this->keys".$str."='".$value."';"); - } else { - $this->keys[$kword] = $value; - } + $pos = strpos($kword, "."); + if (!($pos === false)) { + $p = explode(".", $kword); + $str = ""; + foreach ($p as $w) { + $str .= "[\"".$w."\"]"; + } + eval("\$this->keys".$str."='".$value."';"); + } else { + $this->keys[$kword] = $value; + } } } @@ -98,25 +95,22 @@ class I18n { $this->monthNames = $i18n_months; $this->weekdayNames = $i18n_weekdays; $this->weekdayShortNames = $i18n_weekdays_short; - if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) { $this->holidays = $i18n_holidays; - } foreach ($i18n_key_words as $kword=>$value) { - if (!$value) continue; - $pos = strpos($kword, "."); - if (!($pos === false)) { - $p = explode(".", $kword); - $str = ""; - foreach ($p as $w) { - $str .= "[\"".$w."\"]"; - } - //$value = addslashes($value); - eval("\$this->keys".$str."='".$value."';"); - } else { - $this->keys[$kword] = $value; - } + if (!$value) continue; + $pos = strpos($kword, "."); + if (!($pos === false)) { + $p = explode(".", $kword); + $str = ""; + foreach ($p as $w) { + $str .= "[\"".$w."\"]"; + } + eval("\$this->keys".$str."='".$value."';"); + } else { + $this->keys[$kword] = $value; + } } - return true; + return true; } } @@ -126,22 +120,31 @@ class I18n { return file_exists($filename); } + // getBrowserLanguage() returns a first supported language from browser settings. function getBrowserLanguage() { $acclang = @$_SERVER['HTTP_ACCEPT_LANGUAGE']; if (empty($acclang)) { - return ""; + return false; } $lang_prefs = explode(',', $acclang); foreach ($lang_prefs as $lang_pref) { $lang_pref_parts = explode(';', trim($lang_pref)); - $lang_parts = explode('-', trim($lang_pref_parts[0])); + $lang = $lang_pref_parts[0]; + if ($this->hasLang($lang)) { + return $lang; // Return full language designation (if available), such as pt-BR. + } + + if (strlen($lang) <= 2) + continue; // Do not bother determining main language because we already have it. + + $lang_parts = explode('-', trim($lang)); $lang_main = $lang_parts[0]; - if ($this->hasLang($lang_main)) { - return $lang_main; + if ($lang_main != $lang && $this->hasLang($lang_main)) { + return $lang_main; // Return main language designation, such as pt. } } - return ""; + return false; } // getLangFileList() returns a list of language files. @@ -150,18 +153,17 @@ class I18n { $d = @opendir(RESOURCE_DIR); while (($file = @readdir($d))) { if (($file != ".") && ($file != "..")) { - if (strpos($file, ".lang.php")) { - $fileList[] = @basename($file); - } + if (strpos($file, ".lang.php")) { + $fileList[] = @basename($file); + } } } @closedir($d); return $fileList; } - + static function getLangFromFilename($filename) { return substr($filename, 0, strpos($filename, '.')); } } -?> \ No newline at end of file