<?php
class POfile
{
    private $stream;

    function __construct($lang = 'hu_HU')
    {
        $header = '';

        // PO headers + 1
        $header .= '"Project-Id-Version: LiveLanguageResources\n"' . PHP_EOL;
        $header .= '"POT-Creation-Date: ' . date('Y-m-d H:i') . '\n"' . PHP_EOL;
        $header .= '"Language-Team: \n"' . PHP_EOL;
        $header .= '"Language: ' . $lang . '\n"' . PHP_EOL;
        $header .= '"MIME-Version: 1.0\n"' . PHP_EOL;
        $header .= '"Content-Type: text/plain; charset=UTF-8\n"' . PHP_EOL;
        $header .= '"Content-Transfer-Encoding: 8bit\n"' . PHP_EOL . PHP_EOL;

        $this->stream = $header;
    }

    function getStream()
    {
        return $this->stream;
    }

    function addComment($param, $value)
    {
        $this->stream .= '#. ' . $param . ': ' . str_replace("\r\n", ' ', $value) . PHP_EOL;
    }

    function addMsgctxt($msgctxt)
    {
        if (strpos($msgctxt, "<br />") !== false || strpos($msgctxt, "\r\n") !== false) {
            $msgctxt = strpos($msgctxt, "\r\n") !== false ? explode("\r\n", $msgctxt) : explode("<br />", $msgctxt);

            $this->stream .= 'msgctxt ""' . PHP_EOL;
            for ($i = 0; $i < count($msgctxt); $i++) {
                $msgctxt[$i] = strpos($msgctxt[$i], "\r\n") !== false ? $msgctxt[$i] : preg_replace("/\r|\n/", "", $msgctxt[$i]);

                if ($i == 0) {
                    $this->stream .= '",' . $msgctxt[$i] . '\\n"';
                } else {
                    if ($i == count($msgctxt) - 1)
                        $this->stream .= '"' . $msgctxt[$i] . '"';
                    else
                        $this->stream .= '"' . $msgctxt[$i] . '\\n"';
                }

                $this->stream .= PHP_EOL;
            }
        } else {
            $this->stream .= 'msgctxt ' . '",' . $msgctxt . '"' . PHP_EOL;
        }
    }

    function addMsgID($msgID)
    {
        if (strpos($msgID, "<br />") !== false || strpos($msgID, "\r\n") !== false) {
            $msgID = strpos($msgID, "\r\n") !== false ? explode("\r\n", $msgID) : explode("<br />", $msgID);

            $this->stream .= 'msgid ""' . PHP_EOL;
            for ($i = 0; $i < count($msgID); $i++) {
                $msgID[$i] = strpos($msgID[$i], "\r\n") !== false ? $msgID[$i] : preg_replace("/\r|\n/", "", $msgID[$i]);

                if ($i == count($msgID) - 1)
                    $this->stream .= '"' . $msgID[$i] . '"';
                else
                    $this->stream .= '"' . $msgID[$i] . '\\n"';

                $this->stream .= PHP_EOL;
            }
        } else {
            $this->stream .= 'msgid ' . '"' . $msgID . '"' . PHP_EOL;
        }
    }

    function addMsgSTR($msgStr)
    {
        if (strpos($msgStr, "<br />") !== false) {
            $msgStr = explode("<br />", $msgStr);

            $this->stream .= 'msgstr ""' . PHP_EOL;
            for ($i = 0; $i < count($msgStr); $i++) {
                $msgStr[$i]     = preg_replace("/\r|\n/", "", $msgStr[$i]);

                if ($i == count($msgStr) - 1)
                    $this->stream .= '"' . $msgStr[$i] . '"';
                else
                    $this->stream .= '"' . $msgStr[$i] . '\\n"';

                $this->stream .= PHP_EOL;
            }

            $this->stream .= PHP_EOL;
        } else {
            $this->stream .= 'msgstr ' . '"' . $msgStr . '"' . PHP_EOL . PHP_EOL;
        }
    }
}
