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