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