Location: PHPKode > scripts > msgcat > msgcat/msgcat_class.php
<?PHP
// Implements Message Catalog like utility to PHP

class msgcat {
  
  // PUBLIC VARIABLES
  
  VAR $mclistcount ;   // Counter for the mclist array
  
  // PRIVATE VARIABLES
  
  VAR $mclocale ;      // Identifies current locale
  VAR $mcloaddir ;     // full path to .msg files
  VAR $mclist ;        // Array for the translations for mclocale
  
  VAR $debuglvl ;      // 0 - NONE || 1 - ECHO || 2 - FILE
  VAR $fpdebug ;       // Degub file pointer if needed 
  
  // PUBLIC INTERFACE
  
  function msgcat($localstr,$loaddir="./",$debug=0)
    {
    // Constructor: sets values for initial workout
    $this->mclocale = $localstr ;
    $this->mcloaddir = $loaddir ;
    $this->fpdebug = 0 ;
    $this->debug_init($debug) ;
    $this->mclistcount = 0 ;
    $this->mclist_create() ;
    }
    
  function mc($srcstr)
    {
    // Returns translated string for STRING $srcstr
    $retstr = $this->mclist[$srcstr] ;
    if (!$retstr) 
      {
      $retstr = $this->mcunknown($srcstr);
      }
    return $retstr ;
    }
    
  function destroy()
    {
    // Ending file operations
    if ($this->fpdebug)
      {
      $this->debug("===== Ending operations =====") ;
      fclose($this->fpdebug) ;
      }
    }
  // PRIVATE METHODS
  
  function mcunknown($srcstr)
    {
    // Rebuild this function to suit your needs... called when no translation was found
    $this->debug("ERROR: " . $srcstr . " not found in " . $this->mclocale . ".msg file") ;
    return $srcstr ;
    }
    
  function mcset($localestr,$srcstr,$trstr)
    {
    // Sets mc on $localestr locale for $srcstr as $trstr
    if ($localestr==$this->mclocale) {
      $this->mclist += Array($srcstr => $trstr) ;
      $this->debug("O.K.: Added '" . $trstr . "' as '" . $srcstr . "' for locale " . $localestr) ;
      $this->mclistcount++ ;
    } else {
      $this->debug("ERROR: Invalid locale setting on file " . $this->mclocale . ".msg for " . $srcstr) ;
      }
    }
    
  function mclist_create()
    {
    // Generates message catalog from file
    $this->mclist = Array() ;
    $fp = fopen($this->mcloaddir . $this->mclocale . ".msg","r") ;
    if ($fp) {
      $this->debug("PROCESSING: File " . $this->mclocale . ".msg in folder " . $this->mcloaddir) ;
      while (!feof($fp))
        {
        $str = fgets($fp,4096) ;
        if (substr($str,0,2)!="//") 
          {
          // Note on UNIX/Linux CRLF is length 2 not 1, change -1 to -2
          if (strstr($str,"\n")) $str = substr($str,0,strlen($str)-1) ;
          $data = explode("#:#",$str) ;
          if (count($data)==3) {
            $this->mcset($data[0],$data[1],$data[2]) ;
          } else {
            $this->debug("ERROR: " . substr($str,0,strlen($str)-1) . " rejected!" . $this->loaddir) ;          
            }
          }
        } 
      fclose($fp) ;
    } else {
      $this->debug("ERROR: File " . $this->mclocale . ".msg does not exist in folder " . $this->loaddir) ;
      }
    }
    
  function debug($msg)
    {
    // Debug management
    switch ($this->debuglvl)
      {
      case 0:
        break ;
      case 1: 
        echo "<!-- " . $msg . " -->\n" ;
        break ;
      case 2: 
        $msg = date("d/m/Y - H:i:s",getdate())." - " . $msg . "\n";
        fputs($this->fpdebug,$msg) ;
        break ;
      }
    }
    
  function debug_init($debug)
    {
    // Debug initialization
    $this->debuglvl = $debug ;
    switch ($this->debuglvl)
      {
      case 0: 
        // Do Nothing
        break ;
      case 1: 
        $this->debug("MSGCAT CLASS v 1.0 INITIALIZATION FOR LOCALE " . $this->mclocale . " ON DIR " . $this->mcloaddir) ;
        break ;
      case 2: 
        $this->fpdebug = fopen($this->loaddir . "debug","a") ;
        if (!$this->fpdebug)
          {
          $this->debuglvl = 1 ;
          $this->debug("Could not open debug file, debuglvl set to 1") ;                
        } else {
          $this->debug("===== " . $GLOBALS["PHP_SELF"] . ": MSGCAT CLASS v 1.0 INITIALIZATION FOR LOCALE " . $this->mclocale . " ON DIR " . $this->mcloaddir . " =====");
          }
        break ;
      }
    }
  }
?>
Return current item: msgcat