]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_po_templates.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / scripts / update_po_templates.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
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 published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 // Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23     print "This script must be run from the command line\n";
24     exit();
25 }
26
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28
29 function update_core($dir, $domain)
30 {
31     $old = getcwd();
32     chdir($dir);
33     passthru(<<<END
34 xgettext \
35     --from-code=UTF-8 \
36     --default-domain=$domain \
37     --output=locale/$domain.pot \
38     --language=PHP \
39     --add-comments=TRANS \
40     --keyword="_m:1,1t" \
41     --keyword="_m:1c,2,2t" \
42     --keyword="_m:1,2,3t" \
43     --keyword="_m:1c,2,3,4t" \
44     --keyword="pgettext:1c,2" \
45     --keyword="npgettext:1c,2,3" \
46     actions/*.php \
47     classes/*.php \
48     lib/*.php \
49     scripts/*.php
50 END
51 );
52     chdir($old);
53 }
54
55 function do_update_plugin($dir, $domain)
56 {
57     $old = getcwd();
58     chdir($dir);
59     if (!file_exists('locale')) {
60         mkdir('locale');
61     }
62     $files = get_plugin_sources(".");
63     $cmd = <<<END
64 xgettext \
65     --from-code=UTF-8 \
66     --default-domain=$domain \
67     --output=locale/$domain.pot \
68     --language=PHP \
69     --add-comments=TRANS \
70     --keyword='' \
71     --keyword="_m:1,1t" \
72     --keyword="_m:1c,2,2t" \
73     --keyword="_m:1,2,3t" \
74     --keyword="_m:1c,2,3,4t" \
75
76 END;
77     foreach ($files as $file) {
78       $cmd .= ' ' . escapeshellarg($file);
79     }
80     passthru($cmd);
81     chdir($old);
82 }
83
84 function do_translatewiki_plugin($basedir, $plugin)
85 {
86     $yamldir = "$basedir/locale/TranslateWiki";
87     if (!file_exists($yamldir)) {
88         mkdir($yamldir);
89     }
90     $outfile = "$yamldir/StatusNet-{$plugin}.yml";
91     $pluginlc = strtolower( $plugin );
92     $data = <<<END
93 ---
94 BASIC:
95   id: out-statusnet-{$pluginlc}
96   label: StatusNet - {$plugin}
97   namespace: NS_STATUSNET
98   description: "{{int:bw-desc-statusnet-plugin}}"
99   class: FileBasedMessageGroup
100   display: out/statusnet/{$pluginlc}
101
102 FILES:
103   class: GettextFFS
104   sourcePattern: %GROUPROOT%/statusnet/plugins/{$plugin}/locale/{$plugin}.pot
105   targetPattern: statusnet/plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
106   codeMap:
107     en-gb: en_GB
108     no: nb
109     pt-br: pt_BR
110     zh-hans: zh_CN
111     zh-hant: zh_TW
112
113 MANGLER
114   class: StringMatcher
115   prefix: {$pluginlc}-
116   patterns:
117     - "*"
118
119 END;
120     file_put_contents($outfile, $data);
121 }
122
123 function get_plugins($dir)
124 {
125     $plugins = array();
126     $dirs = new DirectoryIterator("$dir/plugins");
127     foreach ($dirs as $item) {
128         if ($item->isDir() && !$item->isDot()) {
129             $name = $item->getBasename();
130             if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
131                 $plugins[] = $name;
132             }
133         }
134     }
135     return $plugins;
136 }
137
138 function get_plugin_sources($dir)
139 {
140     $files = array();
141
142     $dirs = new RecursiveDirectoryIterator($dir);
143     $iter = new RecursiveIteratorIterator($dirs);
144     foreach ($iter as $pathname => $item) {
145         if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
146             $files[] = $pathname;
147         }
148     }
149     return $files;
150 }
151
152 function plugin_using_gettext($dir)
153 {
154     $files = get_plugin_sources($dir);
155     foreach ($files as $pathname) {
156         // Check if the file is using our _m gettext wrapper
157         $code = file_get_contents($pathname);
158         if (preg_match('/\b_m\(/', $code)) {
159             return true;
160         }
161     }
162
163     return false;
164 }
165
166 function update_plugin($basedir, $name)
167 {
168     $dir = "$basedir/plugins/$name";
169     if (plugin_using_gettext($dir)) {
170         do_update_plugin($dir, $name);
171         do_translatewiki_plugin($basedir, $name);
172         return true;
173     } else {
174         return false;
175     }
176 }
177
178 $args = $_SERVER['argv'];
179 array_shift($args);
180
181 $all = false;
182 $core = false;
183 $allplugins = false;
184 $plugins = array();
185 if (count($args) == 0) {
186     $all = true;
187 }
188 foreach ($args as $arg) {
189     if ($arg == '--all') {
190         $all = true;
191     } elseif ($arg == "--core") {
192         $core = true;
193     } elseif ($arg == "--plugins") {
194         $allplugins = true;
195     } elseif (substr($arg, 0, 9) == "--plugin=") {
196         $plugins[] = substr($arg, 9);
197     } elseif ($arg == '--help') {
198         echo "options: --all --core --plugins --plugin=Foo\n\n";
199         exit(0);
200     }
201 }
202
203
204
205 if ($all || $core) {
206     echo "core...";
207     update_core(INSTALLDIR, 'statusnet');
208     echo " ok\n";
209 }
210 if ($all || $allplugins) {
211     $plugins = get_plugins(INSTALLDIR);
212 }
213 if ($plugins) {
214     foreach ($plugins as $plugin) {
215         echo "$plugin...";
216         if (update_plugin(INSTALLDIR, $plugin)) {
217             echo " ok\n";
218         } else {
219             echo " not localized\n";
220         }
221     }
222 }