MODx: How to redirect a visitor based on browser language?

Some of the clients I work(ed) for have multi langual websites, therefore I created a solution to redirect the visitor to the right language on the website. I did solve this problem by inspecting the language of the browser from the visitor.

The first step is to create your structure in MODx. In this example we have to languages and to accomplish that I set up this structure:

  • languageforward (1)
  • nl (2)
    • Welkom (3)
    • Diensten (4)
    • Contact (5)
  • en (6)
    • Welcome (7)
    • Services (8)
    • Contact (9)

You have to place the FirstChildRedirect snippet on the pages "nl" and "en" by entering it into the content field. This snippet redirects to the first page in the folder. You don't need to install the snippet, it is shipped with the MODx download.

Now we create a new snippet to get the browser language and redirect the visitor to the page. Let's call this snippet "languageforwarder" and put this into it:

  1. <?php
  2. $lang = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'],",");
  3.     while ($lang)
  4.     {
  5.          //check if the language is dutch
  6.          if (strstr($lang,"nl"))
  7.          {
  8.               //redirect the user to the dutch pages
  9.               header ("location: ".$modx->makeUrl(2, '', '', 'full'));
  10.               exit;
  11.           }
  12.           //check if the language is english
  13.           if (strstr($lang,"en"))
  14.           {
  15.               //redirect the user to the english pages
  16.               header ("location: ".$modx->makeUrl(6, '', '', 'full'));
  17.               exit;
  18.           }
  19.         // etc..
  20.                  
  21.          $lang = strtok(",");
  22.      }
  23.      
  24.      // no defined language found, just go to the english pages
  25.     header ("location: ".$modx->makeUrl(6, '', '', 'full'));
  26.     exit;
  27. ?>

The number in the $modx->makeUrl function is the id of the folder that contains the pages for that language. All what's left to do is calling this snippet from the site_start page, which is "languageforward (1)" in this example.

Now the visitor should be redirect to the right language if you have done everything right.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code:
 
Showing comments 1 to 4 of 5 | Next | Last
Shane

Posts: 4
Comment
Re: MODx: How to redirect a visitor based on browser language?
Reply #1 on : Fri June 19, 2009, 16:13:30
Very cool post. Just wondering how this will behave if the user is using one language but wants to visit the other languages...will the snippet allow this or will it always redirect to the browser language?
ronaldlokers

Posts: 2
Comment
Re: MODx: How to redirect a visitor based on browser language?
Reply #2 on : Fri June 19, 2009, 16:27:27
You will be able to visit the other languages because the snippet is only placed on the very first page of the site. After that you can visit every page you want without problems.

http://www.hoteldeborgh.nl/ is one of the sites that uses this snippet. So check it out to see how it works. You can switch between the languages by clicking on one of the flags in the top right of the site.
man

Posts: 4
Comment
Re: MODx: How to redirect a visitor based on browser language?
Reply #3 on : Thu October 22, 2009, 12:58:00
Thanks for this useful snippet.
Frank Los

Posts: 4
Comment
redirect to user created page
Reply #4 on : Mon February 08, 2010, 12:03:17
Hi I am trying this code from dimmy but want to chabge his last snippet to not show a click to go to a page, but want a redirect. Tis is the snippet:

<?php
if($modx->config[login_home]){
return "<a href='' title='to your pages' style='add some style to your link'>Your Pages</a>";
}
?>

Can I use the makeUrl with something of a var (eg $alias), if so, how.

Here s the thread where the code came from:

http://modxcms.com/forums/index.php/topic,25087.120.html
Showing comments 1 to 4 of 5 | Next | Last
 

Latest tweets

  • Geweldig hoe coolblue je op de hoogte houd van je bestelling. Zo zouden alle webshops moeten zijn :)Yesterday
  • Just installed twitter for iPad, what a great interface does it have!2 days ago

Latest poll

What will be the trend of 2010?