TranslationExtractor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A extractTranslationString() 0 10 2
A readFile() 0 9 2
A extract() 0 16 4
A run() 0 8 2
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
class TranslationExtractor {
23
	private $directory;
24
	
25
	public function __construct($directory) {
26
		$this->directory = $directory;
27
	}
28
	
29
	private function extractTranslationString($line) {
30
		$regex = '/\{\{\s*t\s*[\'\"]([\w\d\s,.]*)[\'\"]\s*\}\}/';
31
		$matches = [];
32
		preg_match_all($regex, $line, $matches);
33
		$strings = [];
34
		foreach ($matches[1] as $match) {
35
			$strings[] = $match;
36
		}
37
		return $strings;
38
	}
39
	
40
	private function readFile($file) {
41
		$strings = [];
42
		$content = file_get_contents($file);
43
		$lines = explode(PHP_EOL, $content);
44
		foreach ($lines as $line) {
45
			$strings = array_merge($strings, $this->extractTranslationString($line));
46
		}
47
		return $strings;
48
	}
49
	
50
	private function extract($directory) {
51
		$strings = [];
52
		$iterator = new DirectoryIterator($directory);
53
		foreach ($iterator as $node) {
54
			if ($node->isDot()) {
55
				continue;
56
			}
57
			if ($node->isDir()) {
58
				$this->extract($node);
59
			} else {
60
				$s = $this->readFile($node->getPathname());
61
				$strings = array_merge($strings, $s);
62
			}
63
		}
64
		return array_unique(array_values($strings));
65
	}
66
	
67
	public function run() {
68
		$strings = $this->extract($this->directory);
69
		$export = '';
70
		foreach ($strings as $s) {
71
			$export .= 't(\'' . $s . '\');' . PHP_EOL;
72
		}
73
		file_put_contents('translations.js', $export);
74
	}
75
}
76
77
$te = new TranslationExtractor(dirname(__FILE__) . '/js/templates');
78
$te->run();