This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
translation-extractor.php
78 lines (70 loc) · 2.03 KB
/
translation-extractor.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
<?php
/**
* @author Christoph Wurst <[email protected]>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
class TranslationExtractor {
private $directory;
public function __construct($directory) {
$this->directory = $directory;
}
private function extractTranslationString($line) {
$regex = '/\{\{\s*t\s*[\'\"]([\w\d\s,.]*)[\'\"]\s*\}\}/';
$matches = [];
preg_match_all($regex, $line, $matches);
$strings = [];
foreach ($matches[1] as $match) {
$strings[] = $match;
}
return $strings;
}
private function readFile($file) {
$strings = [];
$content = file_get_contents($file);
$lines = explode(PHP_EOL, $content);
foreach ($lines as $line) {
$strings = array_merge($strings, $this->extractTranslationString($line));
}
return $strings;
}
private function extract($directory) {
$strings = [];
$iterator = new DirectoryIterator($directory);
foreach ($iterator as $node) {
if ($node->isDot()) {
continue;
}
if ($node->isDir()) {
$this->extract($node);
} else {
$s = $this->readFile($node->getPathname());
$strings = array_merge($strings, $s);
}
}
return array_unique(array_values($strings));
}
public function run() {
$strings = $this->extract($this->directory);
$export = '';
foreach ($strings as $s) {
$export .= 't(\'' . $s . '\');' . PHP_EOL;
}
file_put_contents('translations.js', $export);
}
}
$te = new TranslationExtractor(dirname(__FILE__) . '/js/templates');
$te->run();