update_translations.php 3.84 KB
Newer Older
1
#!/usr/bin/env php
Evan Prodromou's avatar
Evan Prodromou committed
2
<?php
3
/*
4
 * StatusNet - the distributed open-source microblogging tool
5
 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
Evan Prodromou's avatar
Evan Prodromou committed
28
define('STATUSNET', true);
29
define('LACONICA', true); // compatibility
30 31 32

require_once(INSTALLDIR . '/lib/common.php');

33
// Master StatusNet .pot file location (created by update_pot.sh)
34
$statusnet_pot = INSTALLDIR . '/locale/statusnet.pot';
Evan Prodromou's avatar
Evan Prodromou committed
35 36 37 38

set_time_limit(60);

/* Languages to pull */
39
$languages = get_all_languages();
Evan Prodromou's avatar
Evan Prodromou committed
40 41

/* Update the languages */
42
// Language code conversion for translatewiki.net (these are MediaWiki codes)
43 44 45 46 47 48
$codeMap = array(
	'nb'    => 'no',
	'pt_BR' => 'pt-br',
	'zh_CN' => 'zh-hans',
	'zh_TW' => 'zh-hant'
);
Evan Prodromou's avatar
Evan Prodromou committed
49

Siebrand Mazeland's avatar
Siebrand Mazeland committed
50 51
$doneCodes = array();

52
foreach ($languages as $language) {
53
	$code = $language['lang'];
54

55
	// Skip export of source language
56
	// and duplicates
Siebrand Mazeland's avatar
Siebrand Mazeland committed
57
	if( $code == 'en' || $code == 'no' ) {
58 59
		continue;
	}
60

Siebrand Mazeland's avatar
Siebrand Mazeland committed
61 62 63 64 65 66 67
	// Do not export codes twice (happens for 'nb')
	if( in_array( $code, $doneCodes ) ) {
		continue;
	} else {
		$doneCodes[] = $code;
	}

68 69
	// Convert code if needed
	if( isset( $codeMap[$code] ) ) {
70 71 72
		$twnCode = $codeMap[$code];
	} else {
		$twnCode = str_replace('_', '-', strtolower($code)); // pt_BR -> pt-br
73 74 75
	}

    // Fetch updates from translatewiki.net...
76 77 78 79
    $file_url = 'http://translatewiki.net/w/i.php?' .
        http_build_query(array(
            'title' => 'Special:Translate',
            'task' => 'export-to-file',
80
            'group' => 'out-statusnet-core',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
81
            'language' => $twnCode));
82

83 84
    $lcdir = INSTALLDIR . '/locale/' . $code;
    $msgdir = "$lcdir/LC_MESSAGES";
85 86
    $pofile = "$msgdir/statusnet.po";
    $mofile = "$msgdir/statusnet.mo";
87

88 89 90 91 92 93 94 95
    /* Check for an existing */
    if (!is_dir($msgdir)) {
        mkdir($lcdir);
        mkdir($msgdir);
        $existingSHA1 = '';
    } else {
        $existingSHA1 = file_exists($pofile) ? sha1_file($pofile) : '';
    }
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 103 104 105 106
        continue;
    }

    // Update if the local .po file is different to the one downloaded, or
    // 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 111
        // --backup=off is workaround for Mac OS X fail
        system(sprintf('msgmerge -U --backup=off %s %s', $pofile, $statusnet_pot));
112 113
        /* Do not rebuild/add .mo files by default
         * FIXME: should be made a command line parameter.
114
        system(sprintf('msgfmt -o %s %s', $mofile, $pofile));
115
         */
116 117 118
    } else {
        echo "Unchanged - ".$code."\n";
    }
119
}
120

121
echo "Finished\n";
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137


function curl_get_file($url)
{
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $url);
    $contents = curl_exec($c);
    curl_close($c);

    if (!empty($contents)) {
        return $contents;
    }

    return FALSE;
}