Post by cheeseus » Tue Apr 28, 2015 5:21 am

If your OpenCart website uses only two languages and you don't want to use the drop-down menu switcher, here's a nice little trick to edit it to only show the other available language, so that you click directly on it.

In my case, I have Bulgarian and English. When the site is loaded in Bulgarian, the language switcher shows the English icon and text - and vice versa. This makes it easier to switch languages at a single click.

What you need to do: EDIT the language template in /catalog/view/theme/default/template/common/language.tpl

Original language.tpl:

Code: Select all

<?php if (count($languages) > 1) { ?>
<div class="pull-left">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language">
  <div class="btn-group">
    <button class="btn btn-link dropdown-toggle" data-toggle="dropdown">
    <?php foreach ($languages as $language) { ?>
    <?php if ($language['code'] == $code) { ?>
    <img src="image/flags/<?php echo $language['image']; ?>" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>">
    <?php } ?>
    <?php } ?>
    <span class="hidden-xs hidden-sm hidden-md"><?php echo $text_language; ?></span> <i class="fa fa-caret-down"></i></button>
    <ul class="dropdown-menu">
      <?php foreach ($languages as $language) { ?>
      <li><a href="<?php echo $language['code']; ?>"><img src="image/flags/<?php echo $language['image']; ?>" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></a></li>
      <?php } ?>
    </ul>
  </div>
  <input type="hidden" name="code" value="" />
  <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</form>
</div>
<?php } ?>
EDITED language.tpl:

Code: Select all

<?php if(count($languages) > 1) : ?>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language">
	<div class="btn-group">
		<?php foreach($languages as $language) : ?>
		<?php if($language['code'] != $code) : ?>
			<a href="<?php echo $language['code']; ?>">
				<img src="image/flags/<?php echo $language['image']; ?>" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>"> <span class="hidden-xs hidden-sm hidden-md"><?php echo $language['name']; ?></span>
			</a>
		<?php endif; ?>
		<?php endforeach; ?>
	</div>
	<input type="hidden" name="code" value="" />
	<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</form>
<?php endif; ?>
You can simply rename the existing language.tpl file to language_old.tpl, then copy the code from above and paste it into a blank text file. Save the new file as language.tpl.

That's it!

New member

Posts

Joined
Sat Apr 18, 2015 3:59 am

Post by Gatak » Fri Jul 24, 2015 1:10 pm

Another option is to list all languages available next to each other instead of using a toggle:

Code: Select all

<?php if (count($languages) > 1) { ?>
<div id="top-links" class="nav pull-left">
	<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language">
			<?php foreach ($languages as $language) { ?>
				<a href="<?php echo $language['code']; ?>"><img src="image/flags/<?php echo $language['image']; ?>" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" />&nbsp;<?php echo $language['name']; ?></a>
			<?php } ?>
		<input type="hidden" name="code" value="" />
		<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
	</form>
</div>
<?php } ?>

Newbie

Posts

Joined
Wed May 06, 2015 9:34 pm

Post by vaguemind » Tue Aug 09, 2016 1:24 am

Hello

i implemented this code but it repeats the language name twice and when press on it to switch the language , it redirects to 404 Not Found error page ?

please advise

here is a screenshot:

Image

New member

Posts

Joined
Wed Jul 27, 2016 7:03 pm

Post by vaguemind » Tue Aug 09, 2016 5:27 pm

No answer please ??

New member

Posts

Joined
Wed Jul 27, 2016 7:03 pm

Post by vaguemind » Sat Sep 10, 2016 7:28 am

No one can help me here please ?

New member

Posts

Joined
Wed Jul 27, 2016 7:03 pm

Post by vaguemind » Thu Sep 29, 2016 5:09 pm

No Answer here !

New member

Posts

Joined
Wed Jul 27, 2016 7:03 pm

Post by cmac » Fri Oct 21, 2016 3:18 am

Hi. Probably You are using a newer OpenCart.

I notced for example that he img link and the href link are different.

for me changing the href to:

<a href="javascript:;" onclick="$('input[name=\'code\']').attr('value', '<?php echo $language['code']; ?>'); $('#language_form').submit();">

and the img to

<img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" width="16px" height="11px" />

did the trick for displaying two side by side languages with flags only.

Newbie

Posts

Joined
Fri Oct 21, 2016 3:15 am

Post by vaguemind » Sun Oct 23, 2016 2:52 pm

cmac wrote:Hi. Probably You are using a newer OpenCart.

I notced for example that he img link and the href link are different.

for me changing the href to:

<a href="javascript:;" onclick="$('input[name=\'code\']').attr('value', '<?php echo $language['code']; ?>'); $('#language_form').submit();">

and the img to

<img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" width="16px" height="11px" />

did the trick for displaying two side by side languages with flags only.
I tried this , it show two language options with flags but when click on it nothing happens !
and i need to show the other language only .. so if i am now on the english home .. and we have another arabic language so shows the link to Arabic only and vice versa .

THanks

New member

Posts

Joined
Wed Jul 27, 2016 7:03 pm

Post by abargoil » Thu Dec 01, 2016 3:16 am

I can't get this to work on OC 2.2 it seems the edit goes to yoursite.com/en-gb or whatever the language is but oc 2.2 doesn't do that so how do I change it? Please help need this fixed asap!

the original language.tpl is as follows

Code: Select all

<?php if (count($languages) > 1) { ?>
<div class="pull-left">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-language">
  <div class="btn-group">
    <button class="btn btn-link dropdown-toggle" data-toggle="dropdown">
    <?php foreach ($languages as $language) { ?>
    <?php if ($language['code'] == $code) { ?>
    <img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>">
    <?php } ?>
    <?php } ?>
    <span class="hidden-xs hidden-sm hidden-md"><?php echo $text_language; ?></span> <i class="fa fa-caret-down"></i></button>
    <ul class="dropdown-menu">
      <?php foreach ($languages as $language) { ?>
      <li><button class="btn btn-link btn-block language-select" type="button" name="<?php echo $language['code']; ?>"><img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></button></li>
      <?php } ?>
    </ul>
  </div>
  <input type="hidden" name="code" value="" />
  <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</form>
</div>
<?php } ?>

Newbie

Posts

Joined
Sat Oct 08, 2016 5:04 am

Post by abargoil » Sat Dec 03, 2016 3:36 am

cmac wrote:Hi. Probably You are using a newer OpenCart.

I notced for example that he img link and the href link are different.

for me changing the href to:

<a href="javascript:;" onclick="$('input[name=\'code\']').attr('value', '<?php echo $language['code']; ?>'); $('#language_form').submit();">

and the img to

<img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" width="16px" height="11px" />

did the trick for displaying two side by side languages with flags only.
I have tried this for the link and it did not work. I am running OC 2.2. I get the attached 404 error when I click a flag. any help would be awesome!

my language.tpl is as follows:

Code: Select all

<?php if (count($languages) > 1) { ?>
<div id="top-links" class="nav pull-left">
   <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language">
         <?php foreach ($languages as $language) { ?>
            <a href="<?php echo $language['code']; ?>"><img src="catalog/language/<?php echo $language['code']; ?>/<?php echo $language['code']; ?>.png" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" width="16px" height="11px" /></a>
         <?php } ?>
      <input type="hidden" name="code" value="" />
      <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
   </form>
</div>
<?php } ?>

Attachments

language link not working.png

404 error - language link not working.png (58.53 KiB) Viewed 9096 times


Newbie

Posts

Joined
Sat Oct 08, 2016 5:04 am

New member

Posts

Joined
Sat Oct 18, 2014 6:45 pm

Who is online

Users browsing this forum: No registered users and 9 guests