t('Menu'));
}
}
/**
* Implementation of hook_menu_alter().
*/
function i18nmenu_menu_alter(&$items){
//dsm($items);
}
/**
* Implementation of hook_menu_link_alter().
*
* Catch changed links, update language and refresh texts
*/
function i18nmenu_menu_link_alter(&$item, $menu) {
// If we set option to language it causes an error with the link system
// This should handle language only as the links are being manually updated
if (!empty($item['language'])) {
//dsm("Language set");
$item['options']['langcode'] = $item['language'];
} elseif(isset($item['language'])) {
//dsm('Removed language');
unset($item['options']['langcode']);
}
// @ TO DO: Refresh texts
//dsm($item);
//$original = $item['original_item'];
//$item['router_path'] = _menu_find_router_path($menu, $item['link_path']);
//i18nmenu_make_translatable($item);
}
function i18nmenu_help($section, $arg) {
switch ($section) {
case 'admin/help#i18nmenu' :
$output = '
'.t('This module provides support for translatable custom menu items:').'
';
$output .= '';
$output .= '- '.t('Create menus as usual, with names in English or the default language. If the menu is already created, no changes are needed.').'
';
$output .= '- '.t('Use the localization system to translate menu item strings').'
';
$output .= '- '.t('Instead of the old menus use the new blocks provided by the module. These will be localized.').'
';
$output .= '- '.t('Optionally, you can set up a language for a menu item so it is only displayed for that language.').'
';
$output .= '
';
return $output;
}
}
/**
* Implementation of hook_block().
*/
function i18nmenu_block($op = 'list', $delta = 0) {
global $user;
$menus = menu_get_menus();
// unset($menus['navigation']);
if ($op == 'list') {
$blocks = array();
foreach ($menus as $name => $title) {
// Default "Navigation" block is handled by user.module.
$blocks[$name]['info'] = check_plain($title).t('[Translated]');
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[$name]['cache'] = BLOCK_NO_CACHE;
}
return $blocks;
}
else if ($op == 'view') {
// The Navigation menu is handled like in the user module.
if ($delta == 'navigation') {
$data['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
} else {
$data['subject'] = check_plain($menus[$delta]);
}
$data['content'] = i18nmenu_translated_tree($delta);
return $data;
}
}
/**
* Get localized menu tree
*/
function i18nmenu_translated_tree($menu_name) {
static $menu_output = array();
if (!isset($menu_output[$menu_name])) {
$tree = menu_tree_page_data($menu_name);
i18nmenu_localize_tree($tree);
$menu_output[$menu_name] = menu_tree_output($tree);
}
return $menu_output[$menu_name];
}
/**
* Localize menu tree
*/
function i18nmenu_localize_tree(&$tree) {
global $language;
foreach($tree as $index => $item) {
$link = $item['link'];
if ($link['customized']) {
// Remove links for other languages than current
// Links with language wont be localized
if (!empty($link['options']['langcode'])) {
if($link['options']['langcode'] != $language->language) {
unset($tree[$index]);
//dsm("Removed because language:".$link['title'].' language='.$link['options']['langcode']);
}
} else {
$router = i18nmenu_get_router($link['router_path']);
// If the title is the same it will be localized by the menu system
if ($link['link_title'] != $router['title']){
//$tree[$index]['link']['title'] = 'Translated';
$tree[$index]['link']['title'] = tt('menu:item:'.$link['mlid'].':title', $link['link_title'], NULL, TRUE);
}
}
}
}
}
/**
* Get the menu router for this router path
*
* We need the untranslated title to comparte, and this will be fast
* There's no api function to do this?
*/
function i18nmenu_get_router($path) {
static $cache = array();
if (!array_key_exists($path, $cache)) {
$cache[$path] = db_fetch_array(db_query("SELECT title FROM {menu_router} WHERE path = '%s'", $path));
}
return $cache[$path];
}
/**
* Implementation of hook_form_alter().
*/
function i18nmenu_form_alter(&$form, $form_state, $form_id){
if ($form_id == 'menu_edit_item') {
//dsm ($form);
if ($form['menu']['#item'] && isset($form['menu']['#item']['options']['langcode'])) {
$language = $form['menu']['#item']['options']['langcode'];
} else {
$language = '';
}
$form['menu']['language'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#options' => array('' => t('All languages')) + locale_language_list('name'),
'#default_value' => $language,
);
}
}