]> git.mxchange.org Git - friendica.git/blob - util/extract.php
Merge pull request #4614 from annando/dir-own-contact
[friendica.git] / util / extract.php
1 #!/usr/bin/env php
2 <?php
3
4 /**
5  * @file util/extract.php
6  *
7  * Extracts translation strings from the Friendica project's files to be exported
8  * to Transifex for translation.
9  *
10  * Outputs a PHP file with language strings used by Friendica
11  */
12
13 $s = '<?php' . PHP_EOL;
14 $s .= '
15 function string_plural_select($n){
16         return ($n != 1);
17 }
18
19 ';
20
21 $arr = [];
22
23 $files = ['index.php', 'boot.php'];
24 $files = array_merge(
25         $files,
26         glob('mod/*'),
27         glob('include/*'),
28         glob('addon/*/*'),
29         glob_recursive('src')
30 );
31
32 foreach ($files as $file) {
33         $str = file_get_contents($file);
34
35         $pat = '|L10n::t\(([^\)]*+)[\)]|';
36         $patt = '|L10n::tt\(([^\)]*+)[\)]|';
37
38         preg_match_all($pat, $str, $matches);
39         preg_match_all($patt, $str, $matchestt);
40
41         if (count($matches) || count($matchestt)) {
42                 $s .= '// ' . $file . PHP_EOL;
43         }
44
45         if (count($matches)) {
46                 foreach ($matches[1] as $long_match) {
47                         $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
48                         $match = $match_arr[0];
49                         if (!in_array($match, $arr)) {
50                                 if (substr($match, 0, 1) == '$') {
51                                         continue;
52                                 }
53
54                                 $arr[] = $match;
55
56                                 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
57                         }
58                 }
59         }
60         if (count($matchestt)) {
61                 foreach ($matchestt[1] as $match) {
62                         $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
63                         if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
64                                 if (substr($matchtkns[1], 0, 1) == '$') {
65                                         continue;
66                                 }
67
68                                 $arr[] = $matchtkns[0];
69
70                                 $s .= '$a->strings[' . $matchtkns[0] . "] = array(\n";
71                                 $s .= "\t0 => " . $matchtkns[0] . ",\n";
72                                 $s .= "\t1 => " . $matchtkns[1] . ",\n";
73                                 $s .= ");\n";
74                         }
75                 }
76         }
77 }
78
79 $s .= '// Timezones' . PHP_EOL;
80
81 $zones = timezone_identifiers_list();
82 foreach ($zones as $zone) {
83         $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
84 }
85
86 echo $s;
87
88 function glob_recursive($path) {
89         $dir_iterator = new RecursiveDirectoryIterator($path);
90         $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
91
92         $return = [];
93         foreach ($iterator as $file) {
94                 if ($file->getBasename() != '.' && $file->getBasename() != '..') {
95                         $return[] = $file->getPathname();
96                 }
97         }
98
99         return $return;
100 }