0

I'm using the Browser Sniff plugin to detect for the browser language on a multilangiage site using Transcribe plugin. It's working for detecting the browser language. I want to redirect users with their browser set to Chinese(zh) to the same url with "zh" as segment_1. So the site currently will display English at www.site.com/page and chinese at www.site.com/zh/page. Now I want to redirect users with their browser set to Chinese from www.site.com/page to www.site.com/zh/page.

I tried this, but it results in a redirect loop:

{exp:browser_sniff available_languages="en|zh"}
{if browser_sniff_negotiated_language == "zh"}
     {redirect="zh/{uri_string}"}
{/if}
{/exp:browser_sniff}

Any ideas?

2

The loop occurs because you're testing again and again and again...

Try this:

{exp:browser_sniff available_languages="en|zh"}
    {if browser_sniff_negotiated_language == "zh"}
         {if browser_sniff_negotiated_language != "{segment_1}"}
             {redirect="zh/{uri_string}"}
         {/if}
    {/if}
{/exp:browser_sniff}

update

Since Transcribe doesn't count the language for your segment_1, use {exp:transcribe:language_abbreviation}. If you're already using this same variable on your template and having parsing issues, add a different parameter to one of them or use the random parameter.

{exp:browser_sniff available_languages="en|zh"}
    {if browser_sniff_negotiated_language == "zh"}
         {if browser_sniff_negotiated_language != "{exp:transcribe:language_abbreviation random}"}
             {redirect="zh/{uri_string}"}
         {/if}
    {/if}
{/exp:browser_sniff}

EECMS template parser replaces all instances of a same variable by a placeholder text to prevent evaluate them more than once. Sometimes, it happens the instances are in different scopes and not all of them are parsed back. So, use the random parameter.

| improve this answer | |
  • Thanks Robson, unfortunately {segment_1} doesn't detect the language segment in the URL. For example, {segment_1} in this url site.com/zh/engineers returns as "engineers". Transcribe has a variable to return the language letters({exp:transcribe:language_abbreviation}), but when used within the browser_sniff plugin, it returns a string of numbers/letters..."M20o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr" instead of "zh". – Mike Lohrman Apr 16 '14 at 17:04
  • This is because your using {exp:transcribe:language_abbreviation} in different parse stages of different scopes. I gonna update my answer. – Robson Sobral Apr 16 '14 at 17:56
  • BOOOOOMMM!! Adding the random parameter to {exp:transcribe:language_abbreviation random} let it work correctly. I seriously thought I had hit a dead end on this. Thank you so much Robson! – Mike Lohrman Apr 16 '14 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.