diff --git a/classes/AdminTab.php b/classes/AdminTab.php index eea20ef85..b3fe51d43 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -249,7 +249,7 @@ abstract class AdminTabCore if(Module::getModuleNameFromClass($currentClass)) { $string = str_replace('\'', '\\\'', $string); - return Module::findTranslation(Module::$classInModule[$currentClass], $string, $currentClass); + return Translate::getModuleTranslation(Module::$classInModule[$currentClass], $string, $currentClass); } global $_LANGADM; diff --git a/classes/Module.php b/classes/Module.php index 1d67ef795..f0d2cfbc0 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -868,7 +868,7 @@ abstract class ModuleCore $_MODULES = !empty($_MODULES) ? array_merge($_MODULES, $_MODULE) : $_MODULE; // Return Name - return Module::findTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->displayName), (string)$xml_module->name); + return Translate::getModuleTranslation((string)$xml_module->name, Module::configXmlStringFormat($xml_module->displayName), (string)$xml_module->name); } @@ -938,12 +938,12 @@ abstract class ModuleCore $item->warning = ''; foreach ($xml_module as $k => $v) $item->$k = (string)$v; - $item->displayName = Module::findTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->displayName), (string)$xml_module->name); - $item->description = Module::findTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->description), (string)$xml_module->name); - $item->author = Module::findTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->author), (string)$xml_module->name); + $item->displayName = Translate::getModuleTranslation((string)$xml_module->name, Module::configXmlStringFormat($xml_module->displayName), (string)$xml_module->name); + $item->description = Translate::getModuleTranslation((string)$xml_module->name, Module::configXmlStringFormat($xml_module->description), (string)$xml_module->name); + $item->author = Translate::getModuleTranslation((string)$xml_module->name, Module::configXmlStringFormat($xml_module->author), (string)$xml_module->name); if (isset($xml_module->confirmUninstall)) - $item->confirmUninstall = Module::findTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->confirmUninstall), (string)$xml_module->name); + $item->confirmUninstall = Translate::getModuleTranslation($xml_module->name, Module::configXmlStringFormat($xml_module->confirmUninstall), (string)$xml_module->name); $item->active = 0; $module_list[] = $item; @@ -1225,44 +1225,13 @@ abstract class ModuleCore } /** - * find translation from $_MODULES and put it in self::$l_cache if not already exist - * and return it. - * - * @param string $name name of the module - * @param string $string term to find - * @param string $source additional param for building translation key - * @return string + * @deprecated 1.5.0 Use Translate::getModuleTranslation() */ public static function findTranslation($name, $string, $source) { - global $_MODULES; - - $cache_key = $name.'|'.$string.'|'.$source; - - if (!isset(self::$l_cache[$cache_key])) - { - if (!is_array($_MODULES)) - return str_replace('"', '"', $string); - // set array key to lowercase for 1.3 compatibility - $_MODULES = array_change_key_case($_MODULES); - $currentKey = '<{'.strtolower($name).'}'.strtolower(_THEME_NAME_).'>'.strtolower($source).'_'.md5($string); - $defaultKey = '<{'.strtolower($name).'}prestashop>'.strtolower($source).'_'.md5($string); - - if (isset($_MODULES[$currentKey])) - $ret = stripslashes($_MODULES[$currentKey]); - elseif (isset($_MODULES[Tools::strtolower($currentKey)])) - $ret = stripslashes($_MODULES[Tools::strtolower($currentKey)]); - elseif (isset($_MODULES[$defaultKey])) - $ret = stripslashes($_MODULES[$defaultKey]); - elseif (isset($_MODULES[Tools::strtolower($defaultKey)])) - $ret = stripslashes($_MODULES[Tools::strtolower($defaultKey)]); - else - $ret = stripslashes($string); - - self::$l_cache[$cache_key] = str_replace('"', '"', $ret); - } - return self::$l_cache[$cache_key]; + return Translate::getModuleTranslation($name, $string, $source); } + /** * Get translation for a given module text * @@ -1279,21 +1248,7 @@ abstract class ModuleCore if (self::$_generate_config_xml_mode) return $string; - global $_MODULES, $_MODULE; - - // @retrocompatibility with translations files in module root - if (Tools::file_exists_cache($this->local_path.'/translations/'.Context::getContext()->language->iso_code.'.php')) - $file = $this->local_path.'/translations/'.Context::getContext()->language->iso_code.'.php'; - else - $file = $this->local_path.'/'.Context::getContext()->language->iso_code.'.php'; - - if (Tools::file_exists_cache($file) && include_once($file)) - $_MODULES = !empty($_MODULES) ? array_merge($_MODULES, $_MODULE) : $_MODULE; - - $source = $specific ? $specific : $this->name; - $string = str_replace('\'', '\\\'', $string); - $ret = Module::findTranslation($this->name, $string, $source); - return $ret; + return Translate::getModuleTranslation($this, $string, ($specific) ? $specific : $this->name); } /* diff --git a/classes/Translate.php b/classes/Translate.php new file mode 100644 index 000000000..fb2369e5a --- /dev/null +++ b/classes/Translate.php @@ -0,0 +1,181 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +/** + * @since 1.5.0 + */ +class TranslateCore +{ + /** + * Get a translation for an admin controller + * + * @param $string + * @param string $class + * @param bool $addslashes + * @param bool $htmlentities + * @return string + */ + public static function getAdminTranslation($string, $class = 'AdminTab', $addslashes = false, $htmlentities = true) + { + static $modules_tabs = null; + + // @todo remove global keyword in translations files and use static + global $_LANGADM; + + if (!isset($modules_tabs)) + $modules_tabs = Tab::getModuleTabList(); + + if (!is_array($_LANGADM)) + { + $iso = Context::getContext()->language->iso_code; + include_once(_PS_TRANSLATIONS_DIR_.$iso.'/admin.php'); + } + + if (isset($modules_tabs[strtolower($class)])) + { + $class_name_controller = $class.'controller'; + // if the class is extended by a module, use modules/[module_name]/xx.php lang file + if (class_exists($class_name_controller) && Module::getModuleNameFromClass($class_name_controller)) + { + $string = str_replace('\'', '\\\'', $string); + return Translate::getModuleTranslation(Module::$classInModule[$class_name_controller], $string, $class); + } + } + $key = md5(str_replace('\'', '\\\'', $string)); + // retrocomp : if value is not set, try with "AdminTab" as prefix. + // @todo : change AdminTab to Helper + + if (isset($_LANGADM[$class.$key])) + $str = $_LANGADM[$class.$key]; + else if (isset($_LANGADM['AdminController'.$key])) + $str = $_LANGADM['AdminController'.$key]; + else if (isset($_LANGADM['Helper'.$key])) + $str = $_LANGADM['Helper'.$key]; + else if (isset($_LANGADM['AdminTab'.$key])) + $str = $_LANGADM['AdminTab'.$key]; + else + // note in 1.5, some translations has moved from AdminXX to helper/*.tpl + $str = $string; + + $str = $htmlentities ? htmlentities($str, ENT_QUOTES, 'utf-8') : $str; + return str_replace('"', '"', ($addslashes ? addslashes($str) : stripslashes($str))); + } + + /** + * Get a translation for a module + * + * @param string|Module $module + * @param string $string + * @param string $source + * @return string + */ + public static function getModuleTranslation($module, $string, $source) + { + global $_MODULES, $_MODULE; + static $lang_cache = array(); + + if ($module instanceof Module) + { + $name = $module->name; + $local_path = $module->getLocalPath(); + } + else + { + $name = $module; + $local_path = _PS_MODULE_DIR_.$module.'/'; + } + + // @retrocompatibility with translations files in module root + if (Tools::file_exists_cache($local_path.'/translations/'.Context::getContext()->language->iso_code.'.php')) + $file = $local_path.'/translations/'.Context::getContext()->language->iso_code.'.php'; + else + $file = $local_path.'/'.Context::getContext()->language->iso_code.'.php'; + + if (Tools::file_exists_cache($file) && include_once($file)) + $_MODULES = !empty($_MODULES) ? array_merge($_MODULES, $_MODULE) : $_MODULE; + + $string = str_replace('\'', '\\\'', $string); + + $cache_key = $name.'|'.$string.'|'.$source; + if (!isset($lang_cache[$cache_key])) + { + if (!is_array($_MODULES)) + return str_replace('"', '"', $string); + + // set array key to lowercase for 1.3 compatibility + $_MODULES = array_change_key_case($_MODULES); + $currentKey = '<{'.strtolower($name).'}'.strtolower(_THEME_NAME_).'>'.strtolower($source).'_'.md5($string); + $defaultKey = '<{'.strtolower($name).'}prestashop>'.strtolower($source).'_'.md5($string); + + if (isset($_MODULES[$currentKey])) + $ret = stripslashes($_MODULES[$currentKey]); + elseif (isset($_MODULES[Tools::strtolower($currentKey)])) + $ret = stripslashes($_MODULES[Tools::strtolower($currentKey)]); + elseif (isset($_MODULES[$defaultKey])) + $ret = stripslashes($_MODULES[$defaultKey]); + elseif (isset($_MODULES[Tools::strtolower($defaultKey)])) + $ret = stripslashes($_MODULES[Tools::strtolower($defaultKey)]); + else + $ret = stripslashes($string); + + $lang_cache[$cache_key] = str_replace('"', '"', $ret); + } + return $lang_cache[$cache_key]; + } + + /** + * Get a translation for a PDF + * + * @param string $string + * @return string + */ + public static function getPdfTranslation($string) + { + global $_LANGPDF; + + $iso = Context::getContext()->language->iso_code; + if (!Validate::isLanguageIsoCode($iso)) + throw PrestaShopException('Invalid iso lang!'); + + $filename = _PS_THEME_DIR_.'pdf/lang/'.$iso.'.php'; + + if (Tools::file_exists_cache($filename)) + @include_once($filename); + + $key = 'PDF'.md5($string); + $lang_array = $_LANGPDF; + + $msg = $string; + if (is_array($lang_array) && key_exists($key, $lang_array)) + $msg = $lang_array[$key]; + elseif (is_array($lang_array) && key_exists(Tools::strtolower($key), $lang_array)) + $msg = $lang_array[Tools::strtolower($key)]; + + return $msg; + } +} + diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index b1bb22dbe..1e5215909 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -166,9 +166,6 @@ class AdminControllerCore extends Controller /** @var array $cache_lang cache for traduction */ public static $cache_lang = array(); - /** @var array list the the tab using under a module */ - public static $tab_module_list; - /** @var array required_fields to display in the Required Fields form */ public $required_fields = array(); @@ -1581,61 +1578,7 @@ class AdminControllerCore extends Controller $this->addJs(_PS_JS_DIR_.'notifications.js'); if (Configuration::get('PS_HELPBOX')) $this->addJS(_PS_JS_DIR_.'helpAccess.js'); - } - } - /** - * use translations files to replace english expression. - * - * @param mixed $string term or expression in english - * @param string $class the classname (without "Controller" suffix) - * @param boolan $addslashes if set to true, the return value will pass through addslashes(). Otherwise, stripslashes(). - * @param boolean $htmlentities if set to true(default), the return value will pass through htmlentities($string, ENT_QUOTES, 'utf-8') - * @return string the translation if available, or the english default text. - */ - public static function translate($string, $class, $addslashes = false, $htmlentities = true) - { - // every admin translations should use this method - // @todo remove global keyword in translations files and use static - global $_LANGADM; - - AdminController::initModuleCacheTab(); - - if (!is_array($_LANGADM)) - { - $iso = Context::getContext()->language->iso_code; - include_once(_PS_TRANSLATIONS_DIR_.$iso.'/admin.php'); } - - if (isset(self::$tab_module_list[strtolower($class)])) - { - $class_name_controller = $class.'controller'; - // if the class is extended by a module, use modules/[module_name]/xx.php lang file - if (class_exists($class_name_controller) && Module::getModuleNameFromClass($class_name_controller)) - { - $string = str_replace('\'', '\\\'', $string); - return Module::findTranslation(Module::$classInModule[$class_name_controller], $string, $class); - } - } - $key = md5(str_replace('\'', '\\\'', $string)); - // retrocomp : - // if value is not set, try with "AdminTab" as prefix. - // @todo : change AdminTab to Helper - //echo $class.$key.'
'; - - if (isset($_LANGADM[$class.$key])) - $str = $_LANGADM[$class.$key]; - else if (isset($_LANGADM['AdminController'.$key])) - $str = $_LANGADM['AdminController'.$key]; - else if (isset($_LANGADM['Helper'.$key])) - $str = $_LANGADM['Helper'.$key]; - else if (isset($_LANGADM['AdminTab'.$key])) - $str = $_LANGADM['AdminTab'.$key]; - else - // note in 1.5, some translations has moved from AdminXX to helper/*.tpl - $str = $string; - - $str = $htmlentities ? htmlentities($str, ENT_QUOTES, 'utf-8') : $str; - return str_replace('"', '"', ($addslashes ? addslashes($str) : stripslashes($str))); } /** @@ -1655,16 +1598,7 @@ class AdminControllerCore extends Controller $class = substr($class, 0, -10); else if ($class == 'AdminTab') $class = substr(get_class($this), 0, -10); - return AdminController::translate($string, $class, $addslashes, $htmlentities); - } - - /** - * Init a cache list of tab existing in a module - */ - public static function initModuleCacheTab() - { - if (!isset(self::$tab_module_list)) - self::$tab_module_list = Tab::getModuleTabList(); + return Translate::getAdminTranslation($string, $class, $addslashes, $htmlentities); } /** diff --git a/classes/helper/Helper.php b/classes/helper/Helper.php index 21ae3a00c..c6eeb693d 100755 --- a/classes/helper/Helper.php +++ b/classes/helper/Helper.php @@ -295,19 +295,9 @@ class HelperCore // if the class is extended by a module, use modules/[module_name]/xx.php lang file $currentClass = get_class($this); if (Module::getModuleNameFromClass($currentClass)) - { - $string = str_replace('\'', '\\\'', $string); - return Module::findTranslation(Module::$classInModule[$currentClass], $string, $currentClass); - } - global $_LANGADM; + return Translate::getModuleTranslation(Module::$classInModule[$currentClass], $string, $currentClass); - if ($class == __CLASS__) - $class = 'AdminTab'; - - $key = md5(str_replace('\'', '\\\'', $string)); - $str = (key_exists(get_class($this).$key, $_LANGADM)) ? $_LANGADM[get_class($this).$key] : ((key_exists($class.$key, $_LANGADM)) ? $_LANGADM[$class.$key] : $string); - $str = $htmlentities ? htmlentities($str, ENT_QUOTES, 'utf-8') : $str; - return str_replace('"', '"', ($addslashes ? addslashes($str) : stripslashes($str))); + return Translate::getAdminTranslation($string, get_class($this), $addslashes, $htmlentities); } /** diff --git a/config/smartyadmin.config.inc.php b/config/smartyadmin.config.inc.php index f6ea004c8..e4cbbf0d9 100644 --- a/config/smartyadmin.config.inc.php +++ b/config/smartyadmin.config.inc.php @@ -35,68 +35,13 @@ function smartyTranslate($params, &$smarty) $addslashes = isset($params['slashes']); if ($pdf) - { - global $_LANGPDF; - $iso = Context::getContext()->language->iso_code; - if (!Validate::isLanguageIsoCode($iso)) - throw PrestaShopException('Invalid iso lang!'); + return Translate::getPdfTranslation($params['s']); - $translationsFile = _PS_THEME_DIR_.'pdf/lang/'.$iso.'.php'; - - if (Tools::file_exists_cache($translationsFile)) - @include_once($translationsFile); - - $key = 'PDF'.md5($params['s']); - $lang_array = $_LANGPDF; - - $msg = $params['s']; - if (is_array($lang_array) AND key_exists($key, $lang_array)) - $msg = $lang_array[$key]; - elseif (is_array($lang_array) && key_exists(Tools::strtolower($key), $lang_array)) - $msg = $lang_array[Tools::strtolower($key)]; - - return $msg; - } - - $filename = ((!isset($smarty->compiler_object) OR !is_object($smarty->compiler_object->template)) ? $smarty->template_resource : $smarty->compiler_object->template->getTemplateFilepath()); + $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'])) - { - global $_MODULES, $_MODULE; - - $key = Tools::substr(basename($filename), 0, -4).'_'.md5($params['s']); - $iso = Language::getIsoById(Context::getContext()->language->id); - - if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php')) - { - $translationsFile = _PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php'; - $key = '<{'.$params['mod'].'}'._THEME_NAME_.'>'.$key; - } - else - { - // @retrocompatibility with translations files in module root - if (Tools::file_exists_cache(_PS_MODULE_DIR_.$params['mod'].'/translations')) - $translationsFile = _PS_MODULE_DIR_.$params['mod'].'/translations/'.$iso.'.php'; - else - $translationsFile = _PS_MODULE_DIR_.$params['mod'].'/'.$iso.'.php'; - $key = '<{'.$params['mod'].'}prestashop>'.$key; - } - - if (!is_array($_MODULES)) - $_MODULES = array(); - if (@include_once($translationsFile)) - if (is_array($_MODULE)) - $_MODULES = array_merge($_MODULES, $_MODULE); - $lang_array = $_MODULES; - if (is_array($lang_array) && key_exists($key, $lang_array)) - $msg = $lang_array[$key]; - elseif (is_array($lang_array) && key_exists(Tools::strtolower($key), $lang_array)) - $msg = $lang_array[Tools::strtolower($key)]; - else - $msg = $params['s']; - return $msg; - } + return Translate::getModuleTranslation($params['mod'], $params['s'], Tools::substr(basename($filename), 0, -4)); // If the tpl is at the root of the template folder if (dirname($filename) == '.') @@ -119,5 +64,5 @@ function smartyTranslate($params, &$smarty) $class = null; } - return AdminController::translate($params['s'], $class, $addslashes, $htmlentities); + return Translate::getAdminTranslation($params['s'], $class, $addslashes, $htmlentities); } diff --git a/config/smartyfront.config.inc.php b/config/smartyfront.config.inc.php index ebf7b4969..eb510d0c2 100644 --- a/config/smartyfront.config.inc.php +++ b/config/smartyfront.config.inc.php @@ -29,7 +29,8 @@ $smarty->setTemplateDir(_PS_THEME_DIR_.'tpl'); function smartyTranslate($params, &$smarty) { - global $_LANG, $_MODULES, $cookie, $_MODULE, $_LANGPDF; + global $_LANG; + if (!isset($params['js'])) $params['js'] = 0; if (!isset($params['pdf'])) $params['pdf'] = false; if (!isset($params['mod'])) $params['mod'] = false; @@ -39,42 +40,9 @@ function smartyTranslate($params, &$smarty) $key = Tools::substr(basename($filename), 0, -4).'_'.md5($string); $lang_array = $_LANG; if ($params['mod']) - { - $iso = Language::getIsoById($cookie->id_lang); - - if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php')) - { - $translationsFile = _PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php'; - $key = '<{'.$params['mod'].'}'._THEME_NAME_.'>'.$key; - } - else - { - // @retrocompatibility with translations files in module root - if (Tools::file_exists_cache(_PS_MODULE_DIR_.$params['mod'].'/translations')) - $translationsFile = _PS_MODULE_DIR_.$params['mod'].'/translations/'.$iso.'.php'; - else - $translationsFile = _PS_MODULE_DIR_.$params['mod'].'/'.$iso.'.php'; - $key = '<{'.$params['mod'].'}prestashop>'.$key; - } - - if (!is_array($_MODULES)) - $_MODULES = array(); - if (@include_once($translationsFile)) - if(is_array($_MODULE)) - $_MODULES = array_merge($_MODULES, $_MODULE); - $lang_array = $_MODULES; - } + return Translate::getModuleTranslation($params['mod'], $params['s'], Tools::substr(basename($filename), 0, -4)); else if ($params['pdf']) - { - $iso = Language::getIsoById($cookie->id_lang); - $translationsFile = _PS_THEME_DIR_.'pdf/lang/'.$iso.'.php'; - - if (Tools::file_exists_cache($translationsFile)) - @include_once($translationsFile); - - $key = 'PDF'.md5($string); - $lang_array = $_LANGPDF; - } + return Translate::getPdfTranslation($params['s']); if (is_array($lang_array) && key_exists($key, $lang_array)) $msg = $lang_array[$key]; diff --git a/override/classes/Translate.php b/override/classes/Translate.php new file mode 100644 index 000000000..cb8406760 --- /dev/null +++ b/override/classes/Translate.php @@ -0,0 +1,7 @@ +