larin555
larin555

Reputation: 1699

Change logo on language change Opencart

I am trying to make the main logo of the store change when I change language. My company has a different name in english (originally in French).

Here's the code I have :

I have changed (in catalog/view/theme/yourtheme/template/common/header.tpl)

 <?php if ($logo) { ?>
  <div id="logo"><a href="<?php echo $home; ?>"><img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" /></a></div>
  <?php } ?>

For :

<?php
if($lang == 'fr'){
$logo = 'image/data/Lg_Axesoirs_Blanc_FR_PNG.png';
} elseif($lang == 'en'){
$logo = 'image/data/Lg_Axesoirs_Blanc_EN_PNG.png';
}
?> 
<?php if ($logo) { ?>
<div id="logo"><a href="<?php echo $home; ?>"><img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" /></a></div>
<?php } ?>

But it's not working, the logo does not change, but I don't understand what's wrong in my code.

Thanks!

Upvotes: 0

Views: 5488

Answers (3)

shadyyx
shadyyx

Reputation: 16055

Here's my solution that should work:

Open up Your controller catalog\controller\common\header.php and find this line of code:

$this->data['logo'] = $server . 'image/' . $this->config->get('config_logo');

(should be line 30 if OC version 1.5.5.1) and comment this line out (add // before the line).

Now right under this commented line (should stay within the if statement) add this:

$this->load->model('localisation/language');
$language = $this->model_localisation_language->getLanguage($this->config->get('config_language_id'));
$logo = $server . 'image/' . $language['code'] . '/' . $this->config->get('config_logo');
$this->data['logo'] = file_exists($logo) ? $logo : $server . 'image/' . $this->config->get('config_logo');

By this code You set up a path to Your logo image like this

  • .../image/EN/data/Lg_Axesoirs_Blanc.png
  • .../image/FR/data/Lg_Axesoirs_Blanc.png

Make sure that in Your admin You set the logo like You normally should (so it should be stored in image/data/) folder and then manualy copy the english/french copy to the folder image/EN/data or image/FR/data - note that the image name must be the same. The code should test whether the file exists in that directory and pick up the default one if it does not.

The code in Your header template file should remain the same.

Upvotes: 0

Jay Gilford
Jay Gilford

Reputation: 15151

The problem with your code is that $lang does not contain the language of the currently selected language. This is stored in $this->config->get('config_language'); in index.php just before

$language = new Language($languages[$code]['directory']);

if you are curious to see how. You can simply amend your code to the following which should work

<?php
$lang = $this->config->get('config_language'); // ADDED LINE
if($lang == 'fr'){
    $logo = 'image/data/Lg_Axesoirs_Blanc_FR_PNG.png';
} elseif($lang == 'en'){
    $logo = 'image/data/Lg_Axesoirs_Blanc_EN_PNG.png';
}
?> 
<?php if ($logo) { ?>
<div id="logo"><a href="<?php echo $home; ?>"><img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" /></a></div>
<?php } ?>

Alternatively assuming your original logo set was

image/data/Lg_Axesoirs_Blanc_FR_PNG.png

You could just use

<?php
$logo = str_replace('_FR_', '_' . strtoupper($this->config->get('config_language')) . '_', $logo);
?> 
<?php if ($logo) { ?>
<div id="logo"><a href="<?php echo $home; ?>"><img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" /></a></div>
<?php } ?>

Which will allow you to use the two letter prefix automatically for each new language logo

Note that none of the code above has been tested but should work in theory

Upvotes: 1

Adam Nicholson
Adam Nicholson

Reputation: 320

Go into the admin of OpenCart to the languages section and edit a language. Look in the URL for something like language_id=1. Make a note of the language ID for each language.

In your code, put the following:

<?php

// $this->config->get('config_language_id') returns the language ID currently active for the session

switch ($this->config->get('config_language_id')) {

 case 1 : // Replace the "1" with your English language ID
   // The English logo
   $logo = 'image/data/Lg_Axesoirs_Blanc_EN_PNG.png';
   break;

 case 2 : // Replace the "2" with your French language ID
   // The French logo
   $logo = 'image/data/Lg_Axesoirs_Blanc_FR_PNG.png';
   break;

 default :
    // The default logo if none of the above cases match
   $logo = 'image/data/Lg_Axesoirs_Blanc_EN_PNG.png';

}
?>

<?php if ($logo) { ?>
<div id="logo">
   <a href="<?php echo $home; ?>">
       <img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" />
   </a>
</div>
<?php } ?>

As a note, this logic should really be in a controller rather than in a template view file. OpenCart doesn't let you extend core controllers so don't worry too much about putting it in the view, but if you want to do it properly have a look at something like vQmod to insert it into the controller.

(Slightly off topic; there's a vQmod-ish-type-thing built in to an upcoming version of OpenCart which should hopefully let you edit the controllers without being dirty and downloading vQmod - but I couldn't say how long it'll be till that's released).

Hope that helps!

Upvotes: 0

Related Questions