Hide the active language in Language Switcher block in Drupal 7

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by mlecha on April 2, 2011 at 3:19pm

I'd like to have the language switcher block in core Drupal 7 hide the active language. (ie: If you're reading the English page, don't display a link to the English page.)

I've come across this solution for Drupal 6, which seems hefty for a seemingly trivial task (and doesn't work in D7):
http://drupal.org/node/909418

I'm new to Drupal, and I am having difficulties knowing where to start.

What's interesting, is that the language links that appear on the bottom of the content that is translated don't include the language of the current page. Perhaps what I need to do is somehow override the functionality and have it output in the theme region of my choosing?

Your comments would REALLY be helpful!

Comments

If you are looking to hide

Posted by stuartEngelhardt on April 2, 2011 at 8:18pm

If you are looking to hide it, why not just assign the .active for the element to display: none; in your theme's CSS?

yeah. that will just work

Posted by dan.hu on April 2, 2011 at 9:00pm

yeah. that will just work perfectly!

Other way

Posted by yrocq on July 26, 2011 at 9:23pm

If, like me, you don't like to hide elements with CSS, you can put this code in your template.php file :

<?php
function mythemename_links__locale_block($variables) {
global
$language;
unset(
$variables['links'][$language->language]);

return
theme('links', $variables);
}
?>

This is better than CSS

Posted by bhavikshah9 on October 1, 2014 at 3:05pm

This approach is better than hiding the active language using CSS.
1) It will not generate the HTML at all
2) You will not have to tackle annoying UL LI's borders.

Bhavik Shah
Drupal and MongoDB enthusiast, Sr. Software Engineer at Tatvasoft
bhavikshah835@gmail.com || (+91) 901 682 0729

Hide untranslated languages

Posted by natuk on October 5, 2011 at 3:16pm

And for those who only want to hide the untranslated languages elements use this:

<?php
function iic_main_links__locale_block($variables) {
foreach(
$variables['links'] as $key => $value) {
if (
$value['attributes']['class']=='locale-untranslated') {
unset(
$variables['links'][$key]);
}
}
return
theme('links', $variables);
}
?>

Hide active language and format to copy Primary Links

Posted by morybel on January 23, 2012 at 4:42pm

I know it's been a while, but I thought I'd post my solution, for the sake of those like me: who never got to work this out other ways.

For drupal 7

I copied this in my css theme style:

.language-switcher-locale-url li.active a{
display: none;
}

.language-switcher-locale-url li,
.language-switcher-locale-url ul,
.language-switcher-locale-url ol,
.language-switcher-locale-url a,
.language-switcher-locale-url a:link,
.language-switcher-locale-url a.link,
.language-switcher-locale-url a:visited,
.language-switcher-locale-url a.visited,
.language-switcher-locale-url a:hover,
.language-switcher-locale-url a.hover,
.language-switcher-locale-url a.hovered
{

font-size: 15px;
font-family: Arial, Helvetica, Sans-Serif;
display: inline;
color: white;
text-decoration: none;
}

After, I got my switcher block in a free region and aligned it with my "Primary links" menu with css (Extra2 in my case)

Up to you to change fonts settings and more. I covered pretty much all angles of the link (hovered, visited). I'm relatively new to css and drupal so please feel free to correct this. This hides my active language and formats the unactive language with my Primary links menu.

** sorry for my bad english, I'm french.

Keep it theme agnostic

Posted by colan on April 9, 2015 at 8:52pm

To keep this theme agnostic (independent of the theme you're currently using), I'd recommend doing this in a hook_language_switch_links_alter implementation.

For details, see How can I hide the active language in the language switcher block?.

This did not work for me. Am

Posted by Tsjippy on May 7, 2015 at 1:24pm

This did not work for me.

Am I correct that I just have to add this code to the language.api.php file in /public_html/modules/system?

Not at all

Posted by colan on May 7, 2015 at 2:42pm

No, that won't work. You need to add it to a custom module you've created. You should never add custom changes to modules from drupal.org.

Normally, I have one custom module per project named after the project that handles little tweaks like this. Let's say the site is called "Lemonade Stand". So you'd build a module with the machine name "lemonade", which would have a lemonade.module file.

In there, you would implement the hook. The "hook" is replaced by the module name:

function lemonade_language_switch_links_alter(array &$links, $type, $path) {
unset($links[$GLOBALS['language']->language]);
}

After you enable your module, your hook implementation will fire.

Ok Thank you, but I still

Posted by Tsjippy on May 7, 2015 at 3:10pm

Ok Thank you, but I still need a little more help.

I created a folder as follows: /public_html/sites/all/modules/TasteDelft

In there I have a TasteDelft.info and a TasteDelft.module file.

the TasteDelft.info looks like this:

name = TasteDelft Custom Module
description = A Module with all specific settings for tastedelft.nl
version = "7.x-1"
core = "7.x"
project = "TasteDelftCustom"

The TasteDelft.module looks like this:

<?php

function TasteDelft_language_switch_links_alter(array &amp;$links, $type, $path) {
unset($links[$GLOBALS['language']-&gt;language]);
}

The module is visible in my modules list but when I enable it, it leads me to a blank page: http://www.tastedelft.nl/admin/modules/list/confirm

If I go back, the module is not enabled.

Do I forget something?

Solved

Posted by Tsjippy on May 8, 2015 at 6:25am

Never mind, I solved it by using this code in the module:

<?php
/**
* Implements hook_language_switch_links_alter().
*/

function tastedelft_language_switch_links_alter(array &$links, $type, $path) {
global $language;

unset($links[$language->language]);
}

Below work for me... Add this

Posted by computer_jin on March 6, 2015 at 11:42pm

Below work for me...

Add this in your theme template.php file.

<?php
function mythemename_links__locale_block($variables) {
global
$language;
unset(
$variables['links'][$language->language]);

return
theme('links', $variables);
}
?>

No

Posted by colan on April 9, 2015 at 7:36pm

You're putting functionality in your theme. I don't recommend that. It'll stop working when you switch themes, and is counter to best practices. See my above comment for a better way to do this.

Thanks Colan

Posted by leanderl on June 29, 2015 at 10:37am

Thanks for sharing that simple and useful piece of code. Works beautifully.

What if I don't want to

Posted by remaye on May 13, 2016 at 11:15am

What if I don't want to completely remove the current language item,
but, for SEO considerations, only the link on the current language item,
like this :

<ul class="language-switcher-locale-url">
<li class="fr first active francais">Français</li>
<li class="en last english">
<a href="/lilylatifi/en/custom-curtain" class="language-link english active" xml:lang="en" hreflang="en" title="Custom curtains">English</a>
</li>
</ul>

Hide the current language

Posted by morybel on May 13, 2016 at 12:04pm

Well, you could hide it, like I do.
It's a cheap solution, but it works.

This is not an option as SEO

Posted by remaye on May 13, 2016 at 12:52pm

This is not an option as SEO tools don't care about CSS and will still see the link.
The reason of not displaying language switcher link to active page is to avoid over-pushing the url with non relevant content.
Indeed, the content of the language switcher <a> tag is the tag "title" i.e. : "french" or "english"... that are not content related terms.
Thanks anyway for your answer.

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /