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
|
<?php
require_once 'PoeditString.php';
class PoeditParser {
protected $header = '';
protected $strings = [];
protected function _fixQuotes($str) {
return stripslashes((string) $str);
}
public function __construct(protected $file)
{
}
public function parse() {
$contents = file_get_contents($this->file);
$parts = preg_split('#(\r\n|\n){2}#', $contents, -1, PREG_SPLIT_NO_EMPTY);
$this->header = array_shift($parts);
foreach ($parts as $part) {
// parse comments
$comments = [];
preg_match_all('#^\\#: (.*?)$#m', (string) $part, $matches, PREG_SET_ORDER);
foreach ($matches as $m) $comments[] = $m[1];
$isFuzzy = preg_match('#^\\#, fuzzy$#im', (string) $part) ? true : false;
preg_match_all('# ^ (msgid|msgstr)\ " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" $ #ixm', (string) $part, $matches2, PREG_SET_ORDER);
$k = $this->_fixQuotes($matches2[0][2]);
$v = !empty($matches2[1][2]) ? $this->_fixQuotes($matches2[1][2]) : '';
$this->strings[$k] = new PoeditString($k, $v, $isFuzzy, $comments);
}
}
public function merge($strings) {
foreach ((array)$strings as $str) {
if (!in_array($str, array_keys($this->strings))) {
$this->strings[$str] = new PoeditString($str);
}
}
}
public function getHeader() {
return $this->header;
}
public function getStrings() {
return $this->strings;
}
public function getJSON() {
$str = [];
foreach ($this->strings as $s) {
if ($s->value) $str[$s->key] = $s->value;
}
return json_encode($str, JSON_THROW_ON_ERROR);
}
public function toJSON($outputFilename, $varName = 'l10n') {
$str = "$varName = " . $this->getJSON() . ";";
return file_put_contents($outputFilename, $str) !== false;
}
public function save($filename = null) {
$data = $this->header . "\n\n";
foreach ($this->strings as $str) {
$data .= $str;
}
return file_put_contents($filename ?: $this->file, $data) !== false;
}
}
?>
|