Bump php-gettext to version 1.0.3. http://mosquito.wordpress.org/view.php?id=1001 Mad props to Nico Kaiser.

git-svn-id: http://svn.automattic.com/wordpress/trunk@2394 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-02-28 16:31:01 +00:00
parent d40a808df3
commit acf8c1c2cc
3 changed files with 301 additions and 154 deletions

View File

@ -1,7 +1,8 @@
<?php <?php
/* /*
Copyright (c) 2003 Danilo Segan <danilo@kvota.net>. Copyright (c) 2003 Danilo Segan <danilo@kvota.net>.
Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
This file is part of PHP-gettext. This file is part of PHP-gettext.
PHP-gettext is free software; you can redistribute it and/or modify PHP-gettext is free software; you can redistribute it and/or modify
@ -20,44 +21,91 @@
*/ */
/**
* Provides a simple gettext replacement that works independently from
// For start, we only want to read the MO files * the system's gettext abilities.
* It can read MO files and use them for translating strings.
* The files are passed to gettext_reader as a Stream (see streams.php)
*
* This version has the ability to cache all strings and translations to
* speed up the string lookup.
* While the cache is enabled by default, it can be switched off with the
* second parameter in the constructor (e.g. whenusing very large MO files
* that you don't want to keep in memory)
*/
class gettext_reader { class gettext_reader {
//public: //public:
var $error = 0; // public variable that holds error code (0 if no error) var $error = 0; // public variable that holds error code (0 if no error)
//private:
var $BYTEORDER = 0; //private:
var $BYTEORDER = 0; // 0: low endian, 1: big endian
var $STREAM = NULL; var $STREAM = NULL;
var $short_circuit = false; var $short_circuit = false;
var $enable_cache = false;
var $originals = NULL; // offset of original table
var $translations = NULL; // offset of translation table
var $pluralheader = NULL; // cache header field for plural forms
var $total = 0; // total string count
var $table_originals = NULL; // table for original strings (offsets)
var $table_translations = NULL; // table for translated strings (offsets)
var $cache_translations = NULL; // original -> translation mapping
/* Methods */
/**
* Reads a 32bit Integer from the Stream
*
* @access private
* @return Integer from the Stream
*/
function readint() { function readint() {
// Reads 4 byte value from $FD and puts it in int if ($this->BYTEORDER == 0) {
// $BYTEORDER specifies the byte order: 0 low endian, 1 big endian // low endian
for ($i=0; $i<4; $i++) { return array_shift(unpack('V', $this->STREAM->read(4)));
$byte[$i]=ord($this->STREAM->read(1)); } else {
// big endian
return array_shift(unpack('N', $this->STREAM->read(4)));
}
} }
//print sprintf("pos: %d\n",$this->STREAM->currentpos());
if ($this->BYTEORDER == 0)
return (int)(($byte[0]) | ($byte[1]<<8) | ($byte[2]<<16) | ($byte[3]<<24));
else
return (int)(($byte[3]) | ($byte[2]<<8) | ($byte[1]<<16) | ($byte[0]<<24));
}
// constructor that requires StreamReader object /**
function gettext_reader($Reader) { * Reads an array of Integers from the Stream
*
* @param int count How many elements should be read
* @return Array of Integers
*/
function readintarray($count) {
if ($this->BYTEORDER == 0) {
// low endian
return unpack('V'.$count, $this->STREAM->read(4 * $count));
} else {
// big endian
return unpack('N'.$count, $this->STREAM->read(4 * $count));
}
}
/**
* Constructor
*
* @param object Reader the StreamReader object
* @param boolean enable_cache Enable or disable caching of strings (default on)
*/
function gettext_reader($Reader, $enable_cache = true) {
// If there isn't a StreamReader, turn on short circuit mode. // If there isn't a StreamReader, turn on short circuit mode.
if (! $Reader) { if (! $Reader) {
$this->short_circuit = true; $this->short_circuit = true;
return; return;
} }
// Caching can be turned off
$this->enable_cache = $enable_cache;
// $MAGIC1 = (int)0x950412de; //bug in PHP 5 // $MAGIC1 = (int)0x950412de; //bug in PHP 5
$MAGIC1 = (int) - 1794895138; $MAGIC1 = (int) - 1794895138;
// $MAGIC2 = (int)0xde120495; //bug // $MAGIC2 = (int)0xde120495; //bug
$MAGIC2 = (int) - 569244523; $MAGIC2 = (int) - 569244523;
$this->STREAM = $Reader; $this->STREAM = $Reader;
$magic = $this->readint(); $magic = $this->readint();
@ -69,169 +117,242 @@ class gettext_reader {
$this->error = 1; // not MO file $this->error = 1; // not MO file
return false; return false;
} }
// FIXME: Do we care about revision? We should. // FIXME: Do we care about revision? We should.
$revision = $this->readint(); $revision = $this->readint();
$total = $this->readint(); $this->total = $this->readint();
$originals = $this->readint(); $this->originals = $this->readint();
$translations = $this->readint(); $this->translations = $this->readint();
$this->total = $total;
$this->originals = $originals;
$this->translations = $translations;
} }
function load_tables($translations=false) { /**
// if tables are loaded do not load them again * Loads the translation tables from the MO file into the cache
if (!is_array($this->ORIGINALS)) { * If caching is enabled, also loads all strings into a cache
$this->ORIGINALS = array(); * to speed up translation lookups
$this->STREAM->seekto($this->originals); *
for ($i=0; $i<$this->total; $i++) { * @access private
$len = $this->readint(); */
$ofs = $this->readint(); function load_tables() {
$this->ORIGINALS[] = array($len,$ofs); if (is_array($this->cache_translations) &&
is_array($this->table_originals) &&
is_array($this->table_translations))
return;
/* get original and translations tables */
$this->STREAM->seekto($this->originals);
$this->table_originals = $this->readintarray($this->total * 2);
$this->STREAM->seekto($this->translations);
$this->table_translations = $this->readintarray($this->total * 2);
if ($this->enable_cache) {
$this->cache_translations = array ();
/* read all strings in the cache */
for ($i = 0; $i < $this->total; $i++) {
$this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
$original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
$this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
$translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
$this->cache_translations[$original] = $translation;
} }
} }
// similar for translations
if ($translations and !is_array($this->TRANSLATIONS)) {
$this->TRANSLATIONS = array();
$this->STREAM->seekto($this->translations);
for ($i=0; $i<$this->total; $i++) {
$len = $this->readint();
$ofs = $this->readint();
$this->TRANSLATIONS[] = array($len,$ofs);
}
}
} }
function get_string_number($num) { /**
// get a string with particular number * Returns a string from the "originals" table
// TODO: Add simple hashing [check array, add if not already there] *
$this->load_tables(); * @access private
$meta = $this->ORIGINALS[$num]; * @param int num Offset number of original string
$length = $meta[0]; * @return string Requested string if found, otherwise ''
$offset = $meta[1]; */
if (! $length) { function get_original_string($num) {
return ''; $length = $this->table_originals[$num * 2 + 1];
} $offset = $this->table_originals[$num * 2 + 2];
$this->STREAM->seekto($offset); if (! $length)
$data = $this->STREAM->read($length); return '';
return (string)$data;
}
function get_translation_number($num) {
// get a string with particular number
// TODO: Add simple hashing [check array, add if not already there]
$this->load_tables(true);
$meta = $this->TRANSLATIONS[$num];
$length = $meta[0];
$offset = $meta[1];
$this->STREAM->seekto($offset); $this->STREAM->seekto($offset);
$data = $this->STREAM->read($length); $data = $this->STREAM->read($length);
return (string)$data; return (string)$data;
} }
// binary search for string /**
function find_string($string, $start,$end) { * Returns a string from the "translations" table
//print "start: $start, end: $end\n"; *
if (abs($start-$end)<=1) { * @access private
// we're done, if it's not it, bye bye * @param int num Offset number of original string
$txt = $this->get_string_number($start); * @return string Requested string if found, otherwise ''
*/
function get_translation_string($num) {
$length = $this->table_translations[$num * 2 + 1];
$offset = $this->table_translations[$num * 2 + 2];
if (! $length)
return '';
$this->STREAM->seekto($offset);
$data = $this->STREAM->read($length);
return (string)$data;
}
/**
* Binary search for string
*
* @access private
* @param string string
* @param int start (internally used in recursive function)
* @param int end (internally used in recursive function)
* @return int string number (offset in originals table)
*/
function find_string($string, $start = -1, $end = -1) {
if (($start == -1) or ($end == -1)) {
// find_string is called with only one parameter, set start end end
$start = 0;
$end = $this->total;
}
if (abs($start - $end) <= 1) {
// We're done, now we either found the string, or it doesn't exist
$txt = $this->get_original_string($start);
if ($string == $txt) if ($string == $txt)
return $start; return $start;
else else
return -1; return -1;
} elseif ($start>$end) { } else if ($start > $end) {
return $this->find_string($string,$end,$start); // start > end -> turn around and start over
} else { return $this->find_string($string, $end, $start);
$half = (int)(($start+$end)/2); } else {
$tst = $this->get_string_number($half); // Divide table in two parts
$cmp = strcmp($string,$tst); $half = (int)(($start + $end) / 2);
if ($cmp == 0) $cmp = strcmp($string, $this->get_original_string($half));
return $half; if ($cmp == 0)
elseif ($cmp<0) // string is exactly in the middle => return it
return $this->find_string($string,$start,$half); return $half;
else if ($cmp < 0)
// The string is in the upper half
return $this->find_string($string, $start, $half);
else else
return $this->find_string($string,$half,$end); // The string is in the lower half
return $this->find_string($string, $half, $end);
} }
} }
/**
* Translates a string
*
* @access public
* @param string string to be translated
* @return string translated string (or original, if not found)
*/
function translate($string) { function translate($string) {
if ($this->short_circuit) { if ($this->short_circuit)
return $string;
}
$num = $this->find_string($string, 0, $this->total);
if ($num == -1)
return $string; return $string;
else $this->load_tables();
return $this->get_translation_number($num);
} if ($this->enable_cache) {
// Caching enabled, get translated string from cache
function get_plural_forms() { if (array_key_exists($string, $this->cache_translations))
// lets assume message number 0 is header return $this->cache_translations[$string];
// this is true, right? else
return $string;
// cache header field for plural forms } else {
if (is_string($this->pluralheader)) // Caching not enabled, try to find string
return $this->pluralheader; $num = $this->find_string($string);
else { if ($num == -1)
$header = $this->get_translation_number(0); return $string;
else
if (eregi("plural-forms: (.*)\n",$header,$regs)) { return $this->get_translation_string($num);
$expr = $regs[1];
} else {
$expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
}
$this->pluralheader = $expr;
return $expr;
} }
} }
/**
* Get possible plural forms from MO header
*
* @access private
* @return string plural form header
*/
function get_plural_forms() {
// lets assume message number 0 is header
// this is true, right?
$this->load_tables();
// cache header field for plural forms
if (! is_string($this->pluralheader)) {
if ($this->enable_cache) {
$header = $this->cache_translations[""];
} else {
$header = $this->get_translation_string(0);
}
if (eregi("plural-forms: (.*)\n", $header, $regs))
$expr = $regs[1];
else
$expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
$this->pluralheader = $expr;
}
return $this->pluralheader;
}
/**
* Detects which plural form to take
*
* @access private
* @param n count
* @return int array index of the right plural form
*/
function select_string($n) { function select_string($n) {
$string = $this->get_plural_forms(); $string = $this->get_plural_forms();
$string = str_replace('nplurals',"\$total",$string); $string = str_replace('nplurals',"\$total",$string);
$string = str_replace("n",$n,$string); $string = str_replace("n",$n,$string);
$string = str_replace('plural',"\$plural",$string); $string = str_replace('plural',"\$plural",$string);
$total = 0; $total = 0;
$plural = 0; $plural = 0;
eval("$string"); eval("$string");
if ($plural>=$total) $plural = 0; if ($plural >= $total) $plural = 0;
return $plural; return $plural;
} }
/**
* Plural version of gettext
*
* @access public
* @param string single
* @param string plural
* @param string number
* @return translated plural form
*/
function ngettext($single, $plural, $number) { function ngettext($single, $plural, $number) {
if ($this->short_circuit) { if ($this->short_circuit) {
if ($number != 1) return $plural; if ($number != 1)
else return $single; return $plural;
else
return $single;
} }
// find out the appropriate form // find out the appropriate form
$select = $this->select_string($number); $select = $this->select_string($number);
// this should contains all strings separated by NULLs // this should contains all strings separated by NULLs
$result = $this->find_string($single.chr(0).$plural,0,$this->total); $key = $single.chr(0).$plural;
if ($result == -1) {
if ($number != 1) return $plural;
else return $single;
} else {
$result = $this->get_translation_number($result);
// lets try to parse all the NUL staff
//$result = "proba0".chr(0)."proba1".chr(0)."proba2"; if ($this->enable_cache) {
$list = explode (chr(0), $result); if (! array_key_exists($key, $this->cache_translations)) {
return $list[$select]; return ($number != 1) ? $plural : $single;
} else {
$result = $this->cache_translations[$key];
$list = explode(chr(0), $result);
return $list[$select];
}
} else {
$num = $this->find_string($key);
if ($num == -1) {
return ($number != 1) ? $plural : $single;
} else {
$result = $this->get_translation_string($num);
$list = explode(chr(0), $result);
return $list[$select];
}
} }
} }
} }
?>
?>

View File

@ -1,6 +1,6 @@
<?php <?php
/* /*
Copyright (c) 2003 Danilo Segan <danilo@kvota.net>. Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
This file is part of PHP-gettext. This file is part of PHP-gettext.
@ -103,11 +103,13 @@ class FileReader {
} }
function read($bytes) { function read($bytes) {
fseek($this->_fd, $this->_pos); if ($bytes) {
$data = fread($this->_fd, $bytes); fseek($this->_fd, $this->_pos);
$this->_pos = ftell($this->_fd); $data = fread($this->_fd, $bytes);
$this->_pos = ftell($this->_fd);
return $data;
return $data;
} else return '';
} }
function seekto($pos) { function seekto($pos) {
@ -130,4 +132,28 @@ class FileReader {
} }
// Preloads entire file in memory first, then creates a StringReader
// over it (it assumes knowledge of StringReader internals)
class CachedFileReader extends StringReader {
function CachedFileReader($filename) {
if (file_exists($filename)) {
$length=filesize($filename);
$fd = fopen($filename,'rb');
if (!$fd) {
$this->error = 3; // Cannot read file, probably permissions
return false;
}
$this->_str = fread($fd, $length);
fclose($fd);
} else {
$this->error = 2; // File doesn't exist
return false;
}
}
}
?> ?>

View File

@ -66,7 +66,7 @@ function load_textdomain($domain, $mofile) {
} }
if ( is_readable($mofile)) { if ( is_readable($mofile)) {
$input = new FileReader($mofile); $input = new CachedFileReader($mofile);
} else { } else {
return; return;
} }