class PoHeader {
private $_langcode;
private $_pluralForms;
private $_authors;
private $_po_date;
private $_languageName;
private $_projectName;
public function __construct($langcode = NULL) {
$this->_langcode = $langcode;
$this->_po_date = date("Y-m-d H:iO");
$this->_pluralForms = 'nplurals=2; plural=(n > 1);';
}
function getPluralForms() {
return $this->_pluralForms;
}
function setLanguageName($languageName) {
$this->_languageName = $languageName;
}
function getLanguageName() {
return $this->_languageName;
}
function setProjectName($projectName) {
$this->_projectName = $projectName;
}
function getProjectName() {
return $this->_projectName;
}
public function setFromString($header) {
$values = $this
->parseHeader($header);
if (!empty($values['Plural-Forms'])) {
$this->_pluralForms = $values['Plural-Forms'];
}
}
public function __toString() {
$output = '';
$isTemplate = empty($this->_languageName);
$output .= '# ' . ($isTemplate ? 'LANGUAGE' : $this->_languageName) . ' translation of ' . ($isTemplate ? 'PROJECT' : $this->_projectName) . "\n";
if (!empty($this->_authors)) {
$output .= '# Generated by ' . implode("\n# ", $this->_authors) . "\n";
}
$output .= "#\n";
$output .= "msgid \"\"\n";
$output .= "msgstr \"\"\n";
$output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
$output .= "\"POT-Creation-Date: " . $this->_po_date . "\\n\"\n";
$output .= "\"PO-Revision-Date: " . $this->_po_date . "\\n\"\n";
$output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
$output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
$output .= "\"MIME-Version: 1.0\\n\"\n";
$output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
$output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
$output .= "\"Plural-Forms: " . $this->_pluralForms . "\\n\"\n";
$output .= "\n";
return $output;
}
function parsePluralForms($pluralforms) {
$pluralforms = strtr($pluralforms, array(
" " => "",
"\t" => "",
));
$nplurals = strstr($pluralforms, "nplurals=");
if (strpos($nplurals, ";")) {
$nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9);
}
else {
return FALSE;
}
$plural = strstr($pluralforms, "plural=");
if (strpos($plural, ";")) {
$plural = substr($plural, 7, strpos($plural, ";") - 7);
}
else {
return FALSE;
}
$plural = $this
->parseArithmetic($plural);
if ($plural !== FALSE) {
return array(
$nplurals,
$plural,
);
}
else {
throw new Exception('The plural formula could not be parsed.');
}
}
private function parseHeader($header) {
$header_parsed = array();
$lines = array_map('trim', explode("\n", $header));
foreach ($lines as $line) {
if ($line) {
list($tag, $contents) = explode(":", $line, 2);
$header_parsed[trim($tag)] = trim($contents);
}
}
return $header_parsed;
}
private function parseArithmetic($string) {
$precedence = array(
"(" => -1,
")" => -1,
"?" => 1,
":" => 1,
"||" => 3,
"&&" => 4,
"==" => 5,
"!=" => 5,
"<" => 6,
">" => 6,
"<=" => 6,
">=" => 6,
"+" => 7,
"-" => 7,
"*" => 8,
"/" => 8,
"%" => 8,
);
$right_associativity = array(
"?" => 1,
":" => 1,
);
$tokens = $this
->tokenizeFormula($string);
$operator_stack = array();
$element_stack = array();
foreach ($tokens as $token) {
$current_token = $token;
if (is_numeric($token)) {
$element_stack[] = $current_token;
}
elseif ($current_token == "n") {
$element_stack[] = '$n';
}
elseif ($current_token == "(") {
$operator_stack[] = $current_token;
}
elseif ($current_token == ")") {
$topop = array_pop($operator_stack);
while (isset($topop) && $topop != "(") {
$element_stack[] = $topop;
$topop = array_pop($operator_stack);
}
}
elseif (!empty($precedence[$current_token])) {
$topop = array_pop($operator_stack);
while (isset($topop) && $precedence[$topop] >= $precedence[$current_token] && !($precedence[$topop] == $precedence[$current_token] && !empty($right_associativity[$topop]) && !empty($right_associativity[$current_token]))) {
$element_stack[] = $topop;
$topop = array_pop($operator_stack);
}
if ($topop) {
$operator_stack[] = $topop;
}
$operator_stack[] = $current_token;
}
else {
return FALSE;
}
}
$topop = array_pop($operator_stack);
while ($topop != NULL) {
$element_stack[] = $topop;
$topop = array_pop($operator_stack);
}
$previous_size = count($element_stack) + 1;
while (count($element_stack) < $previous_size) {
$previous_size = count($element_stack);
for ($i = 2; $i < count($element_stack); $i++) {
$op = $element_stack[$i];
if (!empty($precedence[$op])) {
$f = "";
if ($op == ":") {
$f = $element_stack[$i - 2] . "):" . $element_stack[$i - 1] . ")";
}
elseif ($op == "?") {
$f = "(" . $element_stack[$i - 2] . "?(" . $element_stack[$i - 1];
}
else {
$f = "(" . $element_stack[$i - 2] . $op . $element_stack[$i - 1] . ")";
}
array_splice($element_stack, $i - 2, 3, $f);
break;
}
}
}
if (count($element_stack) == 1) {
return $element_stack[0];
}
else {
return FALSE;
}
}
private function tokenizeFormula($formula) {
$formula = str_replace(" ", "", $formula);
$tokens = array();
for ($i = 0; $i < strlen($formula); $i++) {
if (is_numeric($formula[$i])) {
$num = $formula[$i];
$j = $i + 1;
while ($j < strlen($formula) && is_numeric($formula[$j])) {
$num .= $formula[$j];
$j++;
}
$i = $j - 1;
$tokens[] = $num;
}
elseif ($pos = strpos(" =<>!&|", $formula[$i])) {
$next = $formula[$i + 1];
switch ($pos) {
case 1:
case 2:
case 3:
case 4:
if ($next == '=') {
$tokens[] = $formula[$i] . '=';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
case 5:
if ($next == '&') {
$tokens[] = '&&';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
case 6:
if ($next == '|') {
$tokens[] = '||';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
}
}
else {
$tokens[] = $formula[$i];
}
}
return $tokens;
}
}