]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_translations.php
Make attachment fit better in notice: drop text and link
[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', dirname(__DIR__));
28 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
29 define('GNUSOCIAL', true);
30 define('STATUSNET', true);  // compatibility
31
32 require_once(INSTALLDIR . '/lib/common.php');
33
34 // Master StatusNet .pot file location (created by update_pot.sh)
35 $statusnet_pot = INSTALLDIR . '/locale/statusnet.pot';
36
37 set_time_limit(60);
38
39 /* Languages to pull */
40 $languages = get_all_languages();
41
42 /* Update the languages */
43 // Language code conversion for translatewiki.net (these are MediaWiki codes)
44 $codeMap = array(
45         'nb'    => 'no',
46         'pt_BR' => 'pt-br',
47         'zh_CN' => 'zh-hans',
48         'zh_TW' => 'zh-hant'
49 );
50
51 $doneCodes = array();
52
53 foreach ($languages as $language) {
54         $code = $language['lang'];
55
56         // Skip export of source language
57         // and duplicates
58         if( $code == 'en' || $code == 'no' ) {
59                 continue;
60         }
61
62         // Do not export codes twice (happens for 'nb')
63         if( in_array( $code, $doneCodes ) ) {
64                 continue;
65         } else {
66                 $doneCodes[] = $code;
67         }
68
69         // Convert code if needed
70         if( isset( $codeMap[$code] ) ) {
71                 $twnCode = $codeMap[$code];
72         } else {
73                 $twnCode = str_replace('_', '-', strtolower($code)); // pt_BR -> pt-br
74         }
75
76     // Fetch updates from translatewiki.net...
77     $file_url = 'http://translatewiki.net/w/i.php?' .
78         http_build_query(array(
79             'title' => 'Special:Translate',
80             'task' => 'export-to-file',
81             'group' => 'out-statusnet-core',
82             'language' => $twnCode));
83
84     $lcdir = INSTALLDIR . '/locale/' . $code;
85     $msgdir = "$lcdir/LC_MESSAGES";
86     $pofile = "$msgdir/statusnet.po";
87     $mofile = "$msgdir/statusnet.mo";
88
89     /* Check for an existing */
90     if (!is_dir($msgdir)) {
91         mkdir($lcdir);
92         mkdir($msgdir);
93         $existingSHA1 = '';
94     } else {
95         $existingSHA1 = file_exists($pofile) ? sha1_file($pofile) : '';
96     }
97
98     /* Get the remote one */
99     $new_file = curl_get_file($file_url);
100
101     if ($new_file === FALSE) {
102         echo "Could not retrieve .po file for $code: $file_url\n";
103         continue;
104     }
105
106     // Update if the local .po file is different to the one downloaded, or
107     // if the .mo file is not present.
108     if (sha1($new_file) != $existingSHA1 || !file_exists($mofile)) {
109         echo "Updating ".$code."\n";
110         file_put_contents($pofile, $new_file);
111         // --backup=off is workaround for Mac OS X fail
112         system(sprintf('msgmerge -U --backup=off %s %s', $pofile, $statusnet_pot));
113         /* Do not rebuild/add .mo files by default
114          * FIXME: should be made a command line parameter.
115         system(sprintf('msgfmt -o %s %s', $mofile, $pofile));
116          */
117     } else {
118         echo "Unchanged - ".$code."\n";
119     }
120 }
121
122 echo "Finished\n";
123
124
125 function curl_get_file($url)
126 {
127     $c = curl_init();
128     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
129     curl_setopt($c, CURLOPT_URL, $url);
130     $contents = curl_exec($c);
131     curl_close($c);
132
133     if (!empty($contents)) {
134         return $contents;
135     }
136
137     return FALSE;
138 }