Source for file gettext.class.php
Documentation is available at gettext.class.php
* This file is part of PHP-gettext.
* PHP-gettext is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* PHP-gettext is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with PHP-gettext; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* @copyright 2004-2020 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: gettext.class.php 14845 2020-01-07 08:09:34Z pdontthink $
* Class that uses parsed translation input objects
* holds error code (0 if no error)
* specifies the byte order: 0 low endian, 1 big endian
// Reads 4 byte value from $FD and puts it in int
// $BYTEORDER specifies the byte order: 0 low endian, 1 big endian
$byte[$i]=
ord($this->STREAM->read(1));
//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));
return (int)
(($byte[3]) |
($byte[2]<<
8) |
($byte[1]<<
16) |
($byte[0]<<
24));
* constructor that requires StreamReader object
* @return boolean false, if some error with stream
$MAGIC1 = (int)
((222) |
(18<<
8) |
(4<<
16) |
(149<<
24));
$MAGIC2 = (int)
((149) |
(4<<
8) |
(18<<
16) |
(222<<
24));
if ($this->STREAM->error>
0) {
} elseif ($magic ==
$MAGIC2) {
$this->error =
1; // not MO file
// FIXME: Do we care about revision? We should.
$this->originals =
$originals;
$this->translations =
$translations;
// Here we store already found translations
$this->_HASHED =
array();
* @param boolean $translations do translation have to be loaded
// if tables are loaded do not load them again
if (!isset
($this->ORIGINALS)) {
$this->ORIGINALS =
array();
$this->STREAM->seekto($this->originals);
for ($i=
0; $i<
$this->total; $i++
) {
$this->ORIGINALS[] =
array($len,$ofs);
// similar for translations
if ($translations and !isset
($this->TRANSLATIONS)) {
$this->TRANSLATIONS =
array();
$this->STREAM->seekto($this->translations);
for ($i=
0; $i<
$this->total; $i++
) {
$this->TRANSLATIONS[] =
array($len,$ofs);
* get a string with particular number
* @return string untranslated string
// TODO: Add simple hashing [check array, add if not already there]
$meta =
$this->ORIGINALS[$num];
$this->STREAM->seekto($offset);
$data =
$this->STREAM->read($length);
* get translated string with particular number
* @return string translated string
// get a string with particular number
// TODO: Add simple hashing [check array, add if not already there]
$meta =
$this->TRANSLATIONS[$num];
$this->STREAM->seekto($offset);
$data =
$this->STREAM->read($length);
* binary search for string
//print "start: $start, end: $end\n";
// Simple hashing to improve speed
if (isset
($this->_HASHED[$string])) return $this->_HASHED[$string];
if (abs($start-
$end)<=
1) {
// we're done, if it's not it, bye bye
$this->_HASHED[$string] =
$start;
$half = (int)
(($start+
$end)/
2);
$this->_HASHED[$string] =
$half;
* @param string $string English string
* @return string translated string
if ($this->error >
0) return $string;
* extract plural forms header
* @return string plural-forms header string
// lets assume message number 0 is header
// cache header field for plural forms
if (isset
($this->pluralheader) &&
is_string($this->pluralheader))
return $this->pluralheader;
if (preg_match('/plural-forms: (.*)\n/i',$header,$regs)) {
$expr =
"nplurals=2; plural=n == 1 ? 0 : 1;";
$this->pluralheader =
$expr;
* find out the appropriate form number
* @param integer $n count
if ($plural>=
$total) $plural =
0;
* translate string with singular/plural forms
* @param string $single English singural form of translation
* @param string $plural English plural form of translation
* @param string $number count
function ngettext($single, $plural, $number) {
// find out the appropriate form
// this should contains all strings separated by NULLs
if ($number !=
1) return $plural;
// lets try to parse all the NUL staff
//$result = "proba0".chr(0)."proba1".chr(0)."proba2";
Documentation generated on Mon, 13 Jan 2020 04:22:39 +0100 by phpDocumentor 1.4.3