Sync pomo library with the current GlotPress version

git-svn-id: http://svn.automattic.com/wordpress/trunk@18528 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nbachiyski 2011-08-11 04:29:35 +00:00
parent cdb9e49b8c
commit 688eebdc26
5 changed files with 110 additions and 51 deletions

View File

@ -2,7 +2,7 @@
/**
* Contains Translation_Entry class
*
* @version $Id: entry.php 406 2010-02-07 11:10:24Z nbachiyski $
* @version $Id: entry.php 621 2011-06-13 12:21:50Z nbachiyski $
* @package pomo
* @subpackage entry
*/
@ -65,5 +65,14 @@ class Translation_Entry {
// prepend context and EOT, like in MO files
return is_null($this->context)? $this->singular : $this->context.chr(4).$this->singular;
}
function merge_with(&$other) {
$this->flags = array_unique( array_merge( $this->flags, $other->flags ) );
$this->references = array_unique( array_merge( $this->references, $other->references ) );
if ( $this->extracted_comments != $other->extracted_comments ) {
$this->extracted_comments .= $other->extracted_comments;
}
}
}
endif;

View File

@ -2,7 +2,7 @@
/**
* Class for working with MO files
*
* @version $Id: mo.php 406 2010-02-07 11:10:24Z nbachiyski $
* @version $Id: mo.php 602 2011-01-30 12:43:29Z nbachiyski $
* @package pomo
* @subpackage mo
*/
@ -30,6 +30,20 @@ class MO extends Gettext_Translations {
function export_to_file($filename) {
$fh = fopen($filename, 'wb');
if ( !$fh ) return false;
$res = $this->export_to_file_handle( $fh );
fclose($fh);
return $res;
}
function export() {
$tmp_fh = fopen("php://temp", 'r+');
if ( !$tmp_fh ) return false;
$this->export_to_file_handle( $tmp_fh );
rewind( $tmp_fh );
return stream_get_contents( $tmp_fh );
}
function export_to_file_handle($fh) {
$entries = array_filter($this->entries, create_function('$e', 'return !empty($e->translations);'));
ksort($entries);
$magic = 0x950412de;
@ -70,7 +84,7 @@ class MO extends Gettext_Translations {
fwrite($fh, $originals_table);
fwrite($fh, $translations_table);
fclose($fh);
return true;
}
function export_original($entry) {

View File

@ -2,7 +2,7 @@
/**
* Class for working with PO files
*
* @version $Id: po.php 406 2010-02-07 11:10:24Z nbachiyski $
* @version $Id: po.php 589 2010-12-18 01:40:57Z nbachiyski $
* @package pomo
* @subpackage po
*/
@ -19,6 +19,7 @@ ini_set('auto_detect_line_endings', 1);
if ( !class_exists( 'PO' ) ):
class PO extends Gettext_Translations {
var $comments_before_headers = '';
/**
* Exports headers to a PO entry
@ -31,7 +32,11 @@ class PO extends Gettext_Translations {
$header_string.= "$header: $value\n";
}
$poified = PO::poify($header_string);
return rtrim("msgid \"\"\nmsgstr $poified");
if ($this->comments_before_headers)
$before_headers = $this->prepend_each_line(rtrim($this->comments_before_headers)."\n", '# ');
else
$before_headers = '';
return rtrim("{$before_headers}msgid \"\"\nmsgstr $poified");
}
/**
@ -76,6 +81,15 @@ class PO extends Gettext_Translations {
return fclose($fh);
}
/**
* Text to include as a comment before the start of the PO contents
*
* Doesn't need to include # in the beginning of lines, these are added automatically
*/
function set_comment_before_headers( $text ) {
$this->comments_before_headers = $text;
}
/**
* Formats a string in PO-style
*
@ -254,7 +268,7 @@ class PO extends Gettext_Translations {
return false;
}
// add comment
$this->add_comment_to_entry($entry, $line);
$this->add_comment_to_entry($entry, $line);;
} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
if ($is_final($context)) {
PO::read_line($f, 'put-back');

View File

@ -3,7 +3,7 @@
* Classes, which help reading streams of data from files.
* Based on the classes from Danilo Segan <danilo@kvota.net>
*
* @version $Id: streams.php 406 2010-02-07 11:10:24Z nbachiyski $
* @version $Id: streams.php 597 2011-01-16 20:14:36Z nbachiyski $
* @package pomo
* @subpackage streams
*/
@ -22,7 +22,7 @@ class POMO_Reader {
/**
* Sets the endianness of the file.
*
* @param string $endian 'big' or 'little'
* @param $endian string 'big' or 'little'
*/
function setEndian($endian) {
$this->endian = $endian;
@ -106,7 +106,7 @@ if ( !class_exists( 'POMO_FileReader' ) ):
class POMO_FileReader extends POMO_Reader {
function POMO_FileReader($filename) {
parent::POMO_Reader();
$this->_f = fopen($filename, 'r');
$this->_f = fopen($filename, 'rb');
}
function read($bytes) {

View File

@ -2,7 +2,7 @@
/**
* Class for a set of entries for translation and their associated headers
*
* @version $Id: translations.php 406 2010-02-07 11:10:24Z nbachiyski $
* @version $Id: translations.php 590 2010-12-20 19:58:37Z nbachiyski $
* @package pomo
* @subpackage translations
*/
@ -30,6 +30,19 @@ class Translations {
return true;
}
function add_entry_or_merge($entry) {
if (is_array($entry)) {
$entry = new Translation_Entry($entry);
}
$key = $entry->key();
if (false === $key) return false;
if (isset($this->entries[$key]))
$this->entries[$key]->merge_with($entry);
else
$this->entries[$key] = &$entry;
return true;
}
/**
* Sets $header PO header to $value
*
@ -108,6 +121,15 @@ class Translations {
$this->entries[$entry->key()] = $entry;
}
}
function merge_originals_with(&$other) {
foreach( $other->entries as $entry ) {
if ( !isset( $this->entries[$entry->key()] ) )
$this->entries[$entry->key()] = $entry;
else
$this->entries[$entry->key()]->merge_with($entry);
}
}
}
class Gettext_Translations extends Translations {