]> git.mxchange.org Git - friendica.git/blob - src/Console/Extract.php
The value is used twice, so use a variable
[friendica.git] / src / Console / Extract.php
1 <?php
2
3 namespace Friendica\Console;
4
5 /**
6  * Extracts translation strings from the Friendica project's files to be exported
7  * to Transifex for translation.
8  *
9  * Outputs a PHP file with language strings used by Friendica
10  *
11  * @author Hypolite Petovan <hypolite@mrpetovan.com>
12  */
13 class Extract extends \Asika\SimpleConsole\Console
14 {
15         protected $helpOptions = ['h', 'help', '?'];
16
17         protected function getHelp()
18         {
19                 $help = <<<HELP
20 console extract - Generate translation string file for the Friendica project (deprecated)
21 Usage
22         bin/console extract [-h|--help|-?] [-v]
23
24 Description
25         This script was used to generate the translation string file to be exported to Transifex,
26         please use bin/run_xgettext.sh instead
27
28 Options
29     -h|--help|-? Show help information
30     -v           Show more debug information.
31 HELP;
32                 return $help;
33         }
34
35         protected function doExecute()
36         {
37                 if ($this->getOption('v')) {
38                         $this->out('Class: ' . __CLASS__);
39                         $this->out('Arguments: ' . var_export($this->args, true));
40                         $this->out('Options: ' . var_export($this->options, true));
41                 }
42
43                 if (count($this->args) > 0) {
44                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
45                 }
46
47                 $s = '<?php' . PHP_EOL;
48                 $s .= '
49                 function string_plural_select($n){
50                         return ($n != 1);
51                 }
52
53                 ';
54
55                 $arr = [];
56
57                 $files = array_merge(
58                         ['index.php', 'boot.php'],
59                         glob('mod/*'),
60                         glob('include/*'),
61                         glob('addon/*/*'),
62                         $this->globRecursive('src')
63                 );
64
65                 foreach ($files as $file) {
66                         $str = file_get_contents($file);
67
68                         $pat = '|L10n::t\(([^\)]*+)[\)]|';
69                         $patt = '|L10n::tt\(([^\)]*+)[\)]|';
70
71                         $matches = [];
72                         $matchestt = [];
73
74                         preg_match_all($pat, $str, $matches);
75                         preg_match_all($patt, $str, $matchestt);
76
77                         if (count($matches) || count($matchestt)) {
78                                 $s .= '// ' . $file . PHP_EOL;
79                         }
80
81                         if (!empty($matches[1])) {
82                                 foreach ($matches[1] as $long_match) {
83                                         $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
84                                         $match = $match_arr[0];
85                                         if (!in_array($match, $arr)) {
86                                                 if (substr($match, 0, 1) == '$') {
87                                                         continue;
88                                                 }
89
90                                                 $arr[] = $match;
91
92                                                 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
93                                         }
94                                 }
95                         }
96                         if (!empty($matchestt[1])) {
97                                 foreach ($matchestt[1] as $match) {
98                                         $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
99                                         if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
100                                                 if (substr($matchtkns[1], 0, 1) == '$') {
101                                                         continue;
102                                                 }
103
104                                                 $arr[] = $matchtkns[0];
105
106                                                 $s .= '$a->strings[' . $matchtkns[0] . "] = array(\n";
107                                                 $s .= "\t0 => " . $matchtkns[0] . ",\n";
108                                                 $s .= "\t1 => " . $matchtkns[1] . ",\n";
109                                                 $s .= ");\n";
110                                         }
111                                 }
112                         }
113                 }
114
115                 $s .= '// Timezones' . PHP_EOL;
116
117                 $zones = timezone_identifiers_list();
118                 foreach ($zones as $zone) {
119                         $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
120                 }
121
122                 $this->out($s);
123
124                 return 0;
125         }
126
127         private function globRecursive($path) {
128                 $dir_iterator = new \RecursiveDirectoryIterator($path);
129                 $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
130
131                 $return = [];
132                 foreach ($iterator as $file) {
133                         if ($file->getBasename() != '.' && $file->getBasename() != '..') {
134                                 $return[] = $file->getPathname();
135                         }
136                 }
137
138                 return $return;
139         }
140 }