]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_po_templates.php
Also extract _m() used in core.
[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.po \
38     --language=PHP \
39     --keyword="pgettext:1c,2" \
40     --keyword="npgettext:1c,2,3" \
41     --keyword="_m:1,1t" \
42     --keyword="_m:1c,2,2t" \
43     --keyword="_m:1,2,3t" \
44     --keyword="_m:1c,2,3,4t" \
45     actions/*.php \
46     classes/*.php \
47     lib/*.php \
48     scripts/*.php
49 END
50 );
51     chdir($old);
52 }
53
54 function do_update_plugin($dir, $domain)
55 {
56     $old = getcwd();
57     chdir($dir);
58     if (!file_exists('locale')) {
59         mkdir('locale');
60     }
61     $files = get_plugin_sources(".");
62     $cmd = <<<END
63 xgettext \
64     --from-code=UTF-8 \
65     --default-domain=$domain \
66     --output=locale/$domain.po \
67     --language=PHP \
68     --keyword='' \
69     --keyword="_m:1,1t" \
70     --keyword="_m:1c,2,2t" \
71     --keyword="_m:1,2,3t" \
72     --keyword="_m:1c,2,3,4t" \
73
74 END;
75     foreach ($files as $file) {
76       $cmd .= ' ' . escapeshellarg($file);
77     }
78     passthru($cmd);
79     chdir($old);
80 }
81
82 function do_translatewiki_plugin($basedir, $plugin)
83 {
84     $yamldir = "$basedir/locale/TranslateWiki";
85     if (!file_exists($yamldir)) {
86         mkdir($yamldir);
87     }
88     $outfile = "$yamldir/StatusNet-{$plugin}.yml";
89     $pluginlc = strtolower( $plugin );
90     $data = <<<END
91 ---
92 BASIC:
93   id: out-statusnet-{$pluginlc}
94   label: StatusNet - {$plugin}
95   description: "{{int:bw-desc-statusnet-plugin-{$pluginlc}}}"
96   namespace: NS_STATUSNET
97   display: out/statusnet/{$pluginlc}
98   class: GettextMessageGroup
99
100 FILES:
101   class: GettextFFS
102   sourcePattern: %GROUPROOT%/plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
103   targetPattern: plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
104   codeMap:
105     en-gb: en_GB
106     no: nb
107     pt-br: pt_BR
108     zh-hans: zh_CN
109     zh-hant: zh_TW
110
111 MANGLER
112   class: StringMatcher
113   prefix: {$pluginlc}-
114   patterns:
115     - "*"
116
117 END;
118     file_put_contents($outfile, $data);
119 }
120
121 function get_plugins($dir)
122 {
123     $plugins = array();
124     $dirs = new DirectoryIterator("$dir/plugins");
125     foreach ($dirs as $item) {
126         if ($item->isDir() && !$item->isDot()) {
127             $name = $item->getBasename();
128             if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
129                 $plugins[] = $name;
130             }
131         }
132     }
133     return $plugins;
134 }
135
136 function get_plugin_sources($dir)
137 {
138     $files = array();
139
140     $dirs = new RecursiveDirectoryIterator($dir);
141     $iter = new RecursiveIteratorIterator($dirs);
142     foreach ($iter as $pathname => $item) {
143         if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
144             $files[] = $pathname;
145         }
146     }
147     return $files;
148 }
149
150 function plugin_using_gettext($dir)
151 {
152     $files = get_plugin_sources($dir);
153     foreach ($files as $pathname) {
154         // Check if the file is using our _m gettext wrapper
155         $code = file_get_contents($pathname);
156         if (preg_match('/\b_m\(/', $code)) {
157             return true;
158         }
159     }
160
161     return false;
162 }
163
164 function update_plugin($basedir, $name)
165 {
166     $dir = "$basedir/plugins/$name";
167     if (plugin_using_gettext($dir)) {
168         do_update_plugin($dir, $name);
169         do_translatewiki_plugin($basedir, $name);
170         return true;
171     } else {
172         return false;
173     }
174 }
175
176 $args = $_SERVER['argv'];
177 array_shift($args);
178
179 $all = false;
180 $core = false;
181 $allplugins = false;
182 $plugins = array();
183 if (count($args) == 0) {
184     $all = true;
185 }
186 foreach ($args as $arg) {
187     if ($arg == '--all') {
188         $all = true;
189     } elseif ($arg == "--core") {
190         $core = true;
191     } elseif ($arg == "--plugins") {
192         $allplugins = true;
193     } elseif (substr($arg, 0, 9) == "--plugin=") {
194         $plugins[] = substr($arg, 9);
195     } elseif ($arg == '--help') {
196         echo "options: --all --core --plugins --plugin=Foo\n\n";
197         exit(0);
198     }
199 }
200
201
202
203 if ($all || $core) {
204     echo "core...";
205     update_core(INSTALLDIR, 'statusnet');
206     echo " ok\n";
207 }
208 if ($all || $allplugins) {
209     $plugins = get_plugins(INSTALLDIR);
210 }
211 if ($plugins) {
212     foreach ($plugins as $plugin) {
213         echo "$plugin...";
214         if (update_plugin(INSTALLDIR, $plugin)) {
215             echo " ok\n";
216         } else {
217             echo " not localized\n";
218         }
219     }
220 }