I'm posting this theming snippet that I added to page.tpl.php because it took me so long to debug, given the somewhat complex way that the translations.module api works. That's not a dig on the module - it's pretty amazing!
Basically, I have a English/Spanish site and wanted to add a single menu item to my primary links that switches from English to Spanish and vice versa. The complexity comes in how URLs are translated verses how links to a translated node work. Anyway, perhaps this will help someone build a cool bi-lingual site. (Oh, and it's not the most elegant code, so refinement tips would be much appreciated.)
This snippet is taken from a zen-based theme I am developing.
</div> <!-- /logo-title -->
<div id="navigation" class="menu<?php if ($primary_links) { print " withprimary"; } if ($secondary_links) { print " withsecondary"; } ?> ">
<?php if (!empty($primary_links)): ?>
<div id="primary" class="clear-block">
<?php
$current_lang = i18n_get_lang(); // Look up current language with a call to this i18n function, setup link array elements.
if ($current_lang == "es") {
$switch_lang = "en";
$lang_title = "English";
$trans_link = "trans-link-en";
} else {
$switch_lang = "es";
$lang_title = "Español";
$trans_link = "trans-link-es";
}
$current_url = $_GET['q'];
$switch_url = translation_url($current_url, $switch_lang);
If (arg(0) == "node") {
// If working with node translations, need to use translation_url function to get correct link.
$primary_links = array($trans_link => array("title" => $lang_title, "href" => $switch_url)) + $primary_links;
} else {
// If working with system paths, views, and URL aliases, translation_url doesn't work, so create translation link manually.
$primary_links = array($trans_link => array("title" => $lang_title, "href" => $switch_lang . "/" . $current_url)) + $primary_links;
}
print theme('links', $primary_links);
?>
</div>
<?php endif; ?>
</div> <!-- /navigation -->
Comments
Function in template.php
Thanks, This was helpful. I actually just wanted a link not in a menu, so I modified the code a little and made it a function in my template.php file, so I could just call the function and get the html for the link.
Better snippets for language switching
Better snippets which are originally used in i18n module.
This is rearranged code from module translation inside project i18n (block) - which is switching the interface language an the content if available.
To print links in LI element
Just print links
This is rearranged code from module i18n (block) - which is switching only the interface language.
To print links in LI element
Just print links
Active language ling gets "ACTIVE" class name.
Works great! Just create a
@jmav Thanks!!!
Works great! Just create a new PHP block with that code and everything is hunky dory:
My quick hack is this a)
My quick hack is this
a) after you do il8n module settings create separate menu for another language, as I did for French in my case
b) put this code where you want your language switcher to appear
That's it
------
Why is drupal so hard to learn?
Removing Flags
For many reasons (including SEO, country-flag ambiguity, etc.), you may not want to show the flag in your link.
In that case, override it with something simple like this: http://drupal.org/node/179991#comment-610287
Simple way to show languages before header
I spent about 8 hours trying to figure out how to put a drop-down language selector in my primary links, then in a block, then in panels2, I finally just got frustrated because none of those *looked good* with my design.
If you are interested in a simple, effective, SEO-friendly, and good-looking approach , open up page.tpl.php and copy and paste this line just before the id="header"
<? foreach (translation_get_links($_GET['q'], empty($query) ? NULL : $query) as $lang) print("{$lang} "); ?>
There are probably more efficient ways to handle the above code, so feel free to offer corrections.
This does not work in Drupal
This does not work in Drupal 6.14.
The function translation_get_links() is not recognized.