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).