From c1162f9fa8c5bb736f03a59d1c34a62278693573 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Tue, 6 Nov 2018 15:23:13 -0500 Subject: [PATCH] Move gettext switching script to a more logical directory --- .../scripts/lang/switch_files_to_gettext.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/scripts/lang/switch_files_to_gettext.php diff --git a/tests/scripts/lang/switch_files_to_gettext.php b/tests/scripts/lang/switch_files_to_gettext.php new file mode 100644 index 0000000000..690f404b89 --- /dev/null +++ b/tests/scripts/lang/switch_files_to_gettext.php @@ -0,0 +1,92 @@ + $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 (substr($file, $rootLength, 6) === 'vendor' || substr($file, $rootLength, 3) === 'web') { + 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 (substr($term, 0, 4) == 'lang') { + $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 (substr($term, 0, 4) == 'lang') { + $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";