Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated: When translating text in QloApps only update the fields that are changed #861

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{extends file="helpers/view/view.tpl"}

{block name="override_tpl"}
{include file="./translation_inputs_update.tpl"}
{if $mod_security_warning}
<div class="alert alert-warning">
{l s='Apache mod_security is activated on your server. This could result in some Bad Request errors'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{extends file="helpers/view/view.tpl"}

{block name="override_tpl"}
{include file="./translation_inputs_update.tpl"}
{if $mod_security_warning}
<div class="alert alert-warning">
{l s='Apache mod_security is activated on your server. This could result in some Bad Request errors'}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{**
* 2010-2023 Webkul.
*
* NOTICE OF LICENSE
*
* All right is reserved,
* Please go through LICENSE.txt file inside our module
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to CustomizationPolicy.txt file inside our module for more information.
*
* @author Webkul IN
* @copyright 2010-2023 Webkul IN
* @license LICENSE.txt
*}

<script type="text/javascript">
$(document).ready(function(){
$('#translations_form input:text,textarea').each(function(){
$(this).data('name',$(this).attr('name'));
$(this).removeAttr('name');
});
$('#translations_form').on('change','input:text,textarea',function(){
var name = $(this).data('name');
if(name) $(this).attr('name',name);
});
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{extends file="helpers/view/view.tpl"}

{block name="override_tpl"}
{include file="./translation_inputs_update.tpl"}
{if $mod_security_warning}
<div class="alert alert-warning">
{l s='Apache mod_security is activated on your server. This could result in some Bad Request errors'}
Expand Down
85 changes: 63 additions & 22 deletions controllers/admin/AdminTranslationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,32 +292,63 @@ protected function writeTranslationFile($override_file = false)
}
}

if ($fd = fopen($file_path, 'w')) {
// Get value of button save and stay
$save_and_stay = Tools::isSubmit('submitTranslations'.$type.'AndStay');

// Get language
$lang = strtolower(Tools::getValue('lang'));

// Unset all POST which are not translations
unset(
$_POST['submitTranslations'.$type],
$_POST['submitTranslations'.$type.'AndStay'],
$_POST['lang'],
$_POST['token'],
$_POST['theme'],
$_POST['type']
);
// Get value of button save and stay
$save_and_stay = Tools::isSubmit('submitTranslations'.$type.'AndStay');

// Get language
$lang = strtolower(Tools::getValue('lang'));

// Unset all POST which are not translations
unset(
$_POST['submitTranslations'.$type],
$_POST['submitTranslations'.$type.'AndStay'],
$_POST['lang'],
$_POST['token'],
$_POST['theme'],
$_POST['type']
);

// Get all POST which aren't empty
$to_update = array();
$keysToUpdate = array();
foreach ($_POST as $key => $value) {
$keysToUpdate[] = $key;
$to_update[$key] = $value;
}
include_once($file_path);
switch($this->type_selected) {
case 'front':
$to_insert = $GLOBALS['_LANG'];
break;
case 'back':
$to_insert = $GLOBALS['_LANGADM'];
break;
case 'errors':
$to_insert = $GLOBALS['_ERRORS'];
break;
case 'fields':
$to_insert = $GLOBALS['_FIELDS'];
break;
case 'pdf':
$to_insert = $GLOBALS['_LANGPDF'];
break;

// Get all POST which aren't empty
$to_insert = array();
foreach ($_POST as $key => $value) {
if (!empty($value)) {
$to_insert[$key] = $value;
}
foreach ($to_insert as $key => $value) {
if (in_array($key, $keysToUpdate)) {
if ($to_update[$key]) {
$to_insert[$key] = $to_update[$key];
} else {
unset($to_insert[$key]);
}
unset($to_update[$key]);
}
}
foreach ($to_update as $key => $value) {
$to_insert[$key] = $value;
}

// translations array is ordered by key (easy merge)
if ($fd = fopen($file_path, 'w')) {
ksort($to_insert);
$tab = $translation_informations['var'];
fwrite($fd, "<?php\n\nglobal \$".$tab.";\n\$".$tab." = array();\n");
Expand Down Expand Up @@ -929,9 +960,12 @@ protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_n
static $cache_file = array();
static $str_write = '';
static $array_check_duplicate = array();
static $module_existing_translations = 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])) {
require $file_name;
$module_existing_translations = $_MODULE;
$str_write = '';
$cache_file[$theme_name.'-'.$file_name] = true;
if (!Tools::file_exists_cache(dirname($file_name))) {
Expand Down Expand Up @@ -984,6 +1018,13 @@ protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_n
$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";

}
}

}
}

Expand Down