From 812ded7d63cf348c7cd6f1d419bddbf412fdc78f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 6 Jul 2021 11:29:33 +0200 Subject: [PATCH] Restore switch_files_to_gettext.php https://github.com/chamilo/chamilo-lms/commit/f199ca4a587d7eb67e2d40ae4d93596f63ba23ef --- tests/scripts/switch_files_to_gettext.php | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/scripts/switch_files_to_gettext.php diff --git a/tests/scripts/switch_files_to_gettext.php b/tests/scripts/switch_files_to_gettext.php new file mode 100644 index 0000000000..20a7ec8dc0 --- /dev/null +++ b/tests/scripts/switch_files_to_gettext.php @@ -0,0 +1,90 @@ + $translation) { + $terms[$index] = trim(rtrim($translation, ';'), '"'); +} +// get only the array keys (the language variables defined in language files) +$defined_terms = array_flip(array_keys($terms)); +echo count($defined_terms)." terms were found in language files".PHP_EOL; + +// now get all terms found in all PHP files of Chamilo (this takes some +// time and memory) +$usedTerms = []; +$l = strlen(api_get_path(SYS_PATH)); +$files = getAllPhpFiles(api_get_path(SYS_PATH)); +$rootLength = strlen(api_get_path(SYS_PATH)); +$countFiles = 0; +$countReplaces = 0; +// Browse files +foreach ($files as $file) { + if ('vendor' === substr($file, $rootLength, 6) || 'web' === substr($file, $rootLength, 3)) { + continue; + } + //echo 'Analyzing '.$file.PHP_EOL; + $shortFile = substr($file, $l); + //echo 'Analyzing '.$shortFile.PHP_EOL; + $lines = file($file); + // Browse lines inside file $file + foreach ($lines as $line) { + $myTerms = []; + $res = preg_match_all('/get_lang\(([\'"](\\w*)[\'"])\)/m', $line, $myTerms); + if ($res > 0) { + foreach ($myTerms[2] as $term) { + echo "Found term $term - ".print_r($myTerms, 1).PHP_EOL; + if ('lang' == substr($term, 0, 4)) { + $term = substr($term, 4); + } + if (!empty($terms[$term])) { + $translation = $terms[$term]; + $quotedTerm = $myTerms[1][0]; + //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n"; + system("sed -i \"s#$term#'$translation'#g\" $file"); + $countReplaces++; + } + } + } else { + $res = 0; + $res = preg_match_all('/\{\s*([\'"](\\w*)[\'"])\s*\|get_lang\}/m', $line, $myTerms); + if ($res > 0) { + foreach ($myTerms[2] as $term) { + echo "Found term $term".PHP_EOL; + if ('lang' == substr($term, 0, 4)) { + $term = substr($term, 4); + } + if (!empty($terms[$term])) { + $translation = $terms[$term]; + $quotedTerm = $myTerms[1][0]; + //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n"; + system("sed -i \"s#$term#'$translation'#g\" $file"); + $countReplaces++; + } + } + } + } + } + $countFiles++; + flush(); +} + +echo "Done analyzing $countFiles files, with $countReplaces replacements!\n"; \ No newline at end of file