I have used the code found here http://drupal.org/node/313814. This code returns a themed link to the translation of the current path.

But this makes theming difficult sometimes - I'm not a CSS expert and I have a hard time when things line up properly in one browser but not in others. In many cases the designers I work with like to display the language switcher as part of a menu. I've messed around with absolute positions for the language link so it sits next to the other menu items, but it always ends up a pixel or two off in one browser compared to another.

I'd like to get the translation link to be part of the menu and I've made some headway to achieve this.

Unfortunately I hit a problem which I can't seem to resolve.

Here is the implementation of hook_menu(), using only a wildcard for the path. Please note that at this point my solution would only work for a site that implements no more than two languages. Looping through all implemented languages to create an individual menu item for each would solve this, and that's on my todo list. But I'm in a bit of a hurry to get this site out (no surprise there!) and I know it won't implement more than two languages.

function langswitch_menu() {
  $items['%langswitch_pathmaker'] = array(
    'title' => 'title',
    'title callback' = langswitch_get_translation_language(),
    'page callback' => 'langswitch_houston',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

The page callback is set to a dummy function, since it seems mandatory.
The title callback returns the name of the other implemented language (the one that is not current language).
The menu path, set to a wilcard '%langswitch_pathmaker' creates a dynamic path using a function named langswitch_pathmaker_to_arg().

Here is that function:

  function langswitch_pathmaker_to_arg($arg) {
  // this is copy&paste from locale_block in locale.module
  $languages = language_list('enabled');
  $current = i18n_get_lang();
  $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']);
  //  In a real module some looping needs to happen to implement more than two languages.
  unset ($languages[1][$current]);
  $language_object = array_pop($languages[1]);
  return $links[$language_object->language]['href'];
}

This almost works except for one big problem. The language prefix to the path is the current language, and should be the other implemented language.

The link will point to the translation once, which will show the translation in the current language context. After that all fails.

For example, if I'm at fr/node/12 and the english translation for this path is node/13, the menu item's path will be fr/node/13. I need it to be en/node/13.

I have not been able to figure this one out. Any idea how to resolve this?

Thanks for any help! I'll post the same to the group, as this is not really an issue with the module.

I'm attaching module files that implement all this code.

CommentFileSizeAuthor
langswitch.zip2.03 KBjmlavarenne

Comments

jmlavarenne’s picture

This is the group post where I am also taking this support request http://groups.drupal.org/node/25253. Maybe the status of this should be set to duplicate.

kars-t’s picture

Component: Experimental modules » Taxonomy
Status: Active » Closed (cannot reproduce)

I am closing this due to long inactivity.