switch_files_to_gettext.php 3.2 KB

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