From f9601d45f397ba5d2f360020285271898039936e Mon Sep 17 00:00:00 2001 From: christianbeeznst Date: Thu, 13 Jun 2024 12:46:40 -0500 Subject: [PATCH] Internal: Fix language variable replacement script for Twig and PHP files - refs BT#21777 --- public/main/inc/lib/fileManage.lib.php | 11 +- tests/scripts/switch_files_to_gettext.php | 126 ++++++++++++++-------- 2 files changed, 88 insertions(+), 49 deletions(-) diff --git a/public/main/inc/lib/fileManage.lib.php b/public/main/inc/lib/fileManage.lib.php index 9fd3819180b..8fbb1677a85 100644 --- a/public/main/inc/lib/fileManage.lib.php +++ b/public/main/inc/lib/fileManage.lib.php @@ -89,22 +89,21 @@ function getAllPhpFiles(string $base_path, bool $includeStatic = false): array if ('.' == substr($item, 0, 1)) { continue; } - $special_dirs = [api_get_path(SYS_TEST_PATH), api_get_path(SYS_COURSE_PATH), api_get_path(SYS_LANG_PATH), api_get_path(SYS_ARCHIVE_PATH)]; - if (in_array($base_path.$item.'/', $special_dirs)) { + $special_dirs = []; // Modify this array as needed + if (in_array($base_path . $item . '/', $special_dirs)) { continue; } - if (is_dir($base_path.$item)) { - $files = array_merge($files, getAllPhpFiles($base_path.$item.'/', $includeStatic)); + if (is_dir($base_path . $item)) { + $files = array_merge($files, getAllPhpFiles($base_path . $item . '/', $includeStatic)); } else { foreach ($extensionsArray as $extension) { if (substr($item, -strlen($extension)) == $extension) { - $files[] = $base_path.$item; + $files[] = $base_path . $item; break; } } } } - $list = null; return $files; } diff --git a/tests/scripts/switch_files_to_gettext.php b/tests/scripts/switch_files_to_gettext.php index f80bc2f5077..6289d3fb8b3 100644 --- a/tests/scripts/switch_files_to_gettext.php +++ b/tests/scripts/switch_files_to_gettext.php @@ -1,90 +1,130 @@ $translation) { $terms[$index] = trim(rtrim($translation, ';'), '"'); } -// get only the array keys (the language variables defined in language files) + +// 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; +echo count($defined_terms) . " terms were found in language files" . PHP_EOL; +//print_r($defined_terms); // Debug: print the terms found -// now get all terms found in all PHP files of Chamilo (this takes some -// time and memory) +// Now get all terms found in all PHP, TPL, and Twig files of Chamilo (this takes some time and memory) $usedTerms = []; $l = strlen(api_get_path(SYS_PATH)); -$pathfile = api_get_path(SYS_PATH)."main/template/default/gamification/"; -$files = getAllPhpFiles($pathfile); +$pathfile = api_get_path(SYS_PATH) . "main/template/default/gamification/my_progress.html.twig"; +$files = [$pathfile]; // Process only the specific file for now $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; + echo "Analyzing $file" . PHP_EOL; $lines = file($file); + $newContent = ''; // Store new file content + $fileModified = false; + // Browse lines inside file $file - foreach ($lines as $line) { - $myTerms = []; - $res = preg_match_all('/get_lang\(([\'"](\\w*)[\'"])\)/m', $line, $myTerms); + foreach ($lines as $lineIndex => $line) { + $lineModified = false; + + // Regular expression for {{ 'variable'|get_lang|format() }} + $res = preg_match_all('/\{\{\s*([\'"]\w+[\'"])\s*\|\s*get_lang\s*\|\s*format\s*\((.*?)\)\s*\}\}/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); + echo "Match found for get_lang|format in line: $line" . PHP_EOL; + foreach ($myTerms[1] as $index => $quotedTerm) { + $term = trim($quotedTerm, '\'\"'); + if (isset($terms[$term])) { + $translation = $terms[$term]; + echo "Replacing $quotedTerm with '$translation'" . PHP_EOL; + $line = str_replace($quotedTerm, "'$translation'", $line); + $lineModified = true; + $countReplaces++; + } else { + echo "Term $term not found in language file" . PHP_EOL; // Debug: term not found } - if (!empty($terms[$term])) { + } + } + + // Regular expression for {{ 'variable'|get_lang }} + $res = preg_match_all('/\{\{\s*([\'"]\w+[\'"])\s*\|\s*get_lang\s*\}\}/m', $line, $myTerms); + if ($res > 0) { + echo "Match found for get_lang in line: $line" . PHP_EOL; + foreach ($myTerms[1] as $index => $quotedTerm) { + $term = trim($quotedTerm, '\'\"'); + if (isset($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#$quotedTerm#'$translation'#g\" $file"); + echo "Replacing $quotedTerm with '$translation'" . PHP_EOL; + $line = str_replace($quotedTerm, "'$translation'", $line); + $lineModified = true; $countReplaces++; + } else { + echo "Term $term not found in language file" . PHP_EOL; // Debug: term not found } } - } else { - $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#$quotedTerm#'$translation'#g\" $file"); - $countReplaces++; - } + } + + // Regular expression for get_lang('variable') or get_lang("variable") + $res = preg_match_all('/get_lang\(([\'"](\w+)[\'"])\)/m', $line, $myTerms); + if ($res > 0) { + echo "Match found for get_lang() in line: $line" . PHP_EOL; + foreach ($myTerms[2] as $index => $term) { + if (isset($terms[$term])) { + $translation = $terms[$term]; + $quotedTerm = $myTerms[1][$index]; + echo "Replacing $quotedTerm with '$translation'" . PHP_EOL; + $line = str_replace($quotedTerm, "'$translation'", $line); + $lineModified = true; + $countReplaces++; + } else { + echo "Term $term not found in language file" . PHP_EOL; // Debug: term not found } } } + + $newContent .= $line; // Add modified line to new content + if ($lineModified) { + $fileModified = true; + } } + + // Write the modified content back to the file if there were modifications + if ($fileModified) { + file_put_contents($file, $newContent); + } + $countFiles++; - flush(); } echo "Done analyzing $countFiles files, with $countReplaces replacements!\n";