From a25bbb6587dd281b5ca89de7a402e7b97f174ea5 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Thu, 19 Jan 2023 18:49:16 +0530 Subject: [PATCH] updated template tranlastions method for translation in other thank context language --- classes/Translate.php | 27 +++++++++++++---- classes/order/OrderHistory.php | 2 +- config/smartyadmin.config.inc.php | 15 ++++++++-- config/smartyfront.config.inc.php | 17 +++++++++-- modules/bankwire/bankwire.php | 10 ++++--- .../en => views/templates/mail}/index.php | 0 .../templates/mail}/mail_template_html.tpl | 10 +++---- .../templates/mail}/mail_template_text.tpl | 0 modules/cheque/cheque.php | 10 ++++--- modules/cheque/mails/en/index.php | 29 ------------------- modules/cheque/mails/index.php | 29 ------------------- .../views/templates/mail}/index.php | 0 .../templates/mail}/mail_template_html.tpl | 8 ++--- .../templates/mail}/mail_template_text.tpl | 8 ++--- 14 files changed, 75 insertions(+), 90 deletions(-) rename modules/bankwire/{mails/en => views/templates/mail}/index.php (100%) rename modules/bankwire/{mails/en => views/templates/mail}/mail_template_html.tpl (80%) rename modules/bankwire/{mails/en => views/templates/mail}/mail_template_text.tpl (100%) delete mode 100644 modules/cheque/mails/en/index.php delete mode 100644 modules/cheque/mails/index.php rename modules/{bankwire/mails => cheque/views/templates/mail}/index.php (100%) rename modules/cheque/{mails/en => views/templates/mail}/mail_template_html.tpl (83%) rename modules/cheque/{mails/en => views/templates/mail}/mail_template_text.tpl (65%) diff --git a/classes/Translate.php b/classes/Translate.php index 7bb036099..810fcf0f3 100644 --- a/classes/Translate.php +++ b/classes/Translate.php @@ -126,7 +126,7 @@ class TranslateCore * @param string $source * @return string */ - public static function getModuleTranslation($module, $string, $source, $sprintf = null, $js = false) + public static function getModuleTranslation($module, $string, $source, $sprintf = null, $js = false, $language = null) { global $_MODULES, $_MODULE, $_LANGADM; @@ -135,11 +135,18 @@ class TranslateCore // $translations_merged is a cache of wether a specific module's translations have already been added to $_MODULES static $translations_merged = array(); + // inialize $force_refresh as false and use only when $lang is provided to load new language translations + $force_refresh = false; + $name = $module instanceof Module ? $module->name : $module; - $language = Context::getContext()->language; + if (!($language !== null && Validate::isLoadedObject($language))) { + $language = Context::getContext()->language; + } else { + $force_refresh = true; + } - if (!isset($translations_merged[$name]) && isset(Context::getContext()->language)) { + if ((!isset($translations_merged[$name]) && isset(Context::getContext()->language)) || $force_refresh) { $files_by_priority = array( // Translations in theme _PS_THEME_DIR_.'modules/'.$name.'/translations/'.$language->iso_code.'.php', @@ -149,10 +156,14 @@ class TranslateCore // PrestaShop 1.4 translations _PS_MODULE_DIR_.$name.'/'.$language->iso_code.'.php' ); + // if force_refresh is set reverse the list because array_merge will overrite the previous file with new file + if ($force_refresh) { + $files_by_priority = array_reverse($files_by_priority); + } foreach ($files_by_priority as $file) { if (file_exists($file)) { include_once($file); - $_MODULES = !empty($_MODULES) ? $_MODULES + $_MODULE : $_MODULE; //we use "+" instead of array_merge() because array merge erase existing values. + $_MODULES = !empty($_MODULES) ? ($force_refresh ? array_merge($_MODULES, $_MODULE) : $_MODULES + $_MODULE) : $_MODULE; //we use "+" instead of array_merge() when force_refresh is false because array merge erase existing values. $translations_merged[$name] = true; } } @@ -220,11 +231,15 @@ class TranslateCore * @param string $string * @return string */ - public static function getPdfTranslation($string, $sprintf = null) + public static function getPdfTranslation($string, $sprintf = null, $language = null) { global $_LANGPDF; - $iso = Context::getContext()->language->iso_code; + if (!($language !== null && Validate::isLoadedObject($language))) { + $iso = Context::getContext()->language->iso_code; + } else { + $iso = $language->iso_code; + } if (!Validate::isLangIsoCode($iso)) { Tools::displayError(sprintf('Invalid iso lang (%s)', Tools::safeOutput($iso))); diff --git a/classes/order/OrderHistory.php b/classes/order/OrderHistory.php index 3234f0824..340784c3c 100644 --- a/classes/order/OrderHistory.php +++ b/classes/order/OrderHistory.php @@ -429,7 +429,7 @@ class OrderHistoryCore extends ObjectModel if (method_exists($module, 'getExtraMailContent')) { if (is_array($extra_mail_content = $module->getExtraMailContent( $result['id_order_state'], - $order->id_lang + $order ))) { $data = array_merge($data, $extra_mail_content); } diff --git a/config/smartyadmin.config.inc.php b/config/smartyadmin.config.inc.php index f8c8a270c..fe1a3aa41 100644 --- a/config/smartyadmin.config.inc.php +++ b/config/smartyadmin.config.inc.php @@ -40,15 +40,26 @@ function smartyTranslate($params, $smarty) $addslashes = (isset($params['slashes']) || isset($params['js'])); $sprintf = isset($params['sprintf']) ? $params['sprintf'] : null; + if (!isset($params['lang'])) { + $params['lang'] = null; + } else { + if (!is_object($params['lang'])) { + $params['lang'] = new Language((int)$params['lang']); + } + if (!Validate::isLoadedObject($params['lang'])) { + $params['lang'] = null; + } + } + if ($pdf) { - return Translate::smartyPostProcessTranslation(Translate::getPdfTranslation($params['s'], $sprintf), $params); + return Translate::smartyPostProcessTranslation(Translate::getPdfTranslation($params['s'], $sprintf, $params['lang']), $params); } $filename = ((!isset($smarty->compiler_object) || !is_object($smarty->compiler_object->template)) ? $smarty->template_resource : $smarty->compiler_object->template->getTemplateFilepath()); // If the template is part of a module if (!empty($params['mod'])) { - return Translate::smartyPostProcessTranslation(Translate::getModuleTranslation($params['mod'], $params['s'], basename($filename, '.tpl'), $sprintf, isset($params['js'])), $params); + return Translate::smartyPostProcessTranslation(Translate::getModuleTranslation($params['mod'], $params['s'], basename($filename, '.tpl'), $sprintf, isset($params['js']), $params['lang']), $params); } // If the tpl is at the root of the template folder diff --git a/config/smartyfront.config.inc.php b/config/smartyfront.config.inc.php index c9afef57d..ed9d96367 100644 --- a/config/smartyfront.config.inc.php +++ b/config/smartyfront.config.inc.php @@ -24,6 +24,8 @@ * International Registered Trademark & Property of PrestaShop SA */ +use JetBrains\PhpStorm\Language; + global $smarty; $smarty->setTemplateDir(_PS_THEME_DIR_); @@ -55,6 +57,17 @@ function smartyTranslate($params, $smarty) $params['d'] = null; } + if (!isset($params['lang'])) { + $params['lang'] = null; + } else { + if (!is_object($params['lang'])) { + $params['lang'] = new Language((int)$params['lang']); + } + if (!Validate::isLoadedObject($params['lang'])) { + $params['lang'] = null; + } + } + if (!is_null($params['d'])) { if (isset($params['tags'])) { $backTrace = debug_backtrace(); @@ -102,9 +115,9 @@ function smartyTranslate($params, $smarty) } if ($params['mod']) { - return Translate::smartyPostProcessTranslation(Translate::getModuleTranslation($params['mod'], $params['s'], $basename, $params['sprintf'], $params['js']), $params); + return Translate::smartyPostProcessTranslation(Translate::getModuleTranslation($params['mod'], $params['s'], $basename, $params['sprintf'], $params['js'], $params['lang']), $params); } elseif ($params['pdf']) { - return Translate::smartyPostProcessTranslation(Translate::getPdfTranslation($params['s'], $params['sprintf']), $params); + return Translate::smartyPostProcessTranslation(Translate::getPdfTranslation($params['s'], $params['sprintf'], $params['lang']), $params); } if ($_LANG != null && isset($_LANG[$key])) { diff --git a/modules/bankwire/bankwire.php b/modules/bankwire/bankwire.php index 79b7687f4..f617c8601 100644 --- a/modules/bankwire/bankwire.php +++ b/modules/bankwire/bankwire.php @@ -77,20 +77,22 @@ class Bankwire extends PaymentModule $this->payment_type = OrderPayment::PAYMENT_TYPE_REMOTE_PAYMENT; } - public function getExtraMailContent($id_order_state, $id_lang) + public function getExtraMailContent($id_order_state, $order) { if (Configuration::get('PS_OS_AWAITING_PAYMENT') == $id_order_state) { $this->context->smarty->assign(array( 'bankwire_owner' => Configuration::get('BANK_WIRE_OWNER'), 'bankwire_details' => nl2br(Configuration::get('BANK_WIRE_DETAILS')), - 'bankwire_address' => nl2br(Configuration::get('BANK_WIRE_ADDRESS')) + 'bankwire_address' => nl2br(Configuration::get('BANK_WIRE_ADDRESS')), + 'lang' => new Language($order->id_lang) )); + return array( '{extra_mail_content_html}' => $this->context->smarty->fetch( - $this->local_path.'mails/'.Language::getIsoById($id_lang).'/mail_template_html.tpl' + $this->local_path.'views/templates/mail/mail_template_html.tpl' ), '{extra_mail_content_txt}' => $this->context->smarty->fetch( - $this->local_path.'mails/'.Language::getIsoById($id_lang).'/mail_template_text.tpl' + $this->local_path.'views/templates/mail/mail_template_text.tpl' ) ); } diff --git a/modules/bankwire/mails/en/index.php b/modules/bankwire/views/templates/mail/index.php similarity index 100% rename from modules/bankwire/mails/en/index.php rename to modules/bankwire/views/templates/mail/index.php diff --git a/modules/bankwire/mails/en/mail_template_html.tpl b/modules/bankwire/views/templates/mail/mail_template_html.tpl similarity index 80% rename from modules/bankwire/mails/en/mail_template_html.tpl rename to modules/bankwire/views/templates/mail/mail_template_html.tpl index f0df3ea06..ac5f35d46 100644 --- a/modules/bankwire/mails/en/mail_template_html.tpl +++ b/modules/bankwire/views/templates/mail/mail_template_html.tpl @@ -24,13 +24,13 @@

- {l s='Here are the bank details for your transfer:' mod='bankwire'} + {l s='Here are the bank details for your transfer:' mod='bankwire' lang=$lang}

- {l s='Amount:' mod='bankwire'} {literal}{total_paid}{/literal}
- {l s='Account owner:' mod='bankwire'} {$bankwire_owner}
- {l s='Account details:' mod='bankwire'} {$bankwire_details}
- {l s='Bank address:' mod='bankwire'} {$bankwire_address} + {l s='Amount:' mod='bankwire' lang=$lang} {literal}{total_paid}{/literal}
+ {l s='Account owner:' mod='bankwire' lang=$lang} {$bankwire_owner}
+ {l s='Account details:' mod='bankwire' lang=$lang} {$bankwire_details}
+ {l s='Bank address:' mod='bankwire' lang=$lang} {$bankwire_address}
diff --git a/modules/bankwire/mails/en/mail_template_text.tpl b/modules/bankwire/views/templates/mail/mail_template_text.tpl similarity index 100% rename from modules/bankwire/mails/en/mail_template_text.tpl rename to modules/bankwire/views/templates/mail/mail_template_text.tpl diff --git a/modules/cheque/cheque.php b/modules/cheque/cheque.php index 4f789f607..dc5ffa669 100644 --- a/modules/cheque/cheque.php +++ b/modules/cheque/cheque.php @@ -72,20 +72,22 @@ class Cheque extends PaymentModule $this->payment_type = OrderPayment::PAYMENT_TYPE_REMOTE_PAYMENT; } - public function getExtraMailContent($id_order_state, $id_lang) + public function getExtraMailContent($id_order_state, $order) { if (Configuration::get('PS_OS_AWAITING_PAYMENT') == $id_order_state) { $this->context->smarty->assign(array( 'cheque_name' => Configuration::get('CHEQUE_NAME'), 'cheque_address' => Configuration::get('CHEQUE_ADDRESS'), - 'cheque_address_html' => str_replace("\n", '
', Configuration::get('CHEQUE_ADDRESS')) + 'cheque_address_html' => str_replace("\n", '
', Configuration::get('CHEQUE_ADDRESS')), + 'lang' => new Language($order->id_lang) )); + return array( '{extra_mail_content_html}' => $this->context->smarty->fetch( - $this->local_path.'mails/'.Language::getIsoById($id_lang).'/mail_template_html.tpl' + $this->local_path.'views/templates/mail/mail_template_html.tpl' ), '{extra_mail_content_txt}' => $this->context->smarty->fetch( - $this->local_path.'mails/'.Language::getIsoById($id_lang).'/mail_template_text.tpl' + $this->local_path.'views/templates/mail/mail_template_text.tpl' ) ); } diff --git a/modules/cheque/mails/en/index.php b/modules/cheque/mails/en/index.php deleted file mode 100644 index 8d2cf16fe..000000000 --- a/modules/cheque/mails/en/index.php +++ /dev/null @@ -1,29 +0,0 @@ - -* @copyright 2010-2022 Webkul IN -* @license https://store.webkul.com/license.html -*/ - -header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); -header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); - -header('Cache-Control: no-store, no-cache, must-revalidate'); -header('Cache-Control: post-check=0, pre-check=0', false); -header('Pragma: no-cache'); - -header('Location: ../../../'); -exit; diff --git a/modules/cheque/mails/index.php b/modules/cheque/mails/index.php deleted file mode 100644 index 8d2cf16fe..000000000 --- a/modules/cheque/mails/index.php +++ /dev/null @@ -1,29 +0,0 @@ - -* @copyright 2010-2022 Webkul IN -* @license https://store.webkul.com/license.html -*/ - -header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); -header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); - -header('Cache-Control: no-store, no-cache, must-revalidate'); -header('Cache-Control: post-check=0, pre-check=0', false); -header('Pragma: no-cache'); - -header('Location: ../../../'); -exit; diff --git a/modules/bankwire/mails/index.php b/modules/cheque/views/templates/mail/index.php similarity index 100% rename from modules/bankwire/mails/index.php rename to modules/cheque/views/templates/mail/index.php diff --git a/modules/cheque/mails/en/mail_template_html.tpl b/modules/cheque/views/templates/mail/mail_template_html.tpl similarity index 83% rename from modules/cheque/mails/en/mail_template_html.tpl rename to modules/cheque/views/templates/mail/mail_template_html.tpl index 8fb0d1891..d6ade1dac 100644 --- a/modules/cheque/mails/en/mail_template_html.tpl +++ b/modules/cheque/views/templates/mail/mail_template_html.tpl @@ -24,12 +24,12 @@

- {l s='Here are the bank details for your check:' mod='cheque'} + {l s='Here are the bank details for your check:' mod='cheque' lang=$lang}

- {l s='Amount:' mod='cheque'} {literal}{total_paid}{/literal}
- {l s='Payable to the order of:' mod='cheque'} {$cheque_name}
- {l s='Please mail your check to:' mod='cheque'} {$cheque_address_html} + {l s='Amount:' mod='cheque' lang=$lang} {literal}{total_paid}{/literal}
+ {l s='Payable to the order of:' mod='cheque' lang=$lang} {$cheque_name}
+ {l s='Please mail your check to:' mod='cheque' lang=$lang} {$cheque_address_html}
diff --git a/modules/cheque/mails/en/mail_template_text.tpl b/modules/cheque/views/templates/mail/mail_template_text.tpl similarity index 65% rename from modules/cheque/mails/en/mail_template_text.tpl rename to modules/cheque/views/templates/mail/mail_template_text.tpl index ece2eaffa..adc2c0ac6 100644 --- a/modules/cheque/mails/en/mail_template_text.tpl +++ b/modules/cheque/views/templates/mail/mail_template_text.tpl @@ -16,8 +16,8 @@ * @copyright 2010-2022 Webkul IN * @license https://store.webkul.com/license.html *} -{l s='Here are the bank details for your check:' mod='cheque'} +{l s='Here are the bank details for your check:' mod='cheque' lang=$lang} -{l s='Amount:' mod='cheque'} {literal}{total_paid}{/literal} -{l s='Payable to the order of:' mod='cheque'} {$cheque_name} -{l s='Please mail your check to:' mod='cheque'} {$cheque_address} +{l s='Amount:' mod='cheque' lang=$lang} {literal}{total_paid}{/literal} +{l s='Payable to the order of:' mod='cheque' lang=$lang} {$cheque_name} +{l s='Please mail your check to:' mod='cheque' lang=$lang} {$cheque_address}