public function parsePo($file)
{
$res = array('container' => array(), 'translations' => array(), 'file' => $file);
if (!file_exists($file)) {
return $res;
}
$lastPluralId = $lastId = $lastWasPlural = $inHeader = $nextIsThisContext = null;
$fh = fopen($file, 'r');
while (($buffer = fgets($fh)) !== false) {
if (preg_match('/^msgctxt "(((\\\\.)|[^"])*)"/', $buffer, $match)) {
$lastWasPlural = false;
$nextIsThisContext = $match[1];
}
if (preg_match('/^msgid "(((\\\\.)|[^"])*)"/', $buffer, $match)) {
$lastWasPlural = false;
if ($match[1] == '') {
$inHeader = true;
} else {
$inHeader = false;
$lastId = $match[1];
if ($nextIsThisContext) {
$lastId = $nextIsThisContext . "" . $lastId;
$nextIsThisContext = false;
}
}
}
if (preg_match('/^msgstr "(((\\\\.)|[^"])*)"/', $buffer, $match)) {
if ($inHeader == false) {
$lastWasPlural = false;
$res['translations'][static::evalString($lastId)] = static::evalString($match[1]);
}
}
if (preg_match('/^msgid_plural "(((\\\\.)|[^"])*)"/', $buffer, $match)) {
if ($inHeader == false) {
$lastWasPlural = true;
$res['plurals'][static::evalString($lastId)] = static::evalString($match[1]);
}
}
if (preg_match('/^msgstr\\[([0-9]+)\\] "(((\\\\.)|[^"])*)"/', $buffer, $match)) {
if ($inHeader == false) {
$lastPluralId = intval($match[1]);
$res['translations'][static::evalString($lastId)][$lastPluralId] = static::evalString($match[2]);
}
}
if (preg_match('/^"(((\\\\.)|[^"])*)"/', $buffer, $match)) {
if ($inHeader == true) {
$fp = strpos($match[1], ': ');
$res['container'][substr($match[1], 0, $fp)] = str_replace('\\n', '', substr($match[1], $fp + 2));
} else {
if (is_array($res['translations'][$lastId])) {
$res['translations'][static::evalString($lastId)][$lastPluralId] .= static::evalString($match[1]);
} else {
if ($lastWasPlural) {
$res['plurals'][static::evalString($lastId)] .= static::evalString($match[1]);
} else {
$res['translations'][static::evalString($lastId)] .= static::evalString($match[1]);
}
}
}
}
}
return $res;
}