Jump to content

Custom language link


asdqwerty

Recommended Posts

Hi

 

I'm wondering how to make a link change language and go back to the same page.

 

This is a current link: 

<a href="index.php?id_lang=1"><img src="{$img_dir}flags/eng.png"></a>

As you see it simply links to the homepage, is there a better way to make it stay on the same page as when the link was clicked?

 

Regards

Link to comment
Share on other sites

 

you need to use link object, then you will be properly redirected to page, where you were

$link->getLanguageLink($language.id_lang)|escape:htmlall}

 

Thanks thats almost it!

 

Is there a way I can specify a language the link goes to?

Or is there a way for me to just link it like this: <current page link>?id_lang=2

 

Thanks

Link to comment
Share on other sites

  • 7 months later...

Old Post but this worked great for me, thanks vekia.

<a href="{$link->getLanguageLink(1)}" title="{l s='English' mod='blockpermanentlinks'}">{l s='English' mod='blockpermanentlinks'}</a>
<a href="{$link->getLanguageLink(2)}" title="{l s='Français' mod='blockpermanentlinks'}">{l s='Français' mod='blockpermanentlinks'}</a>

With friendly URLS on or off.

Link to comment
Share on other sites

PS 1.5.6.2

 

I really don't get why it doesn't like cache. With cache off, $link->getLanguageLink(2) works perfectly. I can be on any English page, click the link and see the French version. But when cache is on this function simply gets the first link used in a session and than continues to use the first link regardless of page.

 

I spent several hours yesterday trying to make a link that simply return the same page you are on in a specified language.

 

$link->getLanguageLink(2) is the only one I can get to work properly but only if I turn off cache.

Link to comment
Share on other sites

In the end I went with just copying the code from the language block and adding in the markup. It actually is a better solution because the link only shows in the language of context.

 

So for someone else that may want this here is the code from my Permanent Links Mod.

<!-- Block permanent links module HEADER -->
<section class="header-box blockpermanentlinks-header">
    <ul id="header_links">
        <li><a href="{$link->getPageLink('index.php')}" class="header_links_home"><i class="icon-circle-arrow-right"></i> {l s='Home' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_sitemap"><a href="{$link->getPageLink('sitemap')}" title="{l s='sitemap' mod='blockpermanentlinks'}"><i class="icon-circle-arrow-right"></i> {l s='sitemap' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_contact"><a href="{$link->getPageLink('contact', true)}" title="{l s='contact' mod='blockpermanentlinks'}"><i class="icon-circle-arrow-right"></i> {l s='Contact' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_bookmark">
            <script type="text/javascript">writeBookmarkLink('{$come_from}', '{$meta_title|addslashes|addslashes}', '<i class="icon-circle-arrow-right"></i> {l s='Bookmark' mod='blockpermanentlinks' js=1}');</script>
        </li>
      {foreach from=$languages key=k item=language name="languages"}
   <li {if $language.iso_code == $lang_iso}class="selected_language"{/if}>
   {if $language.iso_code != $lang_iso}
    {assign var=indice_lang value=$language.id_lang}
    {if isset($lang_rewrite_urls.$indice_lang)}
  <li id="header_link"><a href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}"><i class="icon-circle-arrow-right"></i> {$language.name}</a></li>
    {else}
 <li id="header_link"><a href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}"><i class="icon-circle-arrow-right"></i> {$language.name}</a></li>
    {/if}
   {/if}
 </li>
 {/foreach}
 </ul>
Edited by Bill Dalton (see edit history)
Link to comment
Share on other sites

I can use all code above with cache off, but if I turn cache on the links stop working after a few clicks.

 

I even copied the exact code from Language Block and pasted it into the Permanent Links .tpl, and even that code will not work when the cache is on, even though the same code runs perfectly in the Language Block .tpl.

 

Here is the site with cache on.

http://scrubscanada.ca/scpsmay14/en/med-couture/38-med-couture-ez-flex-sport-neckline-top.html

 

The language link in Permanent Links will work when you get to the page, but in just a few different pages it begins to send you to an incorrect page. I don't know why.

Link to comment
Share on other sites

The fix turned out to be nice and simple. Something that I'm sure will prove useful now that I know. Smarty lets you use {nocache} {/nocache} and you can use it at line level for very fine control. Good read here if interested, http://www.smarty.net/docs/en/caching.cacheable.tpl

 

Using {nocache} all of the above code works, but in the end I like this version best, same as above but with {nocache} tags.

<!-- Block permanent links module HEADER -->
<section class="header-box blockpermanentlinks-header">
    <ul id="header_links">
        <li><a href="{$link->getPageLink('index.php')}" class="header_links_home"><i class="icon-circle-arrow-right"></i> {l s='Home' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_sitemap"><a href="{$link->getPageLink('sitemap')}" title="{l s='sitemap' mod='blockpermanentlinks'}"><i class="icon-circle-arrow-right"></i> {l s='sitemap' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_contact"><a href="{$link->getPageLink('contact', true)}" title="{l s='contact' mod='blockpermanentlinks'}"><i class="icon-circle-arrow-right"></i> {l s='Contact' mod='blockpermanentlinks'}</a></li>
        <li id="header_link_bookmark">
            <script type="text/javascript">writeBookmarkLink('{$come_from}', '{$meta_title|addslashes|addslashes}', '<i class="icon-circle-arrow-right"></i> {l s='Bookmark' mod='blockpermanentlinks' js=1}');</script>
        </li>
        {nocache}
      {foreach from=$languages key=k item=language name="languages"}
   <li {if $language.iso_code == $lang_iso}class="selected_language"{/if}>
   {if $language.iso_code != $lang_iso}
    {assign var=indice_lang value=$language.id_lang}
    {if isset($lang_rewrite_urls.$indice_lang)}
	 <li id="header_link"><a href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}"><i class="icon-circle-arrow-right"></i> {$language.name}</a></li>
    {else}
	<li id="header_link"><a href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}"><i class="icon-circle-arrow-right"></i> {$language.name}</a></li>
    {/if}
   {/if}
 </li>
 {/foreach}
 {/nocache}
 </ul>
Link to comment
Share on other sites

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...