Merge updated pomo code. Includes new NOOP_Translations class see #10971 props nbachiyski.
git-svn-id: https://develop.svn.wordpress.org/trunk@12079 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
2ac10a307b
commit
889f15dfdb
|
@ -2,12 +2,12 @@
|
||||||
/**
|
/**
|
||||||
* Contains Translation_Entry class
|
* 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
|
* @package pomo
|
||||||
* @subpackage entry
|
* @subpackage entry
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if ( !class_exists( 'Translation_Entry' ) ):
|
||||||
/**
|
/**
|
||||||
* Translation_Entry class encapsulates a translatable string
|
* 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;
|
return is_null($this->context)? $this->singular : $this->context.chr(4).$this->singular;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
endif;
|
|
@ -2,7 +2,7 @@
|
||||||
/**
|
/**
|
||||||
* Class for working with MO files
|
* 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
|
* @package pomo
|
||||||
* @subpackage mo
|
* @subpackage mo
|
||||||
*/
|
*/
|
||||||
|
@ -10,6 +10,7 @@
|
||||||
require_once dirname(__FILE__) . '/translations.php';
|
require_once dirname(__FILE__) . '/translations.php';
|
||||||
require_once dirname(__FILE__) . '/streams.php';
|
require_once dirname(__FILE__) . '/streams.php';
|
||||||
|
|
||||||
|
if ( !class_exists( 'MO' ) ):
|
||||||
class MO extends Gettext_Translations {
|
class MO extends Gettext_Translations {
|
||||||
|
|
||||||
var $_nplurals = 2;
|
var $_nplurals = 2;
|
||||||
|
@ -95,15 +96,13 @@ class MO extends Gettext_Translations {
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_byteorder($magic) {
|
function get_byteorder($magic) {
|
||||||
|
|
||||||
// The magic is 0x950412de
|
// The magic is 0x950412de
|
||||||
|
|
||||||
// bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
|
// bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
|
||||||
$magic_little = (int) - 1794895138;
|
$magic_little = (int) - 1794895138;
|
||||||
$magic_little_64 = (int) 2500072158;
|
$magic_little_64 = (int) 2500072158;
|
||||||
// 0xde120495
|
// 0xde120495
|
||||||
$magic_big = ((int) - 569244523) && 0xFFFFFFFF;
|
$magic_big = ((int) - 569244523) & 0xFFFFFFFF;
|
||||||
|
|
||||||
if ($magic_little == $magic || $magic_little_64 == $magic) {
|
if ($magic_little == $magic || $magic_little_64 == $magic) {
|
||||||
return 'little';
|
return 'little';
|
||||||
} else if ($magic_big == $magic) {
|
} else if ($magic_big == $magic) {
|
||||||
|
@ -182,4 +181,4 @@ class MO extends Gettext_Translations {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
endif;
|
|
@ -2,7 +2,7 @@
|
||||||
/**
|
/**
|
||||||
* Class for working with PO files
|
* 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
|
* @package pomo
|
||||||
* @subpackage po
|
* @subpackage po
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,7 @@ ini_set('auto_detect_line_endings', 1);
|
||||||
/**
|
/**
|
||||||
* Routines for working with PO files
|
* Routines for working with PO files
|
||||||
*/
|
*/
|
||||||
|
if ( !class_exists( 'PO' ) ):
|
||||||
class PO extends Gettext_Translations {
|
class PO extends Gettext_Translations {
|
||||||
|
|
||||||
|
|
||||||
|
@ -316,7 +317,9 @@ class PO extends Gettext_Translations {
|
||||||
return false;
|
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);
|
return array('entry' => $entry, 'lineno' => $lineno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,4 +360,4 @@ class PO extends Gettext_Translations {
|
||||||
return $s;
|
return $s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
endif;
|
|
@ -3,12 +3,13 @@
|
||||||
* Classes, which help reading streams of data from files.
|
* Classes, which help reading streams of data from files.
|
||||||
* Based on the classes from Danilo Segan <danilo@kvota.net>
|
* 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
|
* @package pomo
|
||||||
* @subpackage streams
|
* @subpackage streams
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
if ( !class_exists( 'POMO_StringReader' ) ):
|
||||||
/**
|
/**
|
||||||
* Provides file-like methods for manipulating a string instead
|
* Provides file-like methods for manipulating a string instead
|
||||||
* of a physical file.
|
* 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.
|
* Reads the contents of the file in the beginning.
|
||||||
*/
|
*/
|
||||||
|
@ -74,7 +77,9 @@ class POMO_CachedFileReader extends POMO_StringReader {
|
||||||
$this->_pos = 0;
|
$this->_pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
endif;
|
||||||
|
|
||||||
|
if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
|
||||||
/**
|
/**
|
||||||
* Allows reading integers from a file.
|
* Allows reading integers from a file.
|
||||||
*/
|
*/
|
||||||
|
@ -133,5 +138,4 @@ class POMO_CachedIntFileReader extends POMO_CachedFileReader {
|
||||||
return unpack($endian_letter.$count, $bytes);
|
return unpack($endian_letter.$count, $bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
endif;
|
||||||
?>
|
|
|
@ -2,13 +2,14 @@
|
||||||
/**
|
/**
|
||||||
* Class for a set of entries for translation and their associated headers
|
* 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
|
* @package pomo
|
||||||
* @subpackage translations
|
* @subpackage translations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/entry.php';
|
require_once dirname(__FILE__) . '/entry.php';
|
||||||
|
|
||||||
|
if ( !class_exists( 'Translations' ) ):
|
||||||
class Translations {
|
class Translations {
|
||||||
var $entries = array();
|
var $entries = array();
|
||||||
var $headers = array();
|
var $headers = array();
|
||||||
|
@ -25,7 +26,7 @@ class Translations {
|
||||||
}
|
}
|
||||||
$key = $entry->key();
|
$key = $entry->key();
|
||||||
if (false === $key) return false;
|
if (false === $key) return false;
|
||||||
$this->entries[$key] = $entry;
|
$this->entries[$key] = &$entry;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,29 +118,33 @@ class Gettext_Translations extends Translations {
|
||||||
*/
|
*/
|
||||||
function gettext_select_plural_form($count) {
|
function gettext_select_plural_form($count) {
|
||||||
if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
|
if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) {
|
||||||
$plural_header = $this->get_header('Plural-Forms');
|
list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms'));
|
||||||
$this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($plural_header);
|
$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);
|
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
|
* Makes a function, which will return the right translation index, according to the
|
||||||
* plural forms header
|
* plural forms header
|
||||||
*/
|
*/
|
||||||
function _make_gettext_select_plural_form($plural_header) {
|
function make_plural_form_function($nplurals, $expression) {
|
||||||
$res = create_function('$count', 'return 1 == $count? 0 : 1;');
|
$expression = str_replace('n', '$n', $expression);
|
||||||
if ($plural_header && (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $plural_header, $matches))) {
|
$func_body = "
|
||||||
$nplurals = (int)$matches[1];
|
\$index = (int)($expression);
|
||||||
$this->_nplurals = $nplurals;
|
return (\$index < $nplurals)? \$index : $nplurals - 1;";
|
||||||
$plural_expr = trim($this->_parenthesize_plural_exression($matches[2]));
|
return create_function('$n', $func_body);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -149,7 +154,7 @@ class Gettext_Translations extends Translations {
|
||||||
* @param string $expression the expression without parentheses
|
* @param string $expression the expression without parentheses
|
||||||
* @return string the expression with parentheses added
|
* @return string the expression with parentheses added
|
||||||
*/
|
*/
|
||||||
function _parenthesize_plural_exression($expression) {
|
function parenthesize_plural_exression($expression) {
|
||||||
$expression .= ';';
|
$expression .= ';';
|
||||||
$res = '';
|
$res = '';
|
||||||
$depth = 0;
|
$depth = 0;
|
||||||
|
@ -189,11 +194,58 @@ class Gettext_Translations extends Translations {
|
||||||
|
|
||||||
function set_header($header, $value) {
|
function set_header($header, $value) {
|
||||||
parent::set_header($header, $value);
|
parent::set_header($header, $value);
|
||||||
if ('Plural-Forms' == $header)
|
if ('Plural-Forms' == $header) {
|
||||||
$this->_gettext_select_plural_form = $this->_make_gettext_select_plural_form($value);
|
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;
|
||||||
?>
|
|
||||||
|
|
Loading…
Reference in New Issue