When writing a custom module, what is the proper way to insert a localized menu via hook_menu
or hook_menu_alter
? By localized I mean not only the translated label - that'd be done using the t()
function on title - but also having localized path and page callback.
I tried something like this:
function hook_menu() {
global $language;
$items = array();
switch($language->language){
case 'en':
$items[] = array(
'title' => 'Contact Us',
'page callback' => 'drupal_goto',
'page arguments' => array('node/285'),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
case 'pl':
$items[] = array(
'title' => 'Kontakt',
'page callback' => 'drupal_goto',
'page arguments' => array('node/286'),
'access callback' => TRUE,
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
break;
}
return $items;
}
but this is not behaving properly - once created, the menu item stick at the original language regardless of subsequent language negotiations. So what is the way to go?
Edit after trying out the code provided in answers
OK, so I approached that topic afresh and tried out all of the three methods suggested below. The two programmatic methods have the culprit that the array key used to install the menu item (as in: $items['corporate/contact-us']
) will display in the status bar on hover, regardless of the language chosen. This is cosmetic but I consider it a bad UX. The third - UI-based - method has a side-effect (which is not the point of the original question) but was tried out just for curiosity. The point of my question was - whether it was possible to install a localized menu item totally programatically, and get exact result as when done through UI. Looks like this is not quite possible, unless there's a piece of know-how that I'm missing.