Jump to content

Recommended Posts

Hi,

 

There are some places in the code that are neither modules nor Smarty templates that may require translations, for example, the php classes under /classes. In this case the $this->l() function may not be used, although it can be used if the class extends the Module class, because PrestaShop won't recognise it as a module and hence won't recognise its translations. PrestaShop only take as modules the classes under /modules and under /themes/YOUR_THEME/modules. Thus, if the $this->l() function is used, the string name will always be used and there will be no possibility to assign a translation to that variable.

 

I've developed a workaround to make available translations everywhere. It is done in three steps:

 

1 - Create a getExtraTranslation() function in Tools.php.

 

2 - Create a extratranslations module under /themes/YOUR_THEME/modules.

 

3 - Use the Tools::getExtraTranslation() anywhere.

 

Explanation:

 

1 - Create a getExtraTranslation() function in Tools.php.

 

The function goes in Tools.php and is this:

 

/**
* Get an extra translation, not in modules or anywhere else
*
* @param string $string String to translate
* @return string Translation
*/
static public function getExtraTranslation($string)
{
	global $_EXTRA_TRANSLATIONS, $cookie;

	if ( ! $_EXTRA_TRANSLATIONS) {

		$id_lang = (!isset($cookie) OR !is_object($cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($cookie->id_lang);

		$translationsFile = _PS_THEME_DIR_.'modules/extratranslations/'.Language::getIsoById($id_lang).'.php';

		if (self::file_exists_cache($translationsFile) AND include_once($translationsFile))
			$_EXTRA_TRANSLATIONS = $_MODULE;
	}

	$string2 = str_replace('\'', '\\\'', $string);
	$currentKey = '<{extratranslations}'._THEME_NAME_.'>extratranslations_'.md5($string2);

	if (is_array($_EXTRA_TRANSLATIONS) && key_exists($currentKey, $_EXTRA_TRANSLATIONS))
		$msg = stripslashes($_EXTRA_TRANSLATIONS[$currentKey]);
	else
		$msg = $string;

	return str_replace('"', '"', $msg);

}

 

 

NOTE: The global variable $_EXTRA_TRANSLATIONS may be initialised in /config/config.inc.php like this:

 

global $_EXTRA_TRANSLATIONS;
$_EXTRA_TRANSLATIONS = array();

 

 

2 - Create a extratranslations module under /themes/YOUR_THEME/modules.

 

A Smarty template called extratranslations.tpl must be created under /themes/YOUR_THEME/modules/extratranslations/.

 

In this template will be created the extra translations that are used anywhere. For example:

 

{l s='Voucher code:'}
{l s='Total discounts'}
{l s='No message'}

 

Then, PrestaShop will recognise them as module translations and they will be available at back office when the module translations are updated. Here their values can be set.

 

 

3 - Use the Tools::getExtraTranslation() anywhere.

 

Wherever the extra translation is to be used, it will be done with the Tools::getExtraTranslation() function, like this:

 

echo Tools::getExtraTranslation('Voucher code:');

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...