We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34017
    • 898 Posts
    This is a quick way to switch between different languages and stay on the same page. It works with PHx and should be able to work with many snippets. It creates a $_SESSION variable so the users choice is remembered on other pages.

    $e = &$modx->Event;
    	
    switch ($e->name) {
    	case "OnWebPageInit":
    			$default_language = 'spanish';						//This can be changed
    			$chosen_language_placeholder = 'chosen_language';	//This can be changed
    			$language_placeholder = 'testing_plugin';			 //This can be changed
    			$change_language_placeholder = 'change_language';	//This can be changed
    						
    			if (isset($_POST['get_language'])) {
    				$get_language = $_POST['get_language'];
    				$_SESSION['get_language'] = $_POST['get_language'];
    			} elseif (isset($SESSION['get_language'])) {
    				$get_language = $_SESSION['get_language'];
    			} else {
    				$get_language = $default_language;
    			}
    
    			if ($get_language == 'spanish') {
    				$change_language_value = 'english';
    				$chosen_language_value = 'spanish';
    			} else {
    				$change_language_value = 'spanish';
    				$chosen_language_value = 'english';
    			}
    
    			$change_language_form  = '<form action="[~[*id*]~]" method="post" class="change_language">';
    			$change_language_form .= '<input type="hidden" name="get_language" id="get_language" value="'.$change_language_value.'">';
    			$change_language_form .= '<input type="submit" name="submit" value="Change to '.$change_language_value.'"  class="btn"></form>';
    
    			$modx->setPlaceholder($chosen_language_placeholder, $chosen_language_value);
    			$modx->setPlaceholder($change_language_placeholder, $change_language_form);
    			$modx->setPlaceholder($language_placeholder, $get_language);
    			break;
    	default :    
    	  return; 
    }
    


    This creates a form with a hidden value and a submit button. This form is called via [+chosen_language+] (can be changed). If the users language choice is "spanish", the form says "change to english" and the [+chosen_language+] value is "spanish".

    You can use this in multiple ways:

    In Ditto, you can do this (where spanishDescription and englishDescription are Template Variables):
    [+chosen_language:is=`spanish`:then=`[+tvspanishDescription+]`:else=`[+tvenglishDescription+]`+]
    


    You could do many things with this.

    Items to do or can be done with this
    - Load a chunk or @DOCUMENT binding TV
    - Use CSS to leave all languages on the page but hide all but the current choice (style display:hidden) (for SEO)
    - Create an array of languages to sort through and make the submit button images with [+language+].jpg for flags
    - Create a cookie of the settings
    - Have a user setting via user_attributes where this pulls from

    I believe this is best for sites with few languages or the TV’s would really add up (unless you used @DOCUMENT binding).
      Chuck the Trukk
      ProWebscape.com :: Nashville-WebDesign.com
      - - - - - - - -
      What are TV's? Here's some info below.
      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The first time I made a multi-language site, over two years ago now, I first thought to use the SESSION. The only problem is that this site also uses a member login, and on logout the SESSION is cleared. So I ended up using a cookie.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 6726
        • 7,075 Posts
        Very cool and elegant solution, I’ll make sure to test it !
          .: COO - Commerce Guys - Community Driven Innovation :.


          MODx est l&#39;outil id
          • 10449
          • 956 Posts
          Won’t this approach (forms and sessions or cookies) mean that searchbots would ever only index one language?

          From a SEO point of view, this is not ideal.
            • 34017
            • 898 Posts
            ganesh,

            That is correct. The tutorial I set up would not allow search engines to find all languages. However, you can show all languages, and wrap the non-chosen languages in a CSS hidden div.

            Chuck
              Chuck the Trukk
              ProWebscape.com :: Nashville-WebDesign.com
              - - - - - - - -
              What are TV's? Here's some info below.
              http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
              http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
              • 32942
              • 141 Posts
              worth a try! grin
              but the css approach would cause more traffic than needed everytime the site is viewed, instead of just the traffic caused by bots when crawling all languages.