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