// Refacto of lang functions (all code moved in Translate class)

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@13262 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2012-02-13 15:20:25 +00:00
parent 6a68b754ce
commit 7682d57803
9 changed files with 210 additions and 230 deletions
+1 -1
View File
@@ -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;
+9 -54
View File
@@ -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('"', '&quot;', $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);
}
/*
+181
View File
@@ -0,0 +1,181 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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('"', '&quot;', ($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('"', '&quot;', $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('"', '&quot;', $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;
}
}
+1 -67
View File
@@ -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.'<br />';
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('"', '&quot;', ($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);
}
/**
+2 -12
View File
@@ -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('"', '&quot;', ($addslashes ? addslashes($str) : stripslashes($str)));
return Translate::getAdminTranslation($string, get_class($this), $addslashes, $htmlentities);
}
/**
+4 -59
View File
@@ -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);
}
+4 -36
View File
@@ -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];
+7
View File
@@ -0,0 +1,7 @@
<?php
class Translate extends TranslateCore
{
}
@@ -1,6 +1,6 @@
<?php
class ModuleAdminController extends ModuleAdminControllerCore
abstract class ModuleAdminController extends ModuleAdminControllerCore
{
}