| 1 |
<?php
|
| 2 |
// $Id: i18nmenu.module,v 1.1 2007/10/22 12:33:50 jareyero Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Internationalization (i18n) submodule: Menu translation
|
| 6 |
*
|
| 7 |
* @author Jose A. Reyero, 2005
|
| 8 |
*
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_locale().
|
| 13 |
*/
|
| 14 |
function i18nmenu_locale($op = 'groups') {
|
| 15 |
switch ($op) {
|
| 16 |
case 'groups':
|
| 17 |
return array('menu' => t('Menu'));
|
| 18 |
}
|
| 19 |
}
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu_alter().
|
| 22 |
*/
|
| 23 |
function i18nmenu_menu_alter(&$items){
|
| 24 |
//dsm($items);
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Implementation of hook_menu_link_alter().
|
| 29 |
*
|
| 30 |
* Catch changed links, update language and refresh texts
|
| 31 |
*/
|
| 32 |
function i18nmenu_menu_link_alter(&$item, $menu) {
|
| 33 |
// If we set option to language it causes an error with the link system
|
| 34 |
// This should handle language only as the links are being manually updated
|
| 35 |
if (!empty($item['language'])) {
|
| 36 |
//dsm("Language set");
|
| 37 |
$item['options']['langcode'] = $item['language'];
|
| 38 |
} elseif(isset($item['language'])) {
|
| 39 |
//dsm('Removed language');
|
| 40 |
unset($item['options']['langcode']);
|
| 41 |
}
|
| 42 |
// @ TO DO: Refresh texts
|
| 43 |
//dsm($item);
|
| 44 |
//$original = $item['original_item'];
|
| 45 |
//$item['router_path'] = _menu_find_router_path($menu, $item['link_path']);
|
| 46 |
//i18nmenu_make_translatable($item);
|
| 47 |
}
|
| 48 |
|
| 49 |
function i18nmenu_help($section, $arg) {
|
| 50 |
switch ($section) {
|
| 51 |
case 'admin/help#i18nmenu' :
|
| 52 |
$output = '<p>'.t('This module provides support for translatable custom menu items:').'</p>';
|
| 53 |
$output .= '<ul>';
|
| 54 |
$output .= '<li>'.t('Create menus as usual, with names in English or the default language. If the menu is already created, no changes are needed.').'</li>';
|
| 55 |
$output .= '<li>'.t('Use the localization system to translate menu item strings').'</li>';
|
| 56 |
$output .= '<li>'.t('Instead of the old menus use the new blocks provided by the module. These will be localized.').'</li>';
|
| 57 |
$output .= '<li>'.t('Optionally, you can set up a language for a menu item so it is only displayed for that language.').'</li>';
|
| 58 |
$output .= '</ul>';
|
| 59 |
return $output;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_block().
|
| 65 |
*/
|
| 66 |
function i18nmenu_block($op = 'list', $delta = 0) {
|
| 67 |
global $user;
|
| 68 |
$menus = menu_get_menus();
|
| 69 |
|
| 70 |
// unset($menus['navigation']);
|
| 71 |
if ($op == 'list') {
|
| 72 |
$blocks = array();
|
| 73 |
foreach ($menus as $name => $title) {
|
| 74 |
// Default "Navigation" block is handled by user.module.
|
| 75 |
$blocks[$name]['info'] = check_plain($title).t('[Translated]');
|
| 76 |
// Menu blocks can't be cached because each menu item can have
|
| 77 |
// a custom access callback. menu.inc manages its own caching.
|
| 78 |
$blocks[$name]['cache'] = BLOCK_NO_CACHE;
|
| 79 |
}
|
| 80 |
return $blocks;
|
| 81 |
}
|
| 82 |
else if ($op == 'view') {
|
| 83 |
// The Navigation menu is handled like in the user module.
|
| 84 |
if ($delta == 'navigation') {
|
| 85 |
$data['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
|
| 86 |
} else {
|
| 87 |
$data['subject'] = check_plain($menus[$delta]);
|
| 88 |
}
|
| 89 |
$data['content'] = i18nmenu_translated_tree($delta);
|
| 90 |
return $data;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Get localized menu tree
|
| 96 |
*/
|
| 97 |
function i18nmenu_translated_tree($menu_name) {
|
| 98 |
static $menu_output = array();
|
| 99 |
|
| 100 |
if (!isset($menu_output[$menu_name])) {
|
| 101 |
$tree = menu_tree_page_data($menu_name);
|
| 102 |
i18nmenu_localize_tree($tree);
|
| 103 |
$menu_output[$menu_name] = menu_tree_output($tree);
|
| 104 |
}
|
| 105 |
return $menu_output[$menu_name];
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Localize menu tree
|
| 110 |
*/
|
| 111 |
function i18nmenu_localize_tree(&$tree) {
|
| 112 |
global $language;
|
| 113 |
foreach($tree as $index => $item) {
|
| 114 |
$link = $item['link'];
|
| 115 |
if ($link['customized']) {
|
| 116 |
// Remove links for other languages than current
|
| 117 |
// Links with language wont be localized
|
| 118 |
if (!empty($link['options']['langcode'])) {
|
| 119 |
if($link['options']['langcode'] != $language->language) {
|
| 120 |
unset($tree[$index]);
|
| 121 |
//dsm("Removed because language:".$link['title'].' language='.$link['options']['langcode']);
|
| 122 |
}
|
| 123 |
} else {
|
| 124 |
$router = i18nmenu_get_router($link['router_path']);
|
| 125 |
// If the title is the same it will be localized by the menu system
|
| 126 |
if ($link['link_title'] != $router['title']){
|
| 127 |
//$tree[$index]['link']['title'] = 'Translated';
|
| 128 |
$tree[$index]['link']['title'] = tt('menu:item:'.$link['mlid'].':title', $link['link_title'], NULL, TRUE);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Get the menu router for this router path
|
| 137 |
*
|
| 138 |
* We need the untranslated title to comparte, and this will be fast
|
| 139 |
* There's no api function to do this?
|
| 140 |
*/
|
| 141 |
function i18nmenu_get_router($path) {
|
| 142 |
static $cache = array();
|
| 143 |
if (!array_key_exists($path, $cache)) {
|
| 144 |
$cache[$path] = db_fetch_array(db_query("SELECT title FROM {menu_router} WHERE path = '%s'", $path));
|
| 145 |
}
|
| 146 |
return $cache[$path];
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Implementation of hook_form_alter().
|
| 151 |
*/
|
| 152 |
function i18nmenu_form_alter(&$form, $form_state, $form_id){
|
| 153 |
if ($form_id == 'menu_edit_item') {
|
| 154 |
//dsm ($form);
|
| 155 |
if ($form['menu']['#item'] && isset($form['menu']['#item']['options']['langcode'])) {
|
| 156 |
$language = $form['menu']['#item']['options']['langcode'];
|
| 157 |
} else {
|
| 158 |
$language = '';
|
| 159 |
}
|
| 160 |
$form['menu']['language'] = array(
|
| 161 |
'#type' => 'select',
|
| 162 |
'#title' => t('Language'),
|
| 163 |
'#options' => array('' => t('All languages')) + locale_language_list('name'),
|
| 164 |
'#default_value' => $language,
|
| 165 |
);
|
| 166 |
}
|
| 167 |
}
|