2010-12-02 23:34:41 +00:00
|
|
|
<?php
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* i18n_PO
|
2018-10-30 10:15:30 -04:00
|
|
|
* This class build biggers PO files without size limit and this not use much
|
|
|
|
|
* memory that the allowed.
|
2010-12-02 23:34:41 +00:00
|
|
|
*/
|
|
|
|
|
class i18n_PO
|
|
|
|
|
{
|
2012-10-18 10:54:46 -04:00
|
|
|
private $_file = null;
|
|
|
|
|
private $_string = '';
|
|
|
|
|
private $_meta;
|
|
|
|
|
private $_fp;
|
|
|
|
|
private $_fileComments;
|
|
|
|
|
protected $_editingHeader;
|
|
|
|
|
protected $_fileLine;
|
|
|
|
|
protected $flagEndHeaders;
|
|
|
|
|
protected $flagError;
|
|
|
|
|
protected $flagInit;
|
|
|
|
|
protected $lineNumber;
|
|
|
|
|
public $translatorComments;
|
|
|
|
|
public $extractedComments;
|
|
|
|
|
public $references;
|
|
|
|
|
public $flags;
|
|
|
|
|
public $previousUntranslatedStrings;
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function __construct($file)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Prepares the construction of the .po file.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean|undefined
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function buildInit()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fp = fopen($this->file, 'w');
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!is_resource($this->_fp)) {
|
|
|
|
|
throw new Exception('Could\'t open ' . $this->file . ' file');
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// lock PO file exclusively
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!flock($this->_fp, LOCK_EX)) {
|
|
|
|
|
fclose($this->_fp);
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_meta = 'msgid ""';
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_writeLine($this->_meta);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->_meta = 'msgstr ""';
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_writeLine($this->_meta);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
$this->_editingHeader = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Start reading the .po file.
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function readInit()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fp = fopen($this->file, 'r');
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!is_resource($this->_fp)) {
|
|
|
|
|
throw new Exception('Could\'t open ' . $this->file . ' file');
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
//skipping comments
|
2018-10-30 10:15:30 -04:00
|
|
|
$this->skipComments();
|
2012-10-18 10:54:46 -04:00
|
|
|
//deaing headers
|
|
|
|
|
$this->readHeaders();
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
$this->translatorComments = [];
|
|
|
|
|
$this->extractedComments = [];
|
|
|
|
|
$this->references = [];
|
|
|
|
|
$this->flags = [];
|
|
|
|
|
$this->previousUntranslatedStrings = [];
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add header information.
|
|
|
|
|
*
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addHeader($id, $value)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
if ($this->_editingHeader) {
|
2013-03-14 12:36:34 -04:00
|
|
|
$meta = '"' . trim($id) . ': ' . trim($value) . '\n"';
|
|
|
|
|
$this->_writeLine($meta);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a translator comment.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addTranslatorComment($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$comment = '# ' . trim($str);
|
|
|
|
|
$this->_writeLine($comment);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a extracted comment.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addExtractedComment($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$comment = '#. ' . trim($str);
|
|
|
|
|
$this->_writeLine($comment);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a reference comment.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addReference($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$reference = '#: ' . trim($str);
|
|
|
|
|
$this->_writeLine($reference);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a flag comment.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addFlag($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$flag = '#, ' . trim($str);
|
|
|
|
|
$this->_writeLine($flag);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add previous untranslated string.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addPreviousUntranslatedString($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$str = '#| ' . trim($str);
|
|
|
|
|
$this->_writeLine($str);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a translation.
|
|
|
|
|
*
|
|
|
|
|
* @param string $msgid
|
|
|
|
|
* @param string $msgstr
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function addTranslation($msgid, $msgstr)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->headerStroke();
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_writeLine('msgid "' . $this->prepare($msgid, true) . '"');
|
|
|
|
|
$this->_writeLine('msgstr "' . $this->prepare($msgstr, true) . '"');
|
|
|
|
|
$this->_writeLine('');
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Write line into file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function _writeLine($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_write($str . "\n");
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Write into file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function _write($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2013-03-14 12:36:34 -04:00
|
|
|
fwrite($this->_fp, $str);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Prepare string for add to file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @param boolean $reverse
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function prepare($string, $reverse = false)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
if ($reverse) {
|
2013-03-14 12:36:34 -04:00
|
|
|
$smap = array('"', "\n", "\t", "\r");
|
|
|
|
|
$rmap = array('\"', '\\n"' . "\n" . '"', '\\t', '\\r');
|
|
|
|
|
return (string) str_replace($smap, $rmap, $string);
|
2012-10-18 10:54:46 -04:00
|
|
|
} else {
|
2013-03-14 12:36:34 -04:00
|
|
|
$string = preg_replace('/"\s+"/', '', $string);
|
|
|
|
|
$smap = array('\\n', '\\r', '\\t', '\"');
|
|
|
|
|
$rmap = array("\n", "\r", "\t", '"');
|
|
|
|
|
return (string) str_replace($smap, $rmap, $string);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Add a header stroke.
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function headerStroke()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
if ($this->_editingHeader) {
|
|
|
|
|
$this->_editingHeader = false;
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_writeLine('');
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
|
|
|
|
* read funtions *
|
|
|
|
|
*/
|
2018-10-30 10:15:30 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Skip comments
|
|
|
|
|
*/
|
|
|
|
|
private function skipComments()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->_fileComments = '';
|
|
|
|
|
do {
|
2013-03-14 12:36:34 -04:00
|
|
|
$lastPos = ftell($this->_fp);
|
|
|
|
|
$line = fgets($this->_fp);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->_fileComments .= $line;
|
2013-03-14 12:36:34 -04:00
|
|
|
} while ((substr($line, 0, 1) == '#' || trim($line) == '') && !feof($this->_fp));
|
|
|
|
|
fseek($this->_fp, $lastPos);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Read headers information from .po file.
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
private function readHeaders()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->flagEndHeaders = false;
|
|
|
|
|
$this->flagError = false;
|
|
|
|
|
$this->flagInit = true;
|
|
|
|
|
$this->lineNumber = 0;
|
|
|
|
|
$errMsg = '';
|
|
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
while (!$this->flagError && !$this->flagEndHeaders) {
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if ($this->flagInit) {
|
|
|
|
|
//in first instance
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagInit = false; //unset init flag
|
|
|
|
|
//read the first and second line of the file
|
2013-03-14 12:36:34 -04:00
|
|
|
$firstLine = fgets($this->_fp);
|
|
|
|
|
$secondLine = fgets($this->_fp);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
//verifying the file head
|
2013-03-14 12:36:34 -04:00
|
|
|
if (strpos($firstLine, 'msgid ""') === false || strpos($secondLine, 'msgstr ""') === false) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
$errMsg = 'Misplace for firts msgid "" and msgstr "" in the header';
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//getting the new line
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fileLine = trim(fgets($this->_fp));
|
2012-10-18 10:54:46 -04:00
|
|
|
//set line number
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->lineNumber++;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
//verifying that is not end of file and applying a restriction for to read just the twenty firsts lines
|
2013-03-14 12:36:34 -04:00
|
|
|
if (trim($this->_fileLine) == '' || !$this->_fileLine || $this->lineNumber >= 20) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagEndHeaders = true; // set ending to read the headers
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//verify if has a valid mask header line
|
2013-03-14 12:36:34 -04:00
|
|
|
preg_match('/^"([a-z0-9\._-]+)\s*:\s*([\W\w]+)\\\n"$/i', $this->_fileLine, $match);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
//for a valid header line the $match size should three
|
2013-03-14 12:36:34 -04:00
|
|
|
if (sizeof($match) == 3) {
|
|
|
|
|
$key = trim($match[1]); //getting the key of the header
|
|
|
|
|
$value = trim($match[2]); //getting the value of the header
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->_meta[$key] = $value; //setting a new header
|
|
|
|
|
} else {
|
|
|
|
|
$this->flagEndHeaders = true; //otherwise set the ending to read the headers
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} //end looking for headeers
|
|
|
|
|
//verifying the headers data
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!isset($this->_meta['X-Poedit-Language'])) {
|
|
|
|
|
if (!isset($this->_meta['Language'])) {
|
2012-12-07 17:01:37 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
$errMsg = "X-Poedit-Language and Language meta doesn't exist";
|
2013-03-14 12:36:34 -04:00
|
|
|
} elseif ($this->_meta['Language'] == '') {
|
2012-12-07 17:01:37 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
$errMsg = "Language meta is empty";
|
|
|
|
|
} else {
|
|
|
|
|
$this->_meta['X-Poedit-Language'] = $this->_meta['Language'];
|
|
|
|
|
unset($this->_meta['Language']);
|
|
|
|
|
$this->flagError = false;
|
|
|
|
|
}
|
2013-03-14 12:36:34 -04:00
|
|
|
} elseif ($this->_meta['X-Poedit-Language'] == '') {
|
2010-12-02 23:34:41 +00:00
|
|
|
$this->flagError = true;
|
2012-10-18 10:54:46 -04:00
|
|
|
$errMsg = "X-Poedit-Language meta is empty";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if the country is not present in metadata
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!isset($this->_meta['X-Poedit-Country'])) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->_meta['X-Poedit-Country'] = '.';
|
2013-03-14 12:36:34 -04:00
|
|
|
} elseif ($this->_meta['X-Poedit-Country'] == '') {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->_meta['X-Poedit-Country'] = '.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//thowing the exception if is necesary
|
|
|
|
|
if ($this->flagError) {
|
2013-03-14 12:36:34 -04:00
|
|
|
throw new Exception("This file is not a valid PO file. ($errMsg)");
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Get headers information.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function getHeaders()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
return $this->_meta;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Get translations.
|
|
|
|
|
*
|
|
|
|
|
* @return array|boolean
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function getTranslation()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$flagReadingComments = true;
|
2018-10-30 10:15:30 -04:00
|
|
|
$this->translatorComments = [];
|
|
|
|
|
$this->extractedComments = [];
|
|
|
|
|
$this->references = [];
|
|
|
|
|
$this->flags = [];
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
//getting the new line
|
2013-03-14 12:36:34 -04:00
|
|
|
while ($flagReadingComments && !$this->flagError) {
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fileLine = trim(fgets($this->_fp));
|
2012-10-18 10:54:46 -04:00
|
|
|
//set line number
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->lineNumber++;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!$this->_fileLine) {
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
$prefix = substr($this->_fileLine, 0, 2);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
switch ($prefix) {
|
|
|
|
|
case '# ':
|
2013-03-14 12:36:34 -04:00
|
|
|
$lineItem = str_replace('# ', '', $this->_fileLine);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->translatorComments[] = $lineItem;
|
|
|
|
|
break;
|
|
|
|
|
case '#.':
|
2013-03-14 12:36:34 -04:00
|
|
|
if (substr_count($this->_fileLine, '#. ') == 0) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
} else {
|
2013-03-14 12:36:34 -04:00
|
|
|
$lineItem = str_replace('#. ', '', $this->_fileLine);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->extractedComments[] = $lineItem;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '#:':
|
2013-03-14 12:36:34 -04:00
|
|
|
if (substr_count($this->_fileLine, '#: ') == 0) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
} else {
|
2013-03-14 12:36:34 -04:00
|
|
|
$lineItem = str_replace('#: ', '', $this->_fileLine);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->references[] = $lineItem;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '#,':
|
2013-03-14 12:36:34 -04:00
|
|
|
if (substr_count($this->_fileLine, '#, ') == 0) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
} else {
|
2013-03-14 12:36:34 -04:00
|
|
|
$lineItem = str_replace('#, ', '', $this->_fileLine);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flags[] = $lineItem;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '#|':
|
2013-03-14 12:36:34 -04:00
|
|
|
if (substr_count($this->_fileLine, '#| ') == 0) {
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->flagError = true;
|
|
|
|
|
} else {
|
2013-03-14 12:36:34 -04:00
|
|
|
$lineItem = str_replace('#| ', '', $this->_fileLine);
|
2012-10-18 10:54:46 -04:00
|
|
|
$this->previousUntranslatedStrings[] = $lineItem;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$flagReadingComments = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (!$this->_fileLine) {
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Getting the msgid
|
2013-03-14 12:36:34 -04:00
|
|
|
preg_match('/\s*msgid\s*"(.*)"\s*/s', $this->_fileLine, $match);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (sizeof($match) != 2) {
|
|
|
|
|
throw new Exception('Invalid PO file format1');
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msgid = '';
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
//g::pr($match);
|
|
|
|
|
$msgid .= $match[1];
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fileLine = trim(fgets($this->_fp));
|
|
|
|
|
preg_match('/^"(.*)"\s*/s', $this->_fileLine, $match);
|
|
|
|
|
} while (sizeof($match) == 2);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
//Getting the msgstr
|
2013-03-14 12:36:34 -04:00
|
|
|
preg_match('/\s*msgstr\s*"(.*)"\s*/s', $this->_fileLine, $match);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-03-14 12:36:34 -04:00
|
|
|
if (sizeof($match) != 2) {
|
|
|
|
|
throw new Exception('Invalid PO file format2');
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msgstr = '';
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
//g::pr($match);
|
|
|
|
|
$msgstr .= $match[1] . "\n";
|
2013-03-14 12:36:34 -04:00
|
|
|
$this->_fileLine = trim(fgets($this->_fp));
|
|
|
|
|
preg_match('/^"(.*)"\s*/s', $this->_fileLine, $match);
|
|
|
|
|
} while (sizeof($match) == 2);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
return [
|
|
|
|
|
'msgid' => trim($msgid),
|
|
|
|
|
'msgstr' => trim($msgstr)
|
|
|
|
|
];
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:15:30 -04:00
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2013-03-14 12:36:34 -04:00
|
|
|
public function __destruct()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
if ($this->_fp) {
|
2013-03-14 12:36:34 -04:00
|
|
|
fclose($this->_fp);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|