Removed holidays from translation files.
[timetracker.git] / WEB-INF / lib / I18n.class.php
index 96c3743..7eb6228 100644 (file)
@@ -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,21 @@ 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 +94,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 +119,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 +152,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