forked from mapkyca/known-language-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildpot.php
49 lines (38 loc) · 1.45 KB
/
buildpot.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
<?php
/**
* Script to build a .pot Gettext language file.
*
* Usage: cat MyFile.php | php buildpot.php > strings.pot
*
* @author Marcus Povey <[email protected]>
* @package Known-Language-Tools
*/
$in = file_get_contents("php://stdin");
$filenames = explode("\n", $in);
$handled = [];
function getLineNumber($content, $charpos)
{
list($before) = str_split($content, $charpos); // fetches all the text before the match
return strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
}
foreach ($filenames as $filename) {
$file = @file_get_contents($filename);
if (!empty($file)) {
if (preg_match_all('/_\((\'|")(.*)(\'|")(\)|,)/imsU', $file, $matches, PREG_OFFSET_CAPTURE)) {
foreach ($matches[2] as $translation) {
$string = $translation[0];
$offset = $translation[1];
$linenumber = getLineNumber($file, $offset);
$string = str_replace(["\'",'\"'],["'",'"'],$string);
//$normalised_string = str_replace('"', '\"', $string);
$normalised_string = addcslashes($string, '"');
if (!in_array($normalised_string, $handled)) {
echo "#: $filename:$linenumber\n";
echo "msgid \"$normalised_string\"\n";
echo "msgstr \"\"\n\n";
$handled[] = $normalised_string; // duplication prevention
}
}
}
}
}