switch_files_to_gettext.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to switch all PHP files in Chamilo to a more Gettext-like syntax.
  5. *
  6. */
  7. die();
  8. require_once __DIR__.'/../../../main/inc/global.inc.php';
  9. $path = api_get_path(SYS_LANG_PATH).'english';
  10. ini_set('memory_limit', '128M');
  11. /**
  12. * Main code.
  13. */
  14. $terms = [];
  15. $list = SubLanguageManager::get_lang_folder_files_list($path);
  16. foreach ($list as $entry) {
  17. $file = $path.'/'.$entry;
  18. if (is_file($file)) {
  19. $terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
  20. }
  21. }
  22. foreach ($terms as $index => $translation) {
  23. $terms[$index] = trim(rtrim($translation, ';'), '"');
  24. }
  25. // get only the array keys (the language variables defined in language files)
  26. $defined_terms = array_flip(array_keys($terms));
  27. echo count($defined_terms)." terms were found in language files".PHP_EOL;
  28. // now get all terms found in all PHP files of Chamilo (this takes some
  29. // time and memory)
  30. $usedTerms = [];
  31. $l = strlen(api_get_path(SYS_PATH));
  32. $files = getAllPhpFiles(api_get_path(SYS_PATH));
  33. $rootLength = strlen(api_get_path(SYS_PATH));
  34. $countFiles = 0;
  35. $countReplaces = 0;
  36. // Browse files
  37. foreach ($files as $file) {
  38. if (substr($file, $rootLength, 6) === 'vendor' || substr($file, $rootLength, 3) === 'web') {
  39. continue;
  40. }
  41. //echo 'Analyzing '.$file.PHP_EOL;
  42. $shortFile = substr($file, $l);
  43. //echo 'Analyzing '.$shortFile.PHP_EOL;
  44. $lines = file($file);
  45. // Browse lines inside file $file
  46. foreach ($lines as $line) {
  47. $myTerms = [];
  48. $res = preg_match_all('/get_lang\(([\'"](\\w*)[\'"])\)/m', $line, $myTerms);
  49. if ($res > 0) {
  50. foreach ($myTerms[2] as $term) {
  51. echo "Found term $term - ".print_r($myTerms, 1).PHP_EOL;
  52. if (substr($term, 0, 4) == 'lang') {
  53. $term = substr($term, 4);
  54. }
  55. if (!empty($terms[$term])) {
  56. $translation = $terms[$term];
  57. $quotedTerm = $myTerms[1][0];
  58. //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
  59. //system("sed -i \"s#$term#'$translation'#g\" $file");
  60. system("sed -i \"s#$term#$translation#g\" $file");
  61. $countReplaces++;
  62. }
  63. }
  64. } else {
  65. $res = preg_match_all('/\{\s*([\'"](\\w*)[\'"])\s*\|get_lang\}/m', $line, $myTerms);
  66. if ($res > 0) {
  67. foreach ($myTerms[2] as $term) {
  68. echo "Found term $term".PHP_EOL;
  69. if (substr($term, 0, 4) == 'lang') {
  70. $term = substr($term, 4);
  71. }
  72. if (!empty($terms[$term])) {
  73. $translation = $terms[$term];
  74. $quotedTerm = $myTerms[1][0];
  75. //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
  76. system("sed -i \"s#$term#'$translation'#g\" $file");
  77. $countReplaces++;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. $countFiles++;
  84. flush();
  85. }
  86. echo "Done analyzing $countFiles files, with $countReplaces replacements!\n";