Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active December 14, 2015 18:39
Show Gist options
  • Save somatonic/5130992 to your computer and use it in GitHub Desktop.
Save somatonic/5130992 to your computer and use it in GitHub Desktop.
language switch for LanguageLocalizedURL module example
<?php
/* somehwere in the top of your template code */
// save current user language
$user_lang = $user->language;
// get the language name example
$curr_langname = $user_lang->name == 'default' ? 'en' : $user_lang->name;
// get the gateway language page /en/ or /de/ example
$curr_langpage = $pages->get("/$curr_langname/");
/**
* construct a language switch example
*/
$lang_switch = '';
foreach($languages as $lang) {
// change user language to get localized urls for each language page
$user->language = $lang;
$langname = $lang->isDefault() ? 'en' : $lang->name;
$gateway = $pages->get("/$langname/");
// is language published or is it homepage?
// language_published is a page field (references gateway pages) with inputfield checkboxes
// language_published this is a page array and we check if the array "has" the gateway page checked
// (which makes the language segment /en/ and /de/ and defines language of the viewed page)
if($page->language_published->has($gateway)) {
$class = $curr_langname == $langname ? ' class="on"' : '';
$lang_switch .= "<li$class><a href='$page->url'>$langname</a></li>";
}
// output on bottom of this file
}
// strip off "|"
$lang_switch = trim($lang_switch, "| ");
// assign current language back to user
$user->language = $user_lang;
/* ------------------------------------------------------------------------------------------------- */
/* somewhere in your template output switch markup */
echo "<ul class='language_switch'>$lang_switch</ul>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment