From 7ce65026007a0bb7bd033a005c8c211d8bce92df Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Tue, 23 Apr 2013 11:21:10 +0200 Subject: [PATCH] // Changed translation tool behavior with dev mode and overrides --- .../translations/helpers/view/main.tpl | 10 +- .../admin/AdminTranslationsController.php | 107 +++++++++--------- 2 files changed, 57 insertions(+), 60 deletions(-) diff --git a/admin-dev/themes/default/template/controllers/translations/helpers/view/main.tpl b/admin-dev/themes/default/template/controllers/translations/helpers/view/main.tpl index 6102eb998..a5a6cc20e 100644 --- a/admin-dev/themes/default/template/controllers/translations/helpers/view/main.tpl +++ b/admin-dev/themes/default/template/controllers/translations/helpers/view/main.tpl @@ -60,15 +60,15 @@
- {foreach $translations_type as $type => $array} - + {/foreach} - + {foreach $themes as $theme} - - + {/foreach} {foreach $languages as $language} diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index af0b6970e..5a0a4d226 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -788,9 +788,6 @@ class AdminTranslationsControllerCore extends AdminController static $str_write = ''; static $array_check_duplicate = array(); - // Default translations and Prestashop overriding themes are distinguish - $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false; - // 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])) { @@ -827,16 +824,16 @@ class AdminTranslationsControllerCore extends AdminController foreach ($matches as $key) { - if ($is_default) - { - $post_key = md5(strtolower($module_name).'_'.self::DEFAULT_THEME_NAME.'_'.strtolower($template_name).'_'.md5($key)); - $pattern = '\'<{'.strtolower($module_name).'}prestashop>'.strtolower($template_name).'_'.md5($key).'\''; - } - else + if ($theme_name) { $post_key = md5(strtolower($module_name).'_'.strtolower($theme_name).'_'.strtolower($template_name).'_'.md5($key)); $pattern = '\'<{'.strtolower($module_name).'}'.strtolower($theme_name).'>'.strtolower($template_name).'_'.md5($key).'\''; } + else + { + $post_key = md5(strtolower($module_name).'_'.self::DEFAULT_THEME_NAME.'_'.strtolower($template_name).'_'.md5($key)); + $pattern = '\'<{'.strtolower($module_name).'}prestashop>'.strtolower($template_name).'_'.md5($key).'\''; + } if (array_key_exists($post_key, $_POST) && !empty($_POST[$post_key]) && !in_array($pattern, $array_check_duplicate)) { @@ -900,9 +897,6 @@ class AdminTranslationsControllerCore extends AdminController // added for compatibility $GLOBALS[$name_var] = array_change_key_case($GLOBALS[$name_var]); - // Default translations and Prestashop overriding themes are distinguish - $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false; - // Thank to this var similar keys are not duplicate // in AndminTranslation::modules_translations array // see below @@ -929,8 +923,11 @@ class AdminTranslationsControllerCore extends AdminController foreach ($matches as $key) { - $module_key = '<{'.Tools::strtolower($module_name).'}'. - strtolower($is_default ? 'prestashop' : $theme_name).'>'.Tools::strtolower($template_name).'_'.md5($key); + $md5_key = md5($key); + $module_key = null; + if ($theme_name) + $module_key = '<{'.Tools::strtolower($module_name).'}'.strtolower($theme_name).'>'.Tools::strtolower($template_name).'_'.$md5_key; + $default_key = '<{'.Tools::strtolower($module_name).'}prestashop>'.Tools::strtolower($template_name).'_'.$md5_key; // to avoid duplicate entry if (!in_array($module_key, $array_check_duplicate)) { @@ -938,8 +935,10 @@ class AdminTranslationsControllerCore extends AdminController if (!isset($this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'])) $this->total_expression++; - if (array_key_exists($module_key, $GLOBALS[$name_var])) + if ($module_key && array_key_exists($module_key, $GLOBALS[$name_var])) $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$module_key], ENT_COMPAT, 'UTF-8'); + elseif (array_key_exists($default_key, $GLOBALS[$name_var])) + $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$default_key], ENT_COMPAT, 'UTF-8'); else { $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = ''; @@ -1128,7 +1127,7 @@ class AdminTranslationsControllerCore extends AdminController 'front' => array( 'name' => $this->l('Front Office translations'), 'var' => '_LANG', - 'dir' => _PS_THEME_SELECTED_DIR_.'lang/', + 'dir' => defined('_PS_THEME_SELECTED_DIR_') ? _PS_THEME_SELECTED_DIR_.'lang/' : '', 'file' => $this->lang_selected->iso_code.'.php' ), 'back' => array( @@ -1153,33 +1152,28 @@ class AdminTranslationsControllerCore extends AdminController 'name' => $this->l('Installed modules translations'), 'var' => '_MODULES', 'dir' => _PS_MODULE_DIR_, - 'file' => '', - 'override' => array( - 'dir' => _PS_THEME_SELECTED_DIR_.'modules/', - 'file' => '' - ) + 'file' => '' ), 'pdf' => array( 'name' => $this->l('PDF translations'), 'var' => '_LANGPDF', 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/', - 'file' => 'pdf.php', - 'override' => array( - 'dir' => _PS_THEME_SELECTED_DIR_.'pdf/lang/', - 'file' => $this->lang_selected->iso_code.'.php' - ) + 'file' => 'pdf.php' ), 'mails' => array( 'name' => $this->l('Email templates translations'), 'var' => '_LANGMAIL', 'dir' => _PS_MAIL_DIR_.$this->lang_selected->iso_code.'/', - 'file' => 'lang.php', - 'override' => array( - 'dir' => _PS_THEME_SELECTED_DIR_.'mails/'.$this->lang_selected->iso_code.'/', - 'file' => 'lang.php' - ) + 'file' => 'lang.php' ) ); + + if (defined('_PS_THEME_SELECTED_DIR_')) + { + $this->translations_informations['modules']['override'] = array('dir' => _PS_THEME_SELECTED_DIR_.'modules/', 'file' => ''); + $this->translations_informations['pdf']['override'] = array('dir' => _PS_THEME_SELECTED_DIR_.'pdf/lang/', 'file' => $this->lang_selected->iso_code.'.php'); + $this->translations_informations['mails']['override'] = array('dir' => _PS_THEME_SELECTED_DIR_.'mails/'.$this->lang_selected->iso_code.'/', 'file' => 'lang.php'); + } } /** @@ -1204,16 +1198,14 @@ class AdminTranslationsControllerCore extends AdminController foreach ($this->themes as $existing_theme) if ($existing_theme->directory == $theme) $theme_exists = true; - if ($theme_exists) - $this->theme_selected = Tools::safeOutput($theme); - else + if (!$theme_exists) throw new PrestaShopException(sprintf(Tools::displayError('Invalid theme "%s"'), $theme)); + $this->theme_selected = Tools::safeOutput($theme); } - else - $this->theme_selected = self::DEFAULT_THEME_NAME; // Set the path of selected theme - define('_PS_THEME_SELECTED_DIR_', _PS_ROOT_DIR_.'/themes/'.$this->theme_selected.'/'); + if ($this->theme_selected) + define('_PS_THEME_SELECTED_DIR_', _PS_ROOT_DIR_.'/themes/'.$this->theme_selected.'/'); // Get type of translation if (($type = Tools::getValue('type')) && !is_array($type)) @@ -1294,7 +1286,7 @@ class AdminTranslationsControllerCore extends AdminController { if ($this->tabAccess['edit'] === '1') // Only the PrestaShop team should write the translations into the _PS_TRANSLATIONS_DIR_ - if (($this->theme_selected == self::DEFAULT_THEME_NAME) && _PS_MODE_DEV_) + if (!$this->theme_selected) $this->writeTranslationFile(); else $this->writeTranslationFile(true); @@ -1339,7 +1331,7 @@ class AdminTranslationsControllerCore extends AdminController { // Get files of all modules $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']); @@ -1409,7 +1401,7 @@ class AdminTranslationsControllerCore extends AdminController $arr_mail_content['core_mail'] = Tools::getValue('core_mail'); // Get path of directory for find a good path of translation file - if ($this->theme_selected == self::DEFAULT_THEME_NAME && _PS_MODE_DEV_) + if (!$this->theme_selected) $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['dir']; else $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['override']['dir']; @@ -1420,7 +1412,7 @@ class AdminTranslationsControllerCore extends AdminController $arr_mail_content['module_mail'] = Tools::getValue('module_mail'); // Get path of directory for find a good path of translation file - if ($this->theme_selected == self::DEFAULT_THEME_NAME && _PS_MODE_DEV_) + if (!$this->theme_selected) $arr_mail_path['module_mail'] = $this->translations_informations['modules']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/'; else $arr_mail_path['module_mail'] = $this->translations_informations['modules']['override']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/'; @@ -2300,7 +2292,7 @@ class AdminTranslationsControllerCore extends AdminController if (!in_array($module_dir, self::$ignore_folder)) { $dir = false; - if (($this->theme_selected != self::DEFAULT_THEME_NAME || !_PS_MODE_DEV_) && Tools::file_exists_cache($this->translations_informations['modules']['override']['dir'].$module_dir.'/mails/')) + if ($this->theme_selected && Tools::file_exists_cache($this->translations_informations['modules']['override']['dir'].$module_dir.'/mails/')) $dir = $this->translations_informations['modules']['override']['dir'].$module_dir.'/'; elseif (Tools::file_exists_cache($this->translations_informations['modules']['dir'].$module_dir.'/mails/')) $dir = $this->translations_informations['modules']['dir'].$module_dir.'/'; @@ -2359,7 +2351,7 @@ class AdminTranslationsControllerCore extends AdminController $subject_mail = $this->getSubjectMail($dir, $file, $subject_mail); // Get path of directory for find a good path of translation file - if (($this->theme_selected != self::DEFAULT_THEME_NAME || !_PS_MODE_DEV_) && @filemtime($this->translations_informations[$this->type_selected]['override']['dir'])) + if ($this->theme_selected && @filemtime($this->translations_informations[$this->type_selected]['override']['dir'])) $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir']; else $i18n_dir = $this->translations_informations[$this->type_selected]['dir']; @@ -2534,28 +2526,33 @@ class AdminTranslationsControllerCore extends AdminController $root_dir = $initial_root_dir; if ($module{0} == '.') continue; - // Get path of directory for find a good path of translation file - if ($root_dir == null) - { - $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir']; - if (is_dir($i18n_dir.$module)) - $root_dir = $i18n_dir; - } + + // First we load the default translation file if ($root_dir == null) { $i18n_dir = $this->translations_informations[$this->type_selected]['dir']; if (is_dir($i18n_dir.$module)) $root_dir = $i18n_dir; - } - - if (is_dir($root_dir.$module)) - { if (Tools::file_exists_cache($root_dir.$module.'/translations/'.$lang.'.php')) $lang_file = $root_dir.$module.'/translations/'.$lang.'.php'; else $lang_file = $root_dir.$module.'/'.$lang.'.php'; @include($lang_file); + $this->getModuleTranslations(); + $this->recursiveGetModuleFiles($root_dir.$module.'/', $array_files, $module, $lang_file, $is_default); + } + // Then we load the overriden translation file + if (isset($this->translations_informations[$this->type_selected]['override'])) + { + $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir']; + if (is_dir($i18n_dir.$module)) + $root_dir = $i18n_dir; + if (Tools::file_exists_cache($root_dir.$module.'/translations/'.$lang.'.php')) + $lang_file = $root_dir.$module.'/translations/'.$lang.'.php'; + else + $lang_file = $root_dir.$module.'/'.$lang.'.php'; + @include($lang_file); $this->getModuleTranslations(); $this->recursiveGetModuleFiles($root_dir.$module.'/', $array_files, $module, $lang_file, $is_default); } @@ -2641,7 +2638,7 @@ class AdminTranslationsControllerCore extends AdminController $i18n_dir = $this->translations_informations[$this->type_selected]['dir']; $default_i18n_file = $i18n_dir.$this->translations_informations[$this->type_selected]['file']; - if (($this->theme_selected == self::DEFAULT_THEME_NAME) && _PS_MODE_DEV_) + if (!$this->theme_selected) $i18n_file = $default_i18n_file; else {