3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use \RecursiveDirectoryIterator;
25 use \RecursiveIteratorIterator;
28 * Extracts translation strings from the Friendica project's files to be exported
29 * to Transifex for translation.
31 * Outputs a PHP file with language strings used by Friendica
33 class Extract extends \Asika\SimpleConsole\Console
35 protected $helpOptions = ['h', 'help', '?'];
37 protected function getHelp()
40 console extract - Generate translation string file for the Friendica project (deprecated)
42 bin/console extract [-h|--help|-?] [-v]
45 This script was used to generate the translation string file to be exported to Transifex,
46 please use bin/run_xgettext.sh instead
49 -h|--help|-? Show help information
50 -v Show more debug information.
55 protected function doExecute(): int
57 if ($this->getOption('v')) {
58 $this->out('Class: ' . __CLASS__);
59 $this->out('Arguments: ' . var_export($this->args, true));
60 $this->out('Options: ' . var_export($this->options, true));
63 if (count($this->args) > 0) {
64 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
67 $s = '<?php' . PHP_EOL;
69 function string_plural_select($n){
81 $this->globRecursive('src')
84 foreach ($files as $file) {
85 $str = file_get_contents($file);
87 $pat = '|->t\(([^\)]*+)[\)]|';
88 $patt = '|->tt\(([^\)]*+)[\)]|';
93 preg_match_all($pat, $str, $matches);
94 preg_match_all($patt, $str, $matchestt);
96 if (count($matches) || count($matchestt)) {
97 $s .= '// ' . $file . PHP_EOL;
100 if (!empty($matches[1])) {
101 foreach ($matches[1] as $long_match) {
102 $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
103 $match = $match_arr[0];
104 if (!in_array($match, $arr)) {
105 if (substr($match, 0, 1) == '$') {
111 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
115 if (!empty($matchestt[1])) {
116 foreach ($matchestt[1] as $match) {
117 $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
118 if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
119 if (substr($matchtkns[1], 0, 1) == '$') {
123 $arr[] = $matchtkns[0];
125 $s .= '$a->strings[' . $matchtkns[0] . "] = [\n";
126 $s .= "\t0 => " . $matchtkns[0] . ",\n";
127 $s .= "\t1 => " . $matchtkns[1] . ",\n";
134 $s .= '// Timezones' . PHP_EOL;
136 $zones = timezone_identifiers_list();
137 foreach ($zones as $zone) {
138 $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
147 * Returns an array with found files and directories including their paths.
149 * @param string $path Base path to scan
151 * @return array A flat array with found files and directories
153 private function globRecursive(string $path): array
155 $dir_iterator = new RecursiveDirectoryIterator($path);
156 $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
159 foreach ($iterator as $file) {
160 if ($file->getBasename() != '.' && $file->getBasename() != '..') {
161 $return[] = $file->getPathname();