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

devnate’s picture

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.

jmav’s picture

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

  <?php //get listed links of languages
      $query = drupal_query_string_encode($_GET, array('q'));
      print theme('item_list', translation_get_links($_GET['q'], empty($query) ? NULL : $query));
  ?>

Just print links

  <?php //get links of languages
    $query = drupal_query_string_encode($_GET, array('q'));
    $lang = translation_get_links($_GET['q'], empty($query) ? NULL : $query);
    foreach($lang as $lang_tmp) {
      print $lang_tmp;
    }
  ?>

This is rearranged code from module i18n (block) - which is switching only the interface language.
To print links in LI element

  <?php //get listed links of languages
  $query = drupal_query_string_encode($_GET, array('q'));
  print theme('item_list', i18n_get_links($_GET['q'], empty($query) ? NULL : $query));
  ?>

Just print links

  <?php //get links of languages
  $query = drupal_query_string_encode($_GET, array('q'));
  $lang = i18n_get_links($_GET['q'], empty($query) ? NULL : $query);
  foreach($lang as $lang_tmp) {
    print $lang_tmp;
  }
  ?>

Active language ling gets "ACTIVE" class name.

drozzy’s picture

@jmav Thanks!!!
Works great! Just create a new PHP block with that code and everything is hunky dory:

 <?php //get listed links of languages
      $query = drupal_query_string_encode($_GET, array('q'));
      print theme('item_list', translation_get_links($_GET['q'], empty($query) ? NULL : $query));
  ?>
kumarldh’s picture

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

echo '<a href="'.$base_path.'en'.(substr_replace( $_REQUEST["q"], '', 0, 2)).'">EN</a>&nbsp;&nbsp;&nbsp;<a href="'.$base_path.'fr'.(substr_replace( $_REQUEST["q"], '', 0, 2)).'">FR</a>';

That's it

------
Why is drupal so hard to learn?

light-blue’s picture

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

light-blue’s picture

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.

Anonymous’s picture

This does not work in Drupal 6.14.

The function translation_get_links() is not recognized.