Compact language switcher
Last modified: September 26, 2008 - 12:09
There is a snippet that can be used for language switching instead of the standard language switching block (see an example of page template code appended) - the point is (1) being more compact (e.g. you can insert the switcher into the page header next to the site name, slogan, or search box), and (2) hiding the current language. See #242646: Compact language switcher - how to modify D5 snippet for D6
<?php
// this is copy&paste from locale_block in locale.module
$languages = language_list('enabled');
$links = array();
foreach ($languages[1] as $language) {
if ($language->language != $current) {
$links[$language->language] = array(
'href' => $_GET['q'],
'title' => $language->native,
'language' => $language,
'attributes' => array('class' => 'language-link'),
);
}
}
// this adds the real paths, i.e. if we are on a german page,
// the british flag will point to en/english_alias instead of
// en/node_with_german_content
translation_translation_link_alter($links, $_GET['q']);
// This one adds extended languages, i.e. those that are not enabled.
// Disable if you want only flags for enabled languages.
i18n_translation_link_alter($links, $_GET['q']);
// now add or replace text links by flags, according to your i18n settings.
if (function_exists('languageicons_translation_link_alter'))
languageicons_translation_link_alter($links, $_GET['q']);
// Or do your own stuff, e.g. set the flags and no lang names,
// no matter what the i18n icon settings say.
//if ($icon = theme('languageicons_icon', $language, NULL)) {
// $links[$language->language]['title'] = theme('languageicons_place', $link['title'], $icon);
// $links[$language->language]['html'] = TRUE;
//}
// remove the current language again. We cannot do that
// earlier because i18n_translation_link_alter would add it
// again, thinking it was an extendeg language because it
// was not yet contained in $links. If you don't use
// i18n_translation_link_alter, you can also make a case distinction
// in the foreach loop
$current = i18n_get_lang();
unset($links[$current]);
// format as you like, e.g.
echo theme('links', $links, array());
?>
How do I get the languages to appear inline instead that as ul
Hello,
I am not really good with defining classes and or elements on the page templates. I was wondering how should the code be modified so that I can have a class I can use to format the way the languages appear?. I am particularly interested to get the languages to appear inline one to the next instead of getting them to appear as a unordered list.
Any help would be great.
Raul
totocol@yahoo.com
"// format as you like"
As seen at the bottom of the snippet's code:
// format as you like, e.g.echo theme('links', $links, array('INSERT ONE OR MORE CLASSES HERE'));
?>
This will only let you "inject" class names into the
unordered listgenerated by the links function. I am fairly new to customizing Drupal's output using functions, but I hope this helps!(I *believe* this is the function being implemented, if you're curious. I don't know that for a fact, but hopefully someone will correct me if I'm wrong!)
note: If you're trying to get your language switcher to appear inline with other blocks (i.e. to the right or left of a horizontal navbar), you will have to override the "block" class using CSS.
"Nothing will work unless you do." - Maya Angelou
show all the enable language
How about to show all the enable language ...
I can't find the snippet that compatible in version 6.x.
Your help is really appreciated..
Thanks...
Tryig to do the same thing in a menu item
This code has proven very useful to me when implemented as a block. But my life as a programmer would be much simpler if I could implement this as a menu item instead of a block because that's how all the designers I work with tend to think about the language switcher. I've had some success when working this code into a hook_menu() implementation that uses a wildcard for the path. Any help to bring this to completion would be very welcome. I've posted this at the internationalization group here : http://groups.drupal.org/node/25253
drupal_alter()
I found it helpful to replace
<?phptranslation_translation_link_alter($links, $_GET['q']);
?>
with
<?phpdrupal_alter('translation_link', $links, $_GET['q']);
?>
I noticed that my language switching link was not working on taxonomy paths, but locale's language switching block does. After using drupal_alter() on the links, taxonomy gets to do it's part too.
Doesn't work properly if browser language detection is enabled
I have a bilingual site that is configured to detect language via path-prefix w/browser fall-back. The default site language is English.
I've noticed that this switcher code works great if I connect to the site with an English browser. I can click the link and switch back and forth from English to French without difficulty.
However, using a French-enabled browser, I get locked into the French side of the site. It seems like the browser setting is taking precedence over the user's choice - but only when dealing with the non-default site language.
Go to
Go to admin/settings/language/configure/language (Language negotiation:)
Select "Path prefix only."