Merge updated pomo code. Includes new NOOP_Translations class see #10971 props nbachiyski.

git-svn-id: http://svn.automattic.com/wordpress/trunk@12079 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2009-10-21 07:06:55 +00:00
parent 0273adeb65
commit cd36d71ed9
5 changed files with 96 additions and 38 deletions

View File

@ -2,12 +2,12 @@
/**
* Contains Translation_Entry class
*
* @version $Id: entry.php 115 2009-05-11 18:56:15Z nbachiyski $
* @version $Id: entry.php 222 2009-09-07 21:14:23Z nbachiyski $
* @package pomo
* @subpackage entry
*/
if ( !class_exists( 'Translation_Entry' ) ):
/**
* Translation_Entry class encapsulates a translatable string
*/
@ -67,4 +67,4 @@ class Translation_Entry {
return is_null($this->context)? $this->singular : $this->context.chr(4).$this->singular;
}
}
?>
endif;

View File

@ -2,7 +2,7 @@
/**
* Class for working with MO files
*
* @version $Id: mo.php 106 2009-04-23 19:48:22Z nbachiyski $
* @version $Id: mo.php 221 2009-09-07 21:08:21Z nbachiyski $
* @package pomo
* @subpackage mo
*/
@ -10,6 +10,7 @@
require_once dirname(__FILE__) . '/translations.php';
require_once dirname(__FILE__) . '/streams.php';
if ( !class_exists( 'MO' ) ):
class MO extends Gettext_Translations {
var $_nplurals = 2;
@ -95,15 +96,13 @@ class MO extends Gettext_Translations {
}
function get_byteorder($magic) {
// The magic is 0x950412de
// bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
$magic_little = (int) - 1794895138;
$magic_little_64 = (int) 2500072158;
// 0xde120495
$magic_big = ((int) - 569244523) && 0xFFFFFFFF;
$magic_big = ((int) - 569244523) & 0xFFFFFFFF;
if ($magic_little == $magic || $magic_little_64 == $magic) {
return 'little';
} else if ($magic_big == $magic) {
@ -182,4 +181,4 @@ class MO extends Gettext_Translations {
}
?>
endif;

View File

@ -2,7 +2,7 @@
/**
* Class for working with PO files
*
* @version $Id: po.php 123 2009-05-13 19:35:43Z nbachiyski $
* @version $Id: po.php 283 2009-09-23 16:21:51Z nbachiyski $
* @package pomo
* @subpackage po
*/
@ -16,6 +16,7 @@ ini_set('auto_detect_line_endings', 1);
/**
* Routines for working with PO files
*/
if ( !class_exists( 'PO' ) ):
class PO extends Gettext_Translations {
@ -316,7 +317,9 @@ class PO extends Gettext_Translations {
return false;
}
}
if (array() == array_filter($entry->translations)) $entry->translations = array();
if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) {
$entry->translations = array();
}
return array('entry' => $entry, 'lineno' => $lineno);
}
@ -357,4 +360,4 @@ class PO extends Gettext_Translations {
return $s;
}
}
?>
endif;

View File

@ -3,12 +3,13 @@
* Classes, which help reading streams of data from files.
* Based on the classes from Danilo Segan <danilo@kvota.net>
*
* @version $Id: streams.php 138 2009-06-23 13:22:09Z nbachiyski $
* @version $Id: streams.php 223 2009-09-07 21:20:13Z nbachiyski $
* @package pomo
* @subpackage streams
*/
if ( !class_exists( 'POMO_StringReader' ) ):
/**
* Provides file-like methods for manipulating a string instead
* of a physical file.
@ -61,7 +62,9 @@ class POMO_StringReader {
}
}
endif;
if ( !class_exists( 'POMO_CachedFileReader' ) ):
/**
* Reads the contents of the file in the beginning.
*/
@ -74,7 +77,9 @@ class POMO_CachedFileReader extends POMO_StringReader {
$this->_pos = 0;
}
}
endif;
if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
/**
* Allows reading integers from a file.
*/
@ -133,5 +138,4 @@ class POMO_CachedIntFileReader extends POMO_CachedFileReader {
return unpack($endian_letter.$count, $bytes);
}
}
?>
endif;

View File

@ -2,13 +2,14 @@
/**
* Class for a set of entries for translation and their associated headers
*
* @version $Id: translations.php 114 2009-05-11 17:30:38Z nbachiyski $
* @version $Id: translations.php 291 2009-10-21 05:46:08Z nbachiyski $
* @package pomo
* @subpackage translations
*/
require_once dirname(__FILE__) . '/entry.php';
if ( !class_exists( 'Translations' ) ):
class Translations {
var $entries = array();
var $headers = array();
@ -25,7 +26,7 @@ class Translations {
}
$key = $entry->key();
if (false === $key) return false;
$this->entries[$key] = $entry;
$this->entries[$key] = &$entry;
return true;
}
@ -117,29 +118,33 @@ class Gettext_Translations extends Translations {
*/
function gettext_select_plural_form($count) {
if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
$plural_header = $this->get_header('Plural-Forms');
$this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($plural_header);
list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
$this->_nplurals = $nplurals;
$this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
}
return call_user_func($this->_gettext_select_plural_form, $count);
}
function nplurals_and_expression_from_header($header) {
if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
$nplurals = (int)$matches[1];
$expression = trim($this->parenthesize_plural_exression($matches[2]));
return array($nplurals, $expression);
} else {
return array(2, 'n != 1');
}
}
/**
* Makes a function, which will return the right translation index, according to the
* plural forms header
*/
function _make_gettext_select_plural_form($plural_header) {
$res = create_function('$count', 'return 1 == $count? 0 : 1;');
if ($plural_header && (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $plural_header, $matches))) {
$nplurals = (int)$matches[1];
$this->_nplurals = $nplurals;
$plural_expr = trim($this->_parenthesize_plural_exression($matches[2]));
$plural_expr = str_replace('n', '$n', $plural_expr);
$func_body = "
\$index = (int)($plural_expr);
return (\$index < $nplurals)? \$index : $nplurals - 1;";
$res = create_function('$n', $func_body);
}
return $res;
function make_plural_form_function($nplurals, $expression) {
$expression = str_replace('n', '$n', $expression);
$func_body = "
\$index = (int)($expression);
return (\$index < $nplurals)? \$index : $nplurals - 1;";
return create_function('$n', $func_body);
}
/**
@ -149,7 +154,7 @@ class Gettext_Translations extends Translations {
* @param string $expression the expression without parentheses
* @return string the expression with parentheses added
*/
function _parenthesize_plural_exression($expression) {
function parenthesize_plural_exression($expression) {
$expression .= ';';
$res = '';
$depth = 0;
@ -186,14 +191,61 @@ class Gettext_Translations extends Translations {
}
return $headers;
}
function set_header($header, $value) {
parent::set_header($header, $value);
if ('Plural-Forms' == $header)
$this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($value);
if ('Plural-Forms' == $header) {
list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
$this->_nplurals = $nplurals;
$this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);
}
}
}
endif;
if ( !class_exists( 'NOOP_Translations' ) ):
/**
* Provides the same interface as Translations, but doesn't do anything
*/
class NOOP_Translations {
var $entries = array();
var $headers = array();
function add_entry($entry) {
return true;
}
}
function set_header($header, $value) {
}
?>
function set_headers(&$headers) {
}
function get_header($header) {
return false;
}
function translate_entry(&$entry) {
return false;
}
function translate($singular, $context=null) {
return $singular;
}
function select_plural_form($count) {
return 1 == $count? 0 : 1;
}
function get_plural_forms_count() {
return 2;
}
function translate_plural($singular, $plural, $count, $context = null) {
return 1 == $count? $singular : $plural;
}
function merge_with(&$other) {
}
}
endif;