forked from Prisjakt/php-po2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Po2Json.php
176 lines (143 loc) · 5.01 KB
/
Po2Json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace neam\po2json;
class Po2Json
{
static public function parseVariable(
$contents,
$fuzzy = false,
$format = 'raw',
$domain = 'messages'
) {
// Parse po contents
$poparser = new \Sepia\PoParser();
$translations = $poparser->readVariable($contents);
$_headers = self::parseHeaders($poparser->headers());
return self::convertToJson($_headers, $translations, $fuzzy, $format, $domain);
}
static public function parseFile(
$path,
$fuzzy = false,
$format = 'raw',
$domain = 'messages'
) {
// Parse po file
$poparser = new \Sepia\PoParser();
$translations = $poparser->read($path);
$_headers = self::parseHeaders($poparser->headers());
return self::convertToJson($_headers, $translations, $fuzzy, $format, $domain);
}
static public function convertToJson(
$_headers,
$translations,
$fuzzy = false,
$format = 'raw',
$domain = 'messages'
) {
$headers = array();
foreach ($_headers as $key => $value) {
$key = strtolower($key);
$headers[$key] = $value;
}
// Attach headers (overwrites any empty translation keys that may have somehow gotten in)
$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"]);
}
// msg id json format
if ($t["msgid"][0] == '' && isset($t["msgid"][1])) {
array_shift($t["msgid"]);
$msgid = implode("", $t["msgid"]);
} else {
$msgid = implode("", $t["msgid"]);
}
// json object key based on msd id and context
if (isset($t["msgctxt"][0])) {
$key = $t["msgctxt"][0] . "\x04" . $msgid;
} else {
$key = $msgid;
}
// do not include fuzzy messages if not wanted
if (!empty($t["fuzzy"])) {
if (!$fuzzy) {
continue;
} else {
// todo
// if (!fuzzy || options . fuzzy) {result}[translationKey] = [t . msgid_plural ? t . msgid_plural : null] . concat(t . msgstr);
throw new \CException("TODO");
}
}
$result[$key] = $entry;
}
// Make JSON fully Jed-compatible
if ($format === 'jed') {
$jed = array(
"domain" => $domain,
"locale_data" => array(),
);
$jed["locale_data"][$domain] = $result;
$jed["locale_data"][$domain][""] = array(
"domain" => $domain,
"plural_forms" => isset($result[""]["plural-forms"]) ? $result[""]["plural-forms"] : null,
"lang" => $result[""]["language"],
);
$result = $jed;
}
return $result;
}
static public function toJSON(
$path,
$fuzzy = false,
$format = 'raw',
$domain = 'messages'
) {
return json_encode(self::parseFile($path, $fuzzy, $format, $domain));
}
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;
}
}