PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/Z_poEditLangModule.php

http://chronique.googlecode.com/
PHP | 242 lines | 155 code | 59 blank | 28 comment | 30 complexity | acf131986f346647ce1bcb139626aa95 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* $Id: Z_poEditLangModule.php 4734 2011-10-29 03:26:27Z daintree $ */
  3. /* Steve Kitchen */
  4. /* This code is really ugly ... */
  5. //$PageSecurity = 15;
  6. include ('includes/session.inc');
  7. $title = _('Edit Module');
  8. include('includes/header.inc');
  9. /* Your webserver user MUST have read/write access to here,
  10. otherwise you'll be wasting your time */
  11. $PathToLanguage = './locale/' . $_SESSION['Language'] . '/LC_MESSAGES/messages.po';
  12. $PathToNewLanguage = './locale/' . $_SESSION['Language'] . '/LC_MESSAGES/messages.po.new';
  13. echo "<br />&nbsp;<a href='" . $rootpath . "/Z_poAdmin.php'>" . _('Back to the translation menu') . "</a>";
  14. echo '<br /><br />&nbsp;' . _('Utility to edit a language file module');
  15. echo '<br />&nbsp;' . _('Current language is') . ' ' . $_SESSION['Language'];
  16. echo '<br /><br />&nbsp;' . _('To change language click on the user name at the top left, change to language desired and click Modify');
  17. echo '<br />&nbsp;' . _('Make sure you have selected the correct language to translate!');
  18. if (isset($_POST['ReMergePO'])){
  19. /*update the messages.po file with any new strings */
  20. /*first rebuild the en_GB default with xgettext */
  21. $PathToDefault = './locale/en_GB.utf8/LC_MESSAGES/messages.po';
  22. $FilesToInclude = '*php includes/*.php includes/*.inc';
  23. $xgettextCmd = 'xgettext --no-wrap -L php -o ' . $PathToDefault . ' ' . $FilesToInclude;
  24. system($xgettextCmd);
  25. /*now merge the translated file with the new template to get new strings*/
  26. $msgMergeCmd = 'msgmerge --no-wrap --update ' . $PathToLanguage . ' ' . $PathToDefault;
  27. system($msgMergeCmd);
  28. //$Result = rename($PathToNewLanguage, $PathToLanguage);
  29. exit;
  30. }
  31. if (isset($_POST['module'])) {
  32. // a module has been selected and is being modified
  33. $PathToLanguage_mo = mb_substr($PathToLanguage,0,strrpos($PathToLanguage,'.')) . '.mo';
  34. /* now read in the language file */
  35. $LangFile = file($PathToLanguage);
  36. $LangFileEntries = sizeof($LangFile);
  37. if (isset($_POST['submit'])) {
  38. // save the modifications
  39. echo '<br /><table><tr><td>';
  40. echo '<form method="post" action=' . htmlspecialchars($_SERVER['PHP_SELF']) . '?' . SID . '>';
  41. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  42. /* write the new language file */
  43. prnMsg (_('Writing the language file') . '.....<br />', 'info', ' ');
  44. for ($i=17; $i<=$LangFileEntries; $i++) {
  45. if (isset($_POST['msgstr_'.$i])) {
  46. $LangFile[$i] = 'msgstr "' . $_POST['moduletext_'.$i] . '"' . "\n";
  47. }
  48. }
  49. $fpOut = fopen($PathToNewLanguage, 'w');
  50. for ($i=0; $i<=$LangFileEntries; $i++) {
  51. $Result = fputs($fpOut, $LangFile[$i]);
  52. }
  53. $Result = fclose($fpOut);
  54. /* Done writing, now move the original file to a .old */
  55. /* and the new one to the default */
  56. if (file_exists($PathToLanguage . '.old')) {
  57. $Result = rename($PathToLanguage . '.old', $PathToLanguage . '.bak');
  58. }
  59. $Result = rename($PathToLanguage, $PathToLanguage . '.old');
  60. $Result = rename($PathToNewLanguage, $PathToLanguage);
  61. if (file_exists($PathToLanguage . '.bak')) {
  62. $Result = unlink($PathToLanguage . '.bak');
  63. }
  64. /*now need to create the .mo file from the .po file */
  65. $msgfmtCommand = 'msgfmt ' . $PathToLanguage . ' -o ' . $PathToLanguage_mo;
  66. system($msgfmtCommand);
  67. prnMsg (_('Done') . '<br />', 'info', ' ');
  68. echo '</form>';
  69. echo '</td></tr></table>';
  70. /* End of Submit block */
  71. } else {
  72. /* now we need to parse the resulting array into something we can show the user */
  73. $j = 1;
  74. for ($i=17; $i<=$LangFileEntries; $i++) { /* start at line 18 to skip the header */
  75. if (mb_substr($LangFile[$i], 0, 2) == '#:') { /* it's a module reference */
  76. $AlsoIn[$j] .= str_replace(' ','<br />', mb_substr($LangFile[$i],3)) . '<br />';
  77. } elseif (mb_substr($LangFile[$i], 0 , 5) == 'msgid') {
  78. $DefaultText[$j] = mb_substr($LangFile[$i], 7, mb_strlen($LangFile[$i])-9);
  79. } elseif (mb_substr($LangFile[$i], 0 , 6) == 'msgstr') {
  80. $ModuleText[$j] = mb_substr($LangFile[$i], 8, mb_strlen($LangFile[$i])-10);
  81. $msgstr[$j] = $i;
  82. $j++;
  83. }
  84. }
  85. $TotalLines = $j - 1;
  86. /* stick it on the screen */
  87. echo '<br />&nbsp;' . _('When finished modifying you must click on Modify at the bottom in order to save changes');
  88. echo '<div class="centre">';
  89. echo '<br />';
  90. prnMsg (_('Your existing translation file (messages.po) will be saved as messages.po.old') . '<br />', 'info', _('PLEASE NOTE'));
  91. echo '<br />';
  92. echo '<form method="post" action=' . htmlspecialchars($_SERVER['PHP_SELF']) . '?' . SID . '>';
  93. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  94. echo '</div';
  95. echo '<table>';
  96. echo '<tr><th ALIGN="center">' . _('Language File for') . ' "' . $_POST['language'] . '"</th></tr>';
  97. echo '<tr><td ALIGN="center">' . _('Module') . ' "' . $_POST['module'] . '"</td></tr>';
  98. echo '<tr><td></td></tr>';
  99. echo '<tr><td>';
  100. echo '<table WIDTH="100%">';
  101. echo '<tr>';
  102. echo '<th>' . _('Default text') . '</th>';
  103. echo '<th>' . _('Translation') . '</th>';
  104. echo '<th>' . _('Exists in') . '</th>';
  105. echo '</tr>' . "\n";
  106. for ($i=1; $i<=$TotalLines; $i++) {
  107. $b = mb_strpos($AlsoIn[$i], $_POST['module']);
  108. if ($b === False) {
  109. /* skip it */
  110. } else {
  111. echo '<tr>';
  112. echo '<td VALIGN="top"><I>' . $DefaultText[$i] . '</I></td>';
  113. echo '<td VALIGN="top"><input type="text" size="60" name="moduletext_' . $msgstr[$i] . '" VALUE="' . $ModuleText[$i] . '"></td>';
  114. echo '<td VALIGN="top">' . $AlsoIn[$i] . '<input type="hidden" name="msgstr_' . $msgstr[$i] . '" VALUE="' . $msgstr[$i] . '"></td>';
  115. echo '</tr>';
  116. echo '<tr><th colspan="3"></th></tr>';
  117. }
  118. }
  119. echo '</table>';
  120. echo '</td></tr>';
  121. echo '</table>';
  122. echo '<br /><div class="centre">';
  123. echo '<input type="Submit" name="submit" VALUE="' . _('Modify') . '">&nbsp;&nbsp;';
  124. echo '<input type="hidden" name="module" VALUE="' . $_POST['module'] . '">';
  125. echo '</form>';
  126. echo '</div>';
  127. }
  128. } else {
  129. /* get available modules */
  130. /* This is a messy way of producing a directory listing of ./locale to fish out */
  131. /* the language directories that have been set up */
  132. /* The other option would be to define an array of the languages you want */
  133. /* and check for the existance of the directory */
  134. /* $ListDirCmd should probably be defined in config.php as a global value */
  135. /* You'll need to change it if you are running a Windows server - sorry !! */
  136. if ($handle = opendir('.')) {
  137. $i=0;
  138. while (false !== ($file = readdir($handle))) {
  139. if ((mb_substr($file, 0, 1) != ".") && (!is_dir($file))) {
  140. $AvailableModules[$i] = $file;
  141. $i += 1;
  142. }
  143. }
  144. closedir($handle);
  145. }
  146. if ($handle = opendir(".//includes")) {
  147. while (false !== ($file = readdir($handle))) {
  148. if ((mb_substr($file, 0, 1) != ".") && (!is_dir($file))) {
  149. $AvailableModules[$i] = $file;
  150. $i += 1;
  151. }
  152. }
  153. closedir($handle);
  154. }
  155. sort($AvailableModules);
  156. $NumberOfModules = sizeof($AvailableModules) - 1;
  157. if (!is_writable('./locale/' . $_SESSION['Language'])) {
  158. prnMsg(_('You do not have write access to the required files please contact your system administrator'),'error');
  159. }
  160. else
  161. {
  162. echo '<br /><table><tr><td>';
  163. echo '<form method="post" action=' . htmlspecialchars($_SERVER['PHP_SELF']) . '?' . SID . '>';
  164. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  165. echo '<table>';
  166. echo '<tr><td>' . _('Select the module to edit') . '</td>';
  167. echo '<td><select name="module">';
  168. for ($i=0; $i<$NumberOfModules; $i++) {
  169. echo '<option>' . $AvailableModules[$i] . '</option>';
  170. }
  171. echo '</select></td>';
  172. echo '</tr></table>';
  173. echo '<br />';
  174. echo '<div class="centre">';
  175. echo '<input type="Submit" name="proceed" VALUE="' . _('Proceed') . '">&nbsp;&nbsp;';
  176. echo '<br /><br /><input type="Submit" name="ReMergePO" VALUE="' . _('Refresh messages with latest strings') . '">';
  177. echo '</div>';
  178. echo '</form>';
  179. echo '</td></tr></table>';
  180. }
  181. }
  182. include('includes/footer.inc');
  183. ?>