Jump to content

Language Switcher with Image


joeck
 Share

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

grafik.png.9c4de3df729aecc4bcb6976767f2112f.png

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

grafik.png.7e86cc82369d4908ff2e3bd6c22cd502.png

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:
grafik.thumb.png.697f484a4c90c468e6a58ae06ed10c1f.png

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

grafik.png.55b229812cdb3845e454c23d4a36d0a2.png

html output when image can be displayed:

grafik.png.c116d1c15a2bb435ed84ebbe2987d94c.png

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

hetzner
7lfq1mh.png

mollie
euFPUX7.png

paypal
1Tc3C4W.png
CBH34cP.png

amazon
R00M2ke.png

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
 Share

×
×
  • Create New...