This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract
executable file
·118 lines (94 loc) · 2.87 KB
/
extract
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
#!/usr/bin/env php
<?php
exit(main());
function main():int {
$messages = [];
output('find_template', 'parse_template', $messages);
output('find_call', 'parse_call', $messages);
output_plural('find_templateN', 'parse_templateN', $messages);
return 0;
}
function output(callable $finder, callable $parser, array &$messages) {
foreach ($finder() as $v) {
list($path, $line, $raw) = explode(':', $v, 3);
$id = $parser($raw);
if ($id === null) {
fwrite(STDERR, "Unable to parse $path:$line: $v\n");
continue;
}
if (array_key_exists($id, $messages)) {
continue;
}
$messages[$id] = true;
echo "#: $path:$line\n";
echo "msgid $id\n";
echo "msgstr \"\"\n\n";
}
return 0;
}
function output_plural(callable $finder, callable $parser, array &$messages) {
foreach ($finder() as $v) {
list($path, $line, $raw) = explode(':', $v, 3);
list($singular, $plural) = $parser($raw);
if ($singular === null || $plural == null) {
fwrite(STDERR, "Unable to parse $path:$line: $v\n");
continue;
}
$key = $singular.$plural;
if (array_key_exists($key, $messages)) {
continue;
}
$messages[$key] = true;
echo "#: $path:$line\n";
echo "msgid $singular\n";
echo "msgid_plural $plural\n";
echo "msgstr[0] \"\"\n\n";
echo "msgstr[1] \"\"\n\n";
}
return 0;
}
function encode(string $v): string {
return preg_replace('`\\\([^"])`', '$1', json_encode($v));
}
function find_template(): array {
exec(
"find resources -type f -name '*.html' -print0 | xargs -0 grep -EHn 't(md)? \\$?.Locale \"'",
$calls,
);
return $calls;
}
function find_templateN(): array {
exec(
"find resources -type f -name '*.html' -print0 | xargs -0 grep -EHn 'tn \\$?.Locale \"'",
$calls,
);
return $calls;
}
function parse_template(string $str): ?string {
preg_match('`\{\{\s*(.+\:?=\s*)?\s*t(md)?\s+\$?\.Locale\s+"([^"]*)".*\}\}`', $str, $matches);
if (count($matches) < 4) {
return null;
}
return encode($matches[3]);
}
function parse_templateN(string $str): ?array {
preg_match('`\{\{\s*tn\s+\$?\.Locale\s+"([^"]*)"\s+"([^"]*)"\s+.*\}\}`', $str, $matches);
if (count($matches) < 3) {
return null;
}
return [encode($matches[1]), encode($matches[2])];
}
function find_call(): array {
exec("find internal -type f -name '*.go' -print0 | " .
"xargs -0 grep -EHn 's\.locales\[locale\]\.Get\(\"(.+)\"\)'",
$calls,
);
return $calls;
}
function parse_call(string $str): ?string {
preg_match('`s\.locales\[locale\]\.Get\("(.+)"\)`', $str, $matches);
if (count($matches) < 2) {
return null;
}
return encode($matches[1]);
}