From b898c489b4a3ad935836b33a4c63370532a97076 Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Fri, 21 Jun 2024 19:35:51 +0530 Subject: [PATCH 1/3] Fixed: Error on AdminTranslationController while translating modules, also optimized the translation process for the modules --- .../admin/AdminTranslationsController.php | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index 5f275f62d..c448ba1a7 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -954,7 +954,7 @@ class AdminTranslationsControllerCore extends AdminController * * @throws PrestaShopException */ - protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_name, $module_name, $dir = false) + protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_name, $module_name, $dir = false, $copyExistingTranslations = false) { // These static vars allow to use file to write just one time. static $cache_file = array(); @@ -964,7 +964,12 @@ class AdminTranslationsControllerCore extends AdminController // Set file_name in static var, this allow to open and wright the file just one time if (!isset($cache_file[$theme_name.'-'.$file_name])) { - require $file_name; + if (file_exists($file_name)) { + require $file_name; + } else { + $_MODULE = array(); + } + $module_existing_translations = $_MODULE; $str_write = ''; $cache_file[$theme_name.'-'.$file_name] = true; @@ -1018,13 +1023,14 @@ class AdminTranslationsControllerCore extends AdminController $this->total_expression++; } } - foreach ($module_existing_translations as $key => $value) { - if (!in_array('\''.$key.'\'', $array_check_duplicate)) { - $str_write .= '$_MODULE[\''.$key.'\'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $value)).'\';'."\n"; + } + } - } + if ($copyExistingTranslations) { + foreach ($module_existing_translations as $key => $value) { + if (!in_array('\''.$key.'\'', $array_check_duplicate)) { + $str_write .= '$_MODULE[\''.$key.'\'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $value)).'\';'."\n"; } - } } @@ -1056,6 +1062,8 @@ class AdminTranslationsControllerCore extends AdminController unset($files[$key]); } elseif ($type_clear === 'directory' && (!is_dir($path.$file) || in_array($file, $arr_exclude))) { unset($files[$key]); + } else if ($file == 'index.php') { + unset($files[$key]); } } @@ -1468,8 +1476,15 @@ class AdminTranslationsControllerCore extends AdminController $arr_files = $this->getAllModuleFiles($modules, null, $this->lang_selected->iso_code, true); // Find and write all translation modules files - foreach ($arr_files as $value) { - $this->findAndWriteTranslationsIntoFile($value['file_name'], $value['files'], $value['theme'], $value['module'], $value['dir']); + foreach ($arr_files as $key => $value) { + $copyExistingTranslations = false; + if (!isset($arr_files[$key + 1]) + || ($arr_files[$key + 1]['module'] != $value['module']) + ) { + $copyExistingTranslations = true; + } + + $this->findAndWriteTranslationsIntoFile($value['file_name'], $value['files'], $value['theme'], $value['module'], $value['dir'], $copyExistingTranslations); } // Clear modules cache From d02b845ccea3d337dff3c91361b5f7b304f1c26c Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Tue, 25 Jun 2024 12:33:10 +0530 Subject: [PATCH 2/3] Fixed: The Admin is unable to remove tranlsations by submitting empty field --- controllers/admin/AdminTranslationsController.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index c448ba1a7..dcc382886 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -961,6 +961,7 @@ class AdminTranslationsControllerCore extends AdminController static $str_write = ''; static $array_check_duplicate = array(); static $module_existing_translations = array(); + static $empty_translation_fields = array(); // Set file_name in static var, this allow to open and wright the file just one time if (!isset($cache_file[$theme_name.'-'.$file_name])) { @@ -1016,6 +1017,7 @@ class AdminTranslationsControllerCore extends AdminController if (array_key_exists($post_key, $_POST) && !in_array($pattern, $array_check_duplicate)) { if ($_POST[$post_key] == '') { + $empty_translation_fields[] = $pattern; continue; } $array_check_duplicate[] = $pattern; @@ -1028,13 +1030,15 @@ class AdminTranslationsControllerCore extends AdminController if ($copyExistingTranslations) { foreach ($module_existing_translations as $key => $value) { - if (!in_array('\''.$key.'\'', $array_check_duplicate)) { + if (!in_array('\''.$key.'\'', $array_check_duplicate) + && !in_array('\''.$key.'\'', $empty_translation_fields) + ) { $str_write .= '$_MODULE[\''.$key.'\'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $value)).'\';'."\n"; } } } - if (isset($cache_file[$theme_name.'-'.$file_name]) && $str_write != " Date: Tue, 23 Jul 2024 11:29:00 +0530 Subject: [PATCH 3/3] optimized code for admin translation and removed non required variables --- controllers/admin/AdminTranslationsController.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index dcc382886..8b5dcfb04 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -961,7 +961,6 @@ class AdminTranslationsControllerCore extends AdminController static $str_write = ''; static $array_check_duplicate = array(); static $module_existing_translations = array(); - static $empty_translation_fields = array(); // Set file_name in static var, this allow to open and wright the file just one time if (!isset($cache_file[$theme_name.'-'.$file_name])) { @@ -1016,11 +1015,10 @@ class AdminTranslationsControllerCore extends AdminController } if (array_key_exists($post_key, $_POST) && !in_array($pattern, $array_check_duplicate)) { + $array_check_duplicate[] = $pattern; if ($_POST[$post_key] == '') { - $empty_translation_fields[] = $pattern; continue; } - $array_check_duplicate[] = $pattern; $str_write .= '$_MODULE['.$pattern.'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $_POST[$post_key])).'\';'."\n"; $this->total_expression++; } @@ -1030,9 +1028,7 @@ class AdminTranslationsControllerCore extends AdminController if ($copyExistingTranslations) { foreach ($module_existing_translations as $key => $value) { - if (!in_array('\''.$key.'\'', $array_check_duplicate) - && !in_array('\''.$key.'\'', $empty_translation_fields) - ) { + if (!in_array('\''.$key.'\'', $array_check_duplicate)) { $str_write .= '$_MODULE[\''.$key.'\'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $value)).'\';'."\n"; } }