Language toggle/switch display problem


I have a multilingual site, with the selectors displaying in a list.

<nav class="nav lang-toggle">
			    <?php foreach($site->languages() as $language): ?>
			    <li<?php e($site->language() == $language, ' class="active"') ?>>
			      <a href="<?php echo $language->url() ?>">
			        <?php echo html($language->name()) ?>
			    <?php endforeach ?>

Which yeilds this on the front-end:

This works fine, with two options of English and Korean to select between. But I am trying to achieve a toggle that only displays the Korean switch when on the English version of the site, and vice versa.

I don’t know how to call and display the alternative $site->language

The language switch on doesn’t provide a means to do this, that I could see.
Any help is very much appreciated!

You can check if $language->code() equals $site->language()->code() in an if statement.

Edit: Or rather, it not equal… $site->language()->code() returns the language code of the currently active language.

I’m not sure I completely understand, would it need to be nested in the foreach statement?

Yes, exactly, since you need the $language variable, it only makes sense within the foreach loop. Try it yourself first, that’s the best way to learn this stuff.

Took me a little while to understand this, but I was able to implement it! Thanks! :slight_smile:

<nav class="nav lang-toggle" role="navigation">
	<?php foreach($site->languages() as $language): ?>
		<?php if($language->code() != $site->language()->code()): ?>
		<a href="<?php echo $page->url($language->code()) ?>">
		  <?php echo html($language->name()) ?>
		<?php endif ?>
	<?php endforeach ?>
1 Like