Make WordPress Core


Ignore:
Timestamp:
06/23/2009 04:32:52 PM (13 years ago)
Author:
ryan
Message:

Merge latest pomo. Works around mbstring.func_overload. Props nbachiyski. fixes #10236 for trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/pomo/mo.php

    r10810 r11626  
    33 * Class for working with MO files
    44 *
    5  * @version $Id: mo.php 33 2009-02-16 09:33:39Z nbachiyski $
     5 * @version $Id: mo.php 106 2009-04-23 19:48:22Z nbachiyski $
    66 * @package pomo
    77 * @subpackage mo
     
    1111require_once dirname(__FILE__) . '/streams.php';
    1212
    13 class MO extends Translations {
     13class MO extends Gettext_Translations {
    1414
    1515    var $_nplurals = 2;
    16 
    17     function set_header($header, $value) {
    18         parent::set_header($header, $value);
    19         if ('Plural-Forms' == $header)
    20             $this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($value);
    21     }
    2216
    2317    /**
     
    3327        return $this->import_from_reader($reader);
    3428    }
     29   
     30    function export_to_file($filename) {
     31        $fh = fopen($filename, 'wb');
     32        if ( !$fh ) return false;
     33        $entries = array_filter($this->entries, create_function('$e', 'return !empty($e->translations);'));
     34        ksort($entries);
     35        $magic = 0x950412de;
     36        $revision = 0;
     37        $total = count($entries) + 1; // all the headers are one entry
     38        $originals_lenghts_addr = 28;
     39        $translations_lenghts_addr = $originals_lenghts_addr + 8 * $total;
     40        $size_of_hash = 0;
     41        $hash_addr = $translations_lenghts_addr + 8 * $total;
     42        $current_addr = $hash_addr;
     43        fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr,
     44            $translations_lenghts_addr, $size_of_hash, $hash_addr));
     45        fseek($fh, $originals_lenghts_addr);
     46       
     47        // headers' msgid is an empty string
     48        fwrite($fh, pack('VV', 0, $current_addr));
     49        $current_addr++;
     50        $originals_table = chr(0);
     51
     52        foreach($entries as $entry) {
     53            $originals_table .= $this->export_original($entry) . chr(0);
     54            $length = strlen($this->export_original($entry));
     55            fwrite($fh, pack('VV', $length, $current_addr));
     56            $current_addr += $length + 1; // account for the NULL byte after
     57        }
     58       
     59        $exported_headers = $this->export_headers();
     60        fwrite($fh, pack('VV', strlen($exported_headers), $current_addr));
     61        $current_addr += strlen($exported_headers) + 1;
     62        $translations_table = $exported_headers . chr(0);
     63       
     64        foreach($entries as $entry) {
     65            $translations_table .= $this->export_translations($entry) . chr(0);
     66            $length = strlen($this->export_translations($entry));
     67            fwrite($fh, pack('VV', $length, $current_addr));
     68            $current_addr += $length + 1;
     69        }
     70       
     71        fwrite($fh, $originals_table);
     72        fwrite($fh, $translations_table);
     73        fclose($fh);
     74    }
     75   
     76    function export_original($entry) {
     77        //TODO: warnings for control characters
     78        $exported = $entry->singular;
     79        if ($entry->is_plural) $exported .= chr(0).$entry->plural;
     80        if (!is_null($entry->context)) $exported = $entry->context . chr(4) . $exported;
     81        return $exported;
     82    }
     83   
     84    function export_translations($entry) {
     85        //TODO: warnings for control characters
     86        return implode(chr(0), $entry->translations);
     87    }
     88   
     89    function export_headers() {
     90        $exported = '';
     91        foreach($this->headers as $header => $value) {
     92            $exported.= "$header: $value\n";
     93        }
     94        return $exported;
     95    }
    3596
    3697    function get_byteorder($magic) {
     
    43104        // 0xde120495
    44105        $magic_big = ((int) - 569244523) && 0xFFFFFFFF;
    45 
     106       
    46107        if ($magic_little == $magic || $magic_little_64 == $magic) {
    47108            return 'little';
     
    64125        $total = $reader->readint32();
    65126        // get addresses of array of lenghts and offsets for original string and translations
    66         $originals_lo_addr = $reader->readint32();
    67         $translations_lo_addr = $reader->readint32();
     127        $originals_lenghts_addr = $reader->readint32();
     128        $translations_lenghts_addr = $reader->readint32();
    68129
    69         $reader->seekto($originals_lo_addr);
    70         $originals_lo = $reader->readint32array($total * 2); // each of
    71         $reader->seekto($translations_lo_addr);
    72         $translations_lo = $reader->readint32array($total * 2);
     130        $reader->seekto($originals_lenghts_addr);
     131        $originals_lenghts = $reader->readint32array($total * 2); // each of
     132        $reader->seekto($translations_lenghts_addr);
     133        $translations_lenghts = $reader->readint32array($total * 2);
    73134
    74135        $length = create_function('$i', 'return $i * 2 + 1;');
     
    76137
    77138        for ($i = 0; $i < $total; ++$i) {
    78             $reader->seekto($originals_lo[$offset($i)]);
    79             $original = $reader->read($originals_lo[$length($i)]);
    80             $reader->seekto($translations_lo[$offset($i)]);
    81             $translation = $reader->read($translations_lo[$length($i)]);
     139            $reader->seekto($originals_lenghts[$offset($i)]);
     140            $original = $reader->read($originals_lenghts[$length($i)]);
     141            $reader->seekto($translations_lenghts[$offset($i)]);
     142            $translation = $reader->read($translations_lenghts[$length($i)]);
    82143            if ('' == $original) {
    83144                $this->set_headers($this->make_headers($translation));
     
    87148        }
    88149        return true;
    89     }
    90 
    91     function make_headers($translation) {
    92         $headers = array();
    93         $lines = explode("\n", $translation);
    94         foreach($lines as $line) {
    95             $parts = explode(':', $line, 2);
    96             if (!isset($parts[1])) continue;
    97             $headers[trim($parts[0])] = trim($parts[1]);
    98         }
    99         return $headers;
    100150    }
    101151
Note: See TracChangeset for help on using the changeset viewer.