DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PO File Parser

11.27.2006
| 559 views |
  • submit to reddit
        // Opens a PO file and parses it out into an associative array 

            if ($this->locale == "none") { 
                $fh = @fopen($po_dir . $this->lang .  "/LC_MESSAGES/messages.po", "r");
            } else {
                $fh = @fopen($po_dir . $this->lang . "_" . $this->locale . "/LC_MESSAGES/messages.po", "r");
            }
            
            
            
            
            
            $po_list = "";
            $headersection = 1;
            
            
            while($line = @fgets($fh)) {
                if ( (substr($line, 0, 1) == "#") && ($headersection != 0) ) {
                    $po_list['[[header_comments]]'] .= $line . "\n";
                }

                if (preg_match("/^(msgid)(\s)*\"[\w\W\b\s\d]*\"/", $line) ){

                    $headersection = 0;
                    $key = substr($line, 7, strlen($line) - 10);
                    
                    if (preg_match("/(msgid)(\s)*(\"\")/", $line) ) {
                        while( !(preg_match("/msgstr/", ($line2 = fgets($fh) ) ) ) ) {
                            $key .= substr($line2, 1, strlen($line2) - 4) . "\r\n";
                        }
                    }
                    
                    $po_list[$key] = "";
                    
                    
                } elseif (substr($line, 0, 6) == "msgstr" && substr($line, 0, 9) != "msgstr \"\"" ) {
                    
                    $headersection = 0;
                    $value = substr($line, 8, strlen($line) - 11);
                    $po_list[$key] = $value;
                    $key_entered="true";
                    
                } elseif (preg_match("/^(\"(.)+\")/", $line) ){
                    
                        $value = substr($line, 1, strlen($line) - 4);
                        $po_list[$key] .= "$value\r\n";
                        //echo "$key => $value <br />";
                        
                }
              

                
        };