update_translations.php 3.8 KB

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