We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29597 ☆ A M B ☆
    • 350 Posts
    Hello MODXers!

    My client would like to have visitors to the site to get redirected based on the users browser language...
    What would be best is to try to detect browser language: if German, French or Italian go to the translation, else go to default English...

    Can anybody help on how to achieve this?

    Website layout is:

    The current setup is:

    • MODX Revolution 2.2.2-pl (traditional)
    • Babel2.2.5-pl

    .htaccess file:
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|fr|de|it)/favicon.png$ favicon.png [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|fr|de|it)/assets(.*)$ assets$2 [L,QSA]
    RewriteRule ^en/iphone/info6html en/iphone/info6.html [L]
    RewriteRule ^de/iphone/info.html3 de/iphone/info3.html [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(en|fr|de|it)?(\/)?(.*)$ index.php?c=$1&q=$3 [L,QSA]
    
    


    Gateway plugin:
    <?php
    //make sure the plugin does not run on the mgr context (the manager)
      if ( $modx->context->get( 'key' ) != 'mgr') {
        /**
         * grab the current context from the request "c" parameter (or context_param_alias from system settings)
         * the alternative .htaccess friendly URL rewrite rule must be activated
         */
        $cntxt_param = $modx->getOption('context_param_alias', null, 'c');
        if ( isset( $_REQUEST[$cntxt_param] ) && $_REQUEST[$cntxt_param] != '' ) {
          switch ( $_REQUEST[$cntxt_param] ) {
              
            case 'en':
              //switch the context
              $modx->switchContext('web');
              //set the cultureKey
              $modx->setOption('cultureKey', 'en');
              break;
              
            case 'fr':
              $modx->switchContext('fr');
              $modx->setOption('cultureKey', 'fr');
              break;
              
           case 'de':
              $modx->switchContext('de');
              $modx->setOption('cultureKey', 'de');
              break;
    
           case 'it':
              $modx->switchContext('it');
              $modx->setOption('cultureKey', 'it');
              break;
              
            default:
              // Set the default language/context here
              break;
          }
        }
      }
    
    [ed. note: ThaClown last edited this post 13 years, 8 months ago.]
      MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
      DESIGNfromWITHIN, MPThemes and Any Screen Size
      Follow me on Twitter | Read my blog | My code on GitHub
      • 18608
      • 112 Posts
      You could do it in the .htaccess file with this: http://www.htaccesstools.com/redirection-by-language/

      I would consider doing by IP instead (there are some services for that). Not everyone has their browser in the same language as they are in (mine is in english, even though i'm in Denmark).
        Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
        • 29597 ☆ A M B ☆
        • 350 Posts
        Thanks for the link, how do I set this up for multiple languages?
        I do not think my client will want the IP based redirection... A visitor in a hotel in Japan will see a Japanese site... right?

        What would be best is to try to detect browsers language, if German, French or Italian go to the translation, else go to default English...

          MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
          DESIGNfromWITHIN, MPThemes and Any Screen Size
          Follow me on Twitter | Read my blog | My code on GitHub
          • 18608
          • 112 Posts
          To answer the IP part( I don't see anymore):
          I have heard of this before, but never used it myself:
          http://dev.maxmind.com/geoip/javascript

          Here is at little tut on the the use:
          http://www.cpafix.com/f2/%5Btut-code%5D%5Bget%5D-free-geo-target-redirect-script-%5Bnoob-friendly%5D-2981/

          The use in .htaccess:
          I agree on english per default. Think someone more skilled at writing .htaccess should advise you on your if statement.
            Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
            • 18608
            • 112 Posts
            Just thought of another option: You could use the
            $_SERVER['HTTP_ACCEPT_LANGUAGE']
            variable in your gateway plugin, that might be easier.
              Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
              • 19604 ☆ A M B ☆
              • 179 Posts
              You could also consider to evaluate the language defaults set inside the browser when someone is calling just "http://www.somesite.com"
              $_SERVER['HTTP_ACCEPT_LANGUAGE'];
                MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
                http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver
                • 18608
                • 112 Posts
                Played with it, think you could do it like this in your gateway plugin (untested):
                <?php
                //make sure the plugin does not run on the mgr context (the manager)
                  if ( $modx->context->get( 'key' ) != 'mgr') {
                  
                	  switch ( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) {
                	       
                	      case 'fr':
                	      $modx->switchContext('fr');
                	      $modx->setOption('cultureKey', 'fr');
                	      break;
                	       
                	   case 'de':
                	      $modx->switchContext('de');
                	      $modx->setOption('cultureKey', 'de');
                	      break;
                	
                	   case 'it':
                	      $modx->switchContext('it');
                	      $modx->setOption('cultureKey', 'it');
                	      break;
                	       
                	    default:
                	      $modx->switchContext('web');
                	      //set the cultureKey
                	      $modx->setOption('cultureKey', 'en');
                	      break;
                	  }
                  }
                  Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
                  • 29597 ☆ A M B ☆
                  • 350 Posts
                  Thanks, but I need the:
                  $cntxt_param = $modx->getOption('context_param_alias', null, 'c');
                      if ( isset( $_REQUEST[$cntxt_param] ) && $_REQUEST[$cntxt_param] != '' ) {
                        switch ( $_REQUEST[$cntxt_param] ) {
                  

                  Right?
                    MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
                    DESIGNfromWITHIN, MPThemes and Any Screen Size
                    Follow me on Twitter | Read my blog | My code on GitHub
                    • 18608
                    • 112 Posts
                    Quote from: ThaClown at Jan 25, 2013, 05:36 AM
                    Thanks, but I need the:
                    $cntxt_param = $modx->getOption('context_param_alias', null, 'c');
                        if ( isset( $_REQUEST[$cntxt_param] ) && $_REQUEST[$cntxt_param] != '' ) {
                          switch ( $_REQUEST[$cntxt_param] ) {
                    

                    Right?

                    I don't think so. I'm using the $_SERVER['HTTP_ACCEPT_LANGUAGE'] instead of the $_REQUEST[$cntxt_param]. And the above just checks to see if $_REQUEST[$cntxt_param] is set. I don't think the $_REQUEST[$cntxt_param] is serving any other purpose, but I can't rule out that i'm missing something...

                    Have you tested it?
                      Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
                      • 19604 ☆ A M B ☆
                      • 179 Posts
                      Quote from: MathiasD at Jan 25, 2013, 05:19 AM
                      Just thought of another option: You could use the
                      $_SERVER['HTTP_ACCEPT_LANGUAGE']
                      variable in your gateway plugin, that might be easier.
                      That was also my suggestion. It's good in case someone calls the website without a language suffix. In other cases, the URL path evaluation should take over.
                      For my multi language website I did not use any contexts at all, just different language branches, like "de","en","zh". Since there were no direkt 1:1 translations needed, I did not have to use language-link-references (Babal does this quite well). All the cross-cultural texts are inside a lexicon. Also the different IDs to change getResources calls and stuff.
                      This is it "in the wild": http://www.inbs-online.com/ Try to change the language priorities inside the browser and you will get a different landing page.
                      This is my plugin-code firing on "OnInitCulture":
                      <?php
                      // get lang from URI
                      $uri= ($_SERVER['REQUEST_URI']);
                      $langFromUri = substr($uri, 1, 2);
                      switch ($langFromUri) {
                        case 'zh':
                          //set the cultureKey
                          $modx->setOption('cultureKey', 'zh');
                          break;
                        case 'en':
                          //set the cultureKey
                          $modx->setOption('cultureKey', 'en');
                          break;
                        case 'de':
                          //set the cultureKey
                          $modx->setOption('cultureKey', 'de');
                          break;
                        default:
                          // detect language and call corresponding start-page
                          $langPrefered = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
                          switch ($langPrefered) {
                            case 'en':
                              $targetId = 2;
                              break;
                            case 'zh':
                              $targetId = 3;
                              break;
                            default:
                              $targetId = 1;
                              $langPrefered="de";
                              break;
                          }
                          //set the cultureKey
                          $modx->setOption('cultureKey', $langPrefered);
                          //call correct start page
                          $url=$modx->makeUrl($targetId);
                          $modx->sendRedirect($url);	
                      
                        break;
                      }
                      
                      return;
                      
                      [ed. note: mindeffects last edited this post 13 years, 8 months ago.]
                        MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
                        http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver