3 * @copyright Copyright (C) 2020, Friendica
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;
25 * Extracts translation strings from the Friendica project's files to be exported
26 * to Transifex for translation.
28 * Outputs a PHP file with language strings used by Friendica
30 class Extract extends \Asika\SimpleConsole\Console
32 protected $helpOptions = ['h', 'help', '?'];
34 protected function getHelp()
37 console extract - Generate translation string file for the Friendica project (deprecated)
39 bin/console extract [-h|--help|-?] [-v]
42 This script was used to generate the translation string file to be exported to Transifex,
43 please use bin/run_xgettext.sh instead
46 -h|--help|-? Show help information
47 -v Show more debug information.
52 protected function doExecute()
54 if ($this->getOption('v')) {
55 $this->out('Class: ' . __CLASS__);
56 $this->out('Arguments: ' . var_export($this->args, true));
57 $this->out('Options: ' . var_export($this->options, true));
60 if (count($this->args) > 0) {
61 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
64 $s = '<?php' . PHP_EOL;
66 function string_plural_select($n){
75 ['index.php', 'boot.php'],
79 $this->globRecursive('src')
82 foreach ($files as $file) {
83 $str = file_get_contents($file);
85 $pat = '|->t\(([^\)]*+)[\)]|';
86 $patt = '|->tt\(([^\)]*+)[\)]|';
91 preg_match_all($pat, $str, $matches);
92 preg_match_all($patt, $str, $matchestt);
94 if (count($matches) || count($matchestt)) {
95 $s .= '// ' . $file . PHP_EOL;
98 if (!empty($matches[1])) {
99 foreach ($matches[1] as $long_match) {
100 $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
101 $match = $match_arr[0];
102 if (!in_array($match, $arr)) {
103 if (substr($match, 0, 1) == '$') {
109 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
113 if (!empty($matchestt[1])) {
114 foreach ($matchestt[1] as $match) {
115 $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
116 if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
117 if (substr($matchtkns[1], 0, 1) == '$') {
121 $arr[] = $matchtkns[0];
123 $s .= '$a->strings[' . $matchtkns[0] . "] = array(\n";
124 $s .= "\t0 => " . $matchtkns[0] . ",\n";
125 $s .= "\t1 => " . $matchtkns[1] . ",\n";
132 $s .= '// Timezones' . PHP_EOL;
134 $zones = timezone_identifiers_list();
135 foreach ($zones as $zone) {
136 $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
144 private function globRecursive($path) {
145 $dir_iterator = new \RecursiveDirectoryIterator($path);
146 $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
149 foreach ($iterator as $file) {
150 if ($file->getBasename() != '.' && $file->getBasename() != '..') {
151 $return[] = $file->getPathname();