update_translations.php 3.9 KB

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