]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_translations.php
0dee4f5a55f983801bbac10581fce3d33e810f17
[quix0rs-gnu-social.git] / scripts / update_translations.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 define('LACONICA', true);
29
30 require_once(INSTALLDIR . '/lib/common.php');
31
32 // Master StatusNet .pot file location (created by update_pot.sh)
33 $laconica_pot = INSTALLDIR . '/locale/laconica.po';
34
35 set_time_limit(60);
36
37 /* Languages to pull */
38 $languages = get_all_languages();
39
40 /* Update the languages */
41
42 foreach ($languages as $language) {
43
44     $code = $language['lang'];
45     $file_url = 'http://status.net/pootle/' . $code .
46         '/laconica/LC_MESSAGES/laconica.po';
47     $lcdir = INSTALLDIR . '/locale/' . $code;
48     $msgdir = "$lcdir/LC_MESSAGES";
49     $pofile = "$msgdir/laconica.po";
50     $mofile = "$msgdir/laconica.mo";
51
52     /* Check for an existing */
53     if (!is_dir($msgdir)) {
54         mkdir($lcdir);
55         mkdir($msgdir);
56         $existingSHA1 = '';
57     } else {
58         $existingSHA1 = file_exists($pofile) ? sha1_file($pofile) : '';
59     }
60
61     /* Get the remote one */
62     $new_file = curl_get_file($file_url);
63
64     if ($new_file === FALSE) {
65         echo "Couldn't retrieve .po file for $code: $file_url\n";
66         continue;
67     }
68
69     // Update if the local .po file is different to the one downloaded, or
70     // if the .mo file is not present.
71     if (sha1($new_file) != $existingSHA1 || !file_exists($mofile)) {
72         echo "Updating ".$code."\n";
73         file_put_contents($pofile, $new_file);
74         system(sprintf('msgmerge -U %s %s', $pofile, $laconica_pot));
75         system(sprintf('msgfmt -f -o %s %s', $mofile, $pofile));
76     } else {
77         echo "Unchanged - ".$code."\n";
78     }
79 }
80
81 echo "Finished\n";
82
83
84 function curl_get_file($url)
85 {
86     $c = curl_init();
87     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
88     curl_setopt($c, CURLOPT_URL, $url);
89     $contents = curl_exec($c);
90     curl_close($c);
91
92     if (!empty($contents)) {
93         return $contents;
94     }
95
96     return FALSE;
97 }