Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support of latest version php-poparser and codeception #3

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 29 additions & 77 deletions Po2Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ static public function parseVariable(
) {

// Parse po contents
$poparser = new \Sepia\PoParser();
$translations = $poparser->readVariable($contents);
$_headers = self::parseHeaders($poparser->headers());
return self::convertToJson($_headers, $translations, $fuzzy, $format, $domain);
$stringHandler = new \Sepia\PoParser\SourceHandler\StringSource($contents);
$poparser = new \Sepia\PoParser\Parser($stringHandler);
$catalog = $poparser->parse();
return self::convertToJson($catalog, $fuzzy, $format, $domain);

}

Expand All @@ -28,23 +28,22 @@ static public function parseFile(
) {

// Parse po file
$poparser = new \Sepia\PoParser();
$translations = $poparser->read($path);
$_headers = self::parseHeaders($poparser->headers());
return self::convertToJson($_headers, $translations, $fuzzy, $format, $domain);
$fileHandler = new \Sepia\PoParser\SourceHandler\FileSystem($path);
$poparser = new \Sepia\PoParser\Parser($fileHandler);
$catalog = $poparser->parse();
return self::convertToJson($catalog, $fuzzy, $format, $domain);

}

static public function convertToJson(
$_headers,
$translations,
$catalog,
$fuzzy = false,
$format = 'raw',
$domain = 'messages'
) {

$headers = array();
foreach ($_headers as $key => $value) {
foreach (self::parseHeaders($catalog->getHeaders()) as $key => $value) {
$key = strtolower($key);
$headers[$key] = $value;
}
Expand All @@ -53,36 +52,26 @@ static public function convertToJson(
$result[""] = $headers;

// Create gettext/Jed compatible JSON from parsed data
foreach ($translations as $translationKey => $t) {

$entry = array();
if (isset($t["msgid_plural"])) {
$entry[0] = isset($t["msgid_plural"]) ? $t["msgid_plural"][0] : null;
$entry[1] = $t["msgstr"][0];
isset($t["msgstr"][1]) ? ($entry[2] = $t["msgstr"][1]) : null;
isset($t["msgstr"][2]) ? ($entry[3] = $t["msgstr"][2]) : null;
} else {
$entry[0] = null;
$entry[1] = implode("", $t["msgstr"]);
}
foreach ($catalog->getEntries() as $entry) {

// msg id json format
if ($t["msgid"][0] == '' && isset($t["msgid"][1])) {
array_shift($t["msgid"]);
$msgid = implode("", $t["msgid"]);
if ($entry->isPlural()) {
$msg = array_merge(
array($entry->getMsgIdPlural()),
$entry->getMsgStrPlurals()
);
} else {
$msgid = implode("", $t["msgid"]);
$msg = array(null, $entry->getMsgStr());
}

// json object key based on msd id and context
if (isset($t["msgctxt"][0])) {
$key = $t["msgctxt"][0] . "\x04" . $msgid;
} else {
$key = $msgid;
}
$msgid = $entry->getMsgId();
$msgctxt = $entry->getMsgCtxt();
if ($msgctxt)
$msgid = "$msgctxt\x04$msgid";

// do not include fuzzy messages if not wanted
if (!empty($t["fuzzy"])) {
if ($entry->isFuzzy()) {
if (!$fuzzy) {
continue;
} else {
Expand All @@ -92,7 +81,7 @@ static public function convertToJson(
}
}

$result[$key] = $entry;
$result[$msgid] = $msg;

}

Expand Down Expand Up @@ -129,48 +118,11 @@ static public function toJSON(

static public function parseHeaders($headers)
{
foreach ($headers as &$h) {
$h = trim($h, "\"\n");
}
$raw = implode("", $headers);
$raw = str_replace('\n', "\n", $raw);
return self::parse_http_headers($raw);
}

/**
* From http://stackoverflow.com/a/20933560/682317
* @param $raw_headers
* @return array
*/
static protected function parse_http_headers($raw_headers)
{

$headers = array();
$key = '';

foreach (explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);

if (isset($h[1])) {
if (!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
} else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
}

$key = $h[0];
} else {
if (substr($h[0], 0, 1) == "\t") {
$headers[$key] .= "\r\n\t" . trim($h[0]);
} elseif (!$key) {
$headers[0] = trim($h[0]);
}
}
}

return $headers;
$result = array();
foreach($headers as $h)
if (preg_match('/^([^:]+): *(.*)$/', $h, $m))
$result[$m[1]] = $m[2];
return $result;
}

}
}
22 changes: 7 additions & 15 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
paths:
tests: tests
log: tests/_log
output: tests/_output
data: tests/_data
helpers: tests/_helpers
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
log: true
modules:
config:
Db:
dsn: ''
user: ''
password: ''
dump: tests/_data/dump.sql
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
14 changes: 5 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
"email": "[email protected]"
}
],
"repositories": [
{
"type":"vcs",
"url":"https://github.com/Gapminder/PHP-po-parser.git"
}
],
"autoload": {
"classmap": ["."]
},
"require": {
"php": ">=5.3.0",
"sepia/po-parser": "dev-master"
"sepia/po-parser": "^6.0"
},
"require-dev": {
"codeception/codeception": "~1.8.3"
"codeception/codeception": "~4.1.31",
"codeception/module-phpbrowser": "^1.0.0",
"codeception/module-asserts": "^1.0.0"
}
}
}
Loading