migration.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. echo "This script is used for net2ftp development... nothing to see here! ;-)";
  3. exit();
  4. // -------------------------------------------------------------------------
  5. // Settings
  6. // -------------------------------------------------------------------------
  7. $languagesArray[] = "ar";
  8. $languagesArray[] = "ar-utf";
  9. $languagesArray[] = "cs";
  10. $languagesArray[] = "da-utf";
  11. $languagesArray[] = "de";
  12. $languagesArray[] = "en";
  13. $languagesArray[] = "en-utf";
  14. $languagesArray[] = "es";
  15. $languagesArray[] = "fr";
  16. $languagesArray[] = "fi";
  17. $languagesArray[] = "he-utf";
  18. $languagesArray[] = "it";
  19. $languagesArray[] = "ja";
  20. $languagesArray[] = "nl";
  21. $languagesArray[] = "pl";
  22. $languagesArray[] = "pt";
  23. $languagesArray[] = "ru";
  24. $languagesArray[] = "sv";
  25. $languagesArray[] = "tc";
  26. $languagesArray[] = "tr";
  27. $languagesArray[] = "ua";
  28. $languagesArray[] = "vi";
  29. $languagesArray[] = "zh";
  30. $directory_old = "";
  31. $directory_new = "../languages-new";
  32. $extension = ".inc.php";
  33. // -------------------------------------------------------------------------
  34. // LOOP: for all language files
  35. // -------------------------------------------------------------------------
  36. for ($i=0; $i<sizeof($languagesArray); $i++) {
  37. // ------------------------------------
  38. // Send status
  39. // ------------------------------------
  40. echo "Language $i - " . $languagesArray[$i] . "<br />\n";
  41. flush();
  42. // ------------------------------------
  43. // Read the English file and get the lines
  44. // ------------------------------------
  45. $en_string = local_readfile($directory_old . "en" . $extension);
  46. $en_lines = explode_lines($en_string);
  47. // ------------------------------------
  48. // Read the old translated file
  49. // ------------------------------------
  50. $translated_old_string = local_readfile($directory_old . $languagesArray[$i] . $extension);
  51. $translated_old_lines = explode_lines($translated_old_string);
  52. // $en_lines contains:
  53. // [694] => $net2ftp_messages["Zip"] = "Zip";
  54. // [695] => $net2ftp_messages["Size"] = "Size";
  55. // [696] => $net2ftp_messages["Search"] = "Search";
  56. // ------------------------------------
  57. // Initialize the translated string
  58. // ------------------------------------
  59. $translated_new_string = "";
  60. $translated_new_lines = array();
  61. // -------------------------------------------------------------------------
  62. // SUBLOOP: for all lines
  63. // -------------------------------------------------------------------------
  64. for ($j=1; $j<sizeof($en_lines); $j++) {
  65. // ------------------------------------
  66. // Send status
  67. // ------------------------------------
  68. if ($j % 20 == 0) {
  69. echo "Language $i line $j <br />\n";
  70. flush();
  71. }
  72. // ------------------------------------
  73. // Real messages
  74. // ------------------------------------
  75. if (substr($en_lines[$j], 0, 1) == "$") {
  76. // Find the position of the first = character
  77. $equalsign_position = strpos($en_lines[$j], "] =");
  78. if ($equalsign_position === false || $equalsign_position == false) {
  79. echo "Language $i, line $j: Equal sign not found or at first position. String is " . $en_lines[$j] . ". Continuing.<br />\n";
  80. $translated_new_lines[$j] = $en_lines[$j];
  81. continue;
  82. }
  83. // Add 3 because we looked for "] =" instead of "="
  84. else {
  85. $equalsign_position = $equalsign_position + 3;
  86. }
  87. // Get the message "$n2f["abd"] ="
  88. $message = substr($en_lines[$j], 0, $equalsign_position);
  89. // Search for the translation
  90. $translation = search_translation($message, $translated_old_lines);
  91. // Debugging info
  92. // echo "Language $i, line $j: message is $message, translation is $translation. <br />\n";
  93. // If the translation is not found, use the English message
  94. if ($translation == false) {
  95. $translated_new_lines[$j] = $en_lines[$j];
  96. }
  97. // If the translation is found
  98. else {
  99. $translated_new_lines[$j] = "$message $translation";
  100. }
  101. } // end if
  102. // ------------------------------------
  103. // Comments and control structures
  104. // ------------------------------------
  105. else {
  106. $translated_new_lines[$j] = $en_lines[$j];
  107. } // end else
  108. } // end for
  109. // ------------------------------------
  110. // Write the new translated string to a file
  111. // ------------------------------------
  112. //print_r($en_lines);
  113. //print_r($translated_new_lines);
  114. // Glue the array to a string
  115. $translated_new_string = implode("\n", $translated_new_lines);
  116. // Write the string to a file
  117. local_writefile($directory_new . "/" . $languagesArray[$i] . $extension, $translated_new_string);
  118. } // end for
  119. // -------------------------------------------------------------------------
  120. // Done
  121. // -------------------------------------------------------------------------
  122. echo "Done. <br />\n";
  123. // **************************************************************************************
  124. // **************************************************************************************
  125. // ** **
  126. // ** **
  127. function search_translation($message, $translated_old_lines) {
  128. // --------------
  129. // Search an array for a message
  130. // --------------
  131. // Go over all the lines of the old translated file
  132. for ($k=0; $k<sizeof($translated_old_lines); $k++) {
  133. if (substr($translated_old_lines[$k], 0, strlen($message)) == $message) {
  134. $toreturn = substr($translated_old_lines[$k], strlen($message)+1);
  135. return $toreturn;
  136. }
  137. }
  138. // If nothing is found, return false
  139. return false;
  140. } // end search_translation
  141. // ** **
  142. // ** **
  143. // **************************************************************************************
  144. // **************************************************************************************
  145. // **************************************************************************************
  146. // **************************************************************************************
  147. // ** **
  148. // ** **
  149. function local_readfile($file) {
  150. // --------------
  151. // Open the local file $file and return its content as a string
  152. // --------------
  153. $handle = fopen($file, "rb"); // Open the file for reading only
  154. if ($handle == false) { echo "Unable to execute fopen() in local_readfile. Exiting.<br />\n"; exit(); }
  155. clearstatcache(); // for filesize
  156. $filesize = filesize($file);
  157. if ($filesize == 0) { return ""; }
  158. $string = fread($handle, $filesize);
  159. if ($string == false && filesize($file)>0) { echo "Unable to execute fread() in local_readfile. Exiting.<br />\n"; exit(); }
  160. $success3 = fclose($handle);
  161. if ($success3 == false) { echo "Unable to execute fclose() in local_readfile. Exiting.<br />\n"; exit(); }
  162. return $string;
  163. } // end local_readfile
  164. // ** **
  165. // ** **
  166. // **************************************************************************************
  167. // **************************************************************************************
  168. // **************************************************************************************
  169. // **************************************************************************************
  170. // ** **
  171. // ** **
  172. function local_writefile($file, $string) {
  173. // --------------
  174. // Open the local file $file and write the $string to it
  175. // --------------
  176. $handle = fopen($file, "wb");
  177. if ($handle == false) { echo "Unable to execute fopen() in local_writefile. Exiting.<br />\n"; exit(); }
  178. $success1 = fwrite($handle, $string);
  179. if ($success1 == false && strlen($string)>0) { echo "Unable to execute fwrite() in local_writefile. Exiting.<br />\n"; exit(); }
  180. $success2 = fclose($handle);
  181. if ($success2 == false) { echo "Unable to execute fclose() in local_writefile. Exiting.<br />\n"; exit(); }
  182. } // end local_writefile
  183. // ** **
  184. // ** **
  185. // **************************************************************************************
  186. // **************************************************************************************
  187. // **************************************************************************************
  188. // **************************************************************************************
  189. // ** **
  190. // ** **
  191. function explode_lines($string) {
  192. // --------------
  193. // Input: $string which may have Windows or Unix end-of-line characters
  194. // Output: $lines array with the lines
  195. // --------------
  196. // $string = standardize_eol($string);
  197. // Add a \n in the beginning of the strings so that the first line of the string would
  198. // be in the first element of the exploded array
  199. $lines = explode("\n", "\n" . $string);
  200. return $lines;
  201. } // explode_lines
  202. // ** **
  203. // ** **
  204. // **************************************************************************************
  205. // **************************************************************************************
  206. ?>