Jump to content

Language Switcher with Image


Recommended Posts

I am struggling with an issue where my language switcher with flags sometimes doesn't show the flag.

It works in the default language (german):


but when the site is in english the german flag is not shown (path to image is not found):


I don't really understand why it works if the site is shown in german but not in english..

Code for language switcher:

// remember what language is set to
$savedLanguage = $user->language;
$languageImage = $savedLanguage->image->url;
echo "<li><a href='#' class='dropdown-toggle' data-toggle='dropdown'><img src='$languageImage' alt='$savedLanguage->title'> <span uk-icon='icon: chevron-down'></span></a>";
echo "<div class='uk-navbar-dropdown'>";
echo "  <ul class='uk-nav uk-navbar-dropdown-nav'>";

foreach($languages as $language) { //go through all languages

  // if user is already viewing the page in this language, skip it
  if($language->id == $savedLanguage->id) continue;

  // if this page isn't viewable (active) for the language, display root page
  $viewable = true;
  if(!$page->viewable($language)) $viewable = false;

  // set the user's language, so that the $page->url and any other
  // fields we access from it will be reflective of the $language
  $user->language = $language;

  // output a link to this page in the other language
  $path = $language->image->url;
  $pagePath = $page->url;
  if(!$viewable) $pagePath = $pages->get(1)->url;
  echo "<li><a class='uk-text-medium' href='$pagePath' alt='$language->title'><img src='$path' alt='$language->title flag'> $language->title</a></li>";
echo "  </ul>";
echo "</div>";
echo "</li>";
// restore the original language setting
$user->language = $savedLanguage;

Language template:

image field is configured as automatic (array if multiple)

in language page (both german and english) only one image is saved

html output of language switcher dropwon (image couldn't be loaded):


html output when image can be displayed:


Edited by joeck
reformat code section
Link to comment
Share on other sites

Not sure if it is really that bad to use flags. Especially if one also adds the name of the language as text label... I've done a little research...





I suppose they have thought about that topic ? 

Link to comment
Share on other sites

Saying "consider" and "should not", I tried to give advice - of course there is no "must not"... I'm aware that many and big players don't care, or see some benefits in their way to handle this. They ignore the arguments against it, and that is bad.

Link to comment
Share on other sites

Yeah no offense - it was me who posted some links about better not using flags, but just wanted to mention that I'm not so sure about that ? So it was no advice from my side - just shared the results of the quick research because I was curious ? 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...