Internal: Add .html.twig support to gettext migration script - refs BT#21777

pull/5586/head
christianbeeznst 1 year ago
parent f7af3b83aa
commit 8ff5955d32
  1. 18
      public/main/inc/lib/fileManage.lib.php
  2. 10
      tests/scripts/switch_files_to_gettext.php

@ -74,17 +74,12 @@ function copyDirTo($source, $destination, $move = true)
/** /**
* Get a list of all PHP (.php) files in a given directory. Includes .tpl files. * Get a list of all PHP (.php) files in a given directory. Includes .tpl files.
*
* @param string $base_path The base path in which to find the corresponding files
* @param bool $includeStatic Include static .html, .htm and .css files
*
* @return array
*/ */
function getAllPhpFiles($base_path, $includeStatic = false) function getAllPhpFiles(string $base_path, bool $includeStatic = false): array
{ {
$list = scandir($base_path); $list = scandir($base_path);
$files = []; $files = [];
$extensionsArray = ['.php', '.tpl']; $extensionsArray = ['.php', '.tpl', '.html.twig'];
if ($includeStatic) { if ($includeStatic) {
$extensionsArray[] = 'html'; $extensionsArray[] = 'html';
$extensionsArray[] = '.htm'; $extensionsArray[] = '.htm';
@ -101,10 +96,11 @@ function getAllPhpFiles($base_path, $includeStatic = false)
if (is_dir($base_path.$item)) { if (is_dir($base_path.$item)) {
$files = array_merge($files, getAllPhpFiles($base_path.$item.'/', $includeStatic)); $files = array_merge($files, getAllPhpFiles($base_path.$item.'/', $includeStatic));
} else { } else {
//only analyse php files foreach ($extensionsArray as $extension) {
$sub = substr($item, -4); if (substr($item, -strlen($extension)) == $extension) {
if (in_array($sub, $extensionsArray)) { $files[] = $base_path.$item;
$files[] = $base_path.$item; break;
}
} }
} }
} }

@ -32,7 +32,8 @@ echo count($defined_terms)." terms were found in language files".PHP_EOL;
// time and memory) // time and memory)
$usedTerms = []; $usedTerms = [];
$l = strlen(api_get_path(SYS_PATH)); $l = strlen(api_get_path(SYS_PATH));
$files = getAllPhpFiles(api_get_path(SYS_PATH)); $pathfile = api_get_path(SYS_PATH)."main/template/default/gamification/";
$files = getAllPhpFiles($pathfile);
$rootLength = strlen(api_get_path(SYS_PATH)); $rootLength = strlen(api_get_path(SYS_PATH));
$countFiles = 0; $countFiles = 0;
$countReplaces = 0; $countReplaces = 0;
@ -59,13 +60,12 @@ foreach ($files as $file) {
$translation = $terms[$term]; $translation = $terms[$term];
$quotedTerm = $myTerms[1][0]; $quotedTerm = $myTerms[1][0];
//echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n"; //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
system("sed -i \"s#$term#'$translation'#g\" $file"); system("sed -i \"s#$quotedTerm#'$translation'#g\" $file");
$countReplaces++; $countReplaces++;
} }
} }
} else { } else {
$res = 0; $res = preg_match_all('/\{\{\s*([\'"](\\w*)[\'"])\s*\|get_lang\}\}/m', $line, $myTerms);
$res = preg_match_all('/\{\s*([\'"](\\w*)[\'"])\s*\|get_lang\}/m', $line, $myTerms);
if ($res > 0) { if ($res > 0) {
foreach ($myTerms[2] as $term) { foreach ($myTerms[2] as $term) {
echo "Found term $term".PHP_EOL; echo "Found term $term".PHP_EOL;
@ -76,7 +76,7 @@ foreach ($files as $file) {
$translation = $terms[$term]; $translation = $terms[$term];
$quotedTerm = $myTerms[1][0]; $quotedTerm = $myTerms[1][0];
//echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n"; //echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
system("sed -i \"s#$term#'$translation'#g\" $file"); system("sed -i \"s#$quotedTerm#'$translation'#g\" $file");
$countReplaces++; $countReplaces++;
} }
} }

Loading…
Cancel
Save