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
<?php

$langsDir = './langs';

$dir = isset($argv[1]) ? $argv[1] : dirname(__FILE__);
$dir = realpath($dir);

$langs = array_slice($argv, 2);

if ( !is_dir($langsDir) )
	mkdir($langsDir);

$strings = process_dir($dir);

foreach ($langs as $lang) {
	$content = "<?php \n";
	foreach ($strings as $str) {
		$content .= "\$strings['$lang'][$str] = '';\n";
	}
	file_put_contents($langsDir.DIRECTORY_SEPARATOR.$lang.'.php', $content);
}



function process_file($path) {

	$content = file_get_contents($path);

	// Remove disturbing whitespaces
	$content = preg_replace('/\s*\(\s*/u', '(', $content);

	$tokens = token_get_all($content);

	$result = array();

	foreach($tokens as $i => $t){

		if ( T_STRING == $t[0] && 't' == $t[1] ){

			if ( '(' == $tokens[$i + 1] ){
				$fnArg = $tokens[$i + 2];
				if ( is_array($fnArg) && T_CONSTANT_ENCAPSED_STRING == $fnArg[0] ){
					$result []= $fnArg[1];
				}
			}
		}
	}

	return $result;
}

// For each php file in directory call process_file function
function process_dir($dir) {

    $dir = rtrim($dir, '/\\');

    if ( $dh = opendir($dir) ) {

        $strings = array();

        while(($file = readdir($dh)) !== false) {

    		if ( '.' !== $file && '..' != $file ) {
	            
	            $path = $dir . DIRECTORY_SEPARATOR . $file;

	            if ( is_dir($path) ){
	            	$tmp = process_dir($path);
	                if ( is_array($tmp) )
	                    $strings = array_merge($strings, $tmp); 
	            }
	            else {
	                $ext = pathinfo($path, PATHINFO_EXTENSION);
	                if ( 'php' == $ext ){
	            		$tmp = process_file($path);
	            		$strings = array_merge($strings, $tmp); 
	                }                
	            }
    		}
        }

        $strings = array_unique($strings);

        closedir($dh);
        return $strings;
    }
    return false;
}