We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18608
    • 112 Posts
    I think mindeffects solution is a bit more elegant. If the user has already requested a specific language (i.e. they written somesite.com/FR) they should go to that language no matter what, but if they haven't selected their language setting should be applied. If you combine it I think this should work:

    <?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:
          	  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
      • 19604 ☆ A M B ☆
      • 179 Posts
      Quote from: MathiasD at Jan 25, 2013, 06:28 AM
      I think mindeffects solution is a bit more elegant. If the user has already requested a specific language (i.e. they written somesite.com/FR) they should go to that language no matter what, but if they haven't selected their language setting should be applied. If you combine it I think this should work:
      Thank you! ;-)
      But that is just how my code works: See, if there is a requested language in the URI and if not, look what the browser tells you and reakt. Of nothing works, pick a default.
        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
        Thank you! ;-)
        But that is just how my code works: See, if there is a requested language in the URI and if not, look what the browser tells you and reakt. Of nothing works, pick a default.
        You're right - I didn't read your code properly - was trying to make it applicable to Mienno's case, but it already is smiley
          Mathias Dannevang | Webdesigner at dannevang.org | Tweets @dannevang
          • 37105
          • 194 Posts
          Just curious. Why set the cultureKey in a script? In my opinion it is easier and better to set this option on context level. Also the set_locale option could be set on the context itself.
            Codeplaza Webdesign: for professional websites at low cost
            • 19604 ☆ A M B ☆
            • 179 Posts
            Quote from: donquicky at Jan 25, 2013, 07:50 AM
            Just curious. Why set the cultureKey in a script? In my opinion it is easier and better to set this option on context level. Also the set_locale option could be set on the context itself.
            Not sure, who your are asking... ;-) Anyway. I didn't use contexts in my example. I was perfectly happy with "the language in the tree" solution – since I did not use Babel but my own scripts. I wanted to have independent language branches, without a 1:1 realtion.
            The lexicon needs the culture key. I use the lexicon because it's the place where the language related stuff seems to be at home (at least in my small world ;-) ). A lexicon is way easier to edit than a context because you edit a single file with ALL the settings: texts, IDs, chunk- and snippet-calls, just every type of code. I have about 60 entries per language in the lexicon.
            Need a new language? Copy the folder (in the filesystem), rename it, make some quick translation, done. Try to duplicate a context WITH ALL settings but WITHOUT ALL resources...
            Sure, you could store all the settings in a context setting. MODX leaves you the choice.
              MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
              http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver
              • 37105
              • 194 Posts
              I was just adding some remarks on the use of setting the cultureKey used in the scripts in this topic each time a page wil be loaded while it can be so much easier to handle this setting as option on the context.

              {:o)
                Codeplaza Webdesign: for professional websites at low cost
                • 19604 ☆ A M B ☆
                • 179 Posts
                Quote from: donquicky at Jan 25, 2013, 09:17 AM
                I was just adding some remarks on the use of setting the cultureKey used in the scripts in this topic each time a page wil be loaded while it can be so much easier to handle this setting as option on the context.
                {:o)
                The performance shouldn't be that much worse with this custom plugin. But that is indeed the price I have to pay for the lexicon usage.
                  MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
                  http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver
                  • 29597 ☆ A M B ☆
                  • 350 Posts
                  Thanks so much guys it work now great!

                  One more thing I would like to ask. My client want so send out email with links like:
                  http://mysite.com/promo/

                  I am using thye Redirector extra to redirect user that go to:
                  http://mysite.com/promo/
                  to go to:
                  http://mysite.com/[[!++cultureKey]]/promo.html

                  But this will always go to the default English...

                  Any more brilliant tips?
                    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
                    • 19604 ☆ A M B ☆
                    • 179 Posts
                    Quote from: DESIGNfromWITHIN
                    Thanks so much guys it work now great!
                    Thank you very much, Sir, for your kind words.
                    Quote from: DESIGNfromWITHIN

                    http://mysite.com/[[!++cultureKey]]/promo.html
                    But this will always go to the default English...
                    What does the generated URL look like? Does it contain the "xy" in "...com/xy/promo.html"?
                    Does Redirector evaluate system setting?
                      MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
                      http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver
                      • 37105
                      • 194 Posts
                      I'am also afraid that it evaluates the system setting. In your script the cultureKey is set based on language served. Consider setting cultureKey by default on each context and not in a script. That way I believe the Redirector extra will redirect fine!
                        Codeplaza Webdesign: for professional websites at low cost