Projet

Général

Profil

Paste
Télécharger (32,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_menu / i18n_menu.module @ b3ab3446

1
<?php
2

    
3
/**
4
 * @file
5
 * Internationalization (i18n) submodule: Menu translation.
6
 *
7
 * @author Jose A. Reyero, 2005
8
 *
9
 */
10

    
11
/**
12
 * Implements hook_menu()
13
 */
14
function i18n_menu_menu() {
15
  $items['admin/structure/menu/manage/translation'] = array(
16
    'title' => 'Translation sets',
17
    'page callback' => 'i18n_translation_set_list_manage',
18
    'page arguments' => array('menu_link'),
19
    'access arguments' => array('administer menu'),
20
    'type' => MENU_LOCAL_TASK,
21
    'weight' => 10,
22
  );
23
  $items['admin/structure/menu/manage/translation/add'] = array(
24
    'title' => 'Add translation',
25
    'page callback' => 'drupal_get_form',
26
    'page arguments' => array('i18n_menu_translation_form'),
27
    'access arguments' => array('administer menu'),
28
    'type' => MENU_LOCAL_ACTION,
29
    'file' => 'i18n_menu.admin.inc',
30
  );
31
  $items['admin/structure/menu/manage/translation/edit/%i18n_menu_translation'] = array(
32
    'title' => 'Edit translation',
33
    'page callback' => 'drupal_get_form',
34
    'page arguments' => array('i18n_menu_translation_form', 6),
35
    'access arguments' => array('administer menu'),
36
    'type' => MENU_CALLBACK,
37
    'file' => 'i18n_menu.admin.inc',
38
  );
39
  $items['admin/structure/menu/manage/translation/delete/%i18n_menu_translation'] = array(
40
    'title' => 'Delete translation',
41
    'page callback' => 'drupal_get_form',
42
    'page arguments' => array('i18n_translation_set_delete_confirm', 6),
43
    'access arguments' => array('administer menu'),
44
    'type' => MENU_CALLBACK,
45
  );
46
  return $items;
47
}
48

    
49
/**
50
 * Implements hook_menu_alter()
51
 */
52
function i18n_menu_menu_alter(&$items) {
53
  $items['admin/structure/menu/item/%menu_link'] = $items['admin/structure/menu/item/%menu_link/edit'];
54
  $items['admin/structure/menu/item/%menu_link']['type'] = MENU_CALLBACK;
55
  $items['admin/structure/menu/item/%menu_link/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
56
  $items['admin/structure/menu/manage/%menu']['title callback'] = 'i18n_menu_menu_overview_title';
57
}
58

    
59
/**
60
 * Preprocess theme_menu_admin_overview to translate menu name and description
61
 *
62
 * @param $variables
63
 */
64
function i18n_menu_preprocess_menu_admin_overview(&$variables) {
65
  $variables['title'] = i18n_string(array('menu', 'menu', $variables['name'], 'title'), $variables['title']);
66
  $variables['description'] = i18n_string(array('menu', 'menu', $variables['name'], 'description'), $variables['description']);
67
}
68

    
69
/**
70
 * Title callback for the menu overview page and links.
71
 */
72
function i18n_menu_menu_overview_title($menu) {
73
  return i18n_string(array('menu', 'menu', $menu['menu_name'], 'title'), $menu['title']);
74
}
75

    
76
/**
77
 * Implements hook_block_view().
78
 */
79
function i18n_menu_block_view_alter(&$data, $block) {
80
  if (($block->module == 'menu' || $block->module == 'system') && (i18n_menu_mode($block->delta) & I18N_MODE_MULTIPLE)) {
81
    $menus = menu_get_menus();
82
    if (isset($menus[$block->delta])) {
83
      if (empty($block->title)) {
84
        $data['subject'] = i18n_string_plain(
85
          array('menu', 'menu', $block->delta, 'title'),
86
          $menus[$block->delta]
87
        );
88
      }
89
      // Add contextual links for this block.
90
      if (!empty($data['content'])) {
91
        $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
92
      }
93
    }
94
  }
95
}
96

    
97
/**
98
 * Implements hook_i18n_translate_path()
99
 */
100
function i18n_menu_i18n_translate_path($path) {
101
  $item = i18n_menu_link_load($path, i18n_langcode());
102
  if ($item && ($set = i18n_translation_object('menu_link', $item))) {
103
    $links = array();
104
    foreach ($set->get_translations() as $lang => $link) {
105
      $links[$lang] = array(
106
        'href' => $link['link_path'],
107
        'title' => $link['link_title'],
108
        'i18n_type' => 'menu_link',
109
        'i18n_object' => $link,
110
      );
111
    }
112
    return $links;
113
  }
114
}
115

    
116
/**
117
 * Implements hook_menu_insert()
118
 */
119
function i18n_menu_menu_insert($menu) {
120
  i18n_menu_menu_update($menu);
121
}
122

    
123
/**
124
 * Implements hook_menu_update()
125
 */
126
function i18n_menu_menu_update($menu) {
127
  // Stores the fields of menu links which need an update.
128
  $update = array();
129

    
130
  if (!isset($menu['i18n_mode'])) {
131
    $menu['i18n_mode'] = I18N_MODE_NONE;
132
  }
133
  if (!($menu['i18n_mode'] & I18N_MODE_LANGUAGE)) {
134
    $menu['language'] = LANGUAGE_NONE;
135
  }
136
  db_update('menu_custom')
137
    ->fields(array('language' => $menu['language'], 'i18n_mode' => $menu['i18n_mode']))
138
    ->condition('menu_name', $menu['menu_name'])
139
    ->execute();
140
  if (!$menu['i18n_mode']) {
141
    $update['language'] = LANGUAGE_NONE;
142
  }
143
  elseif ($menu['i18n_mode'] & I18N_MODE_LANGUAGE) {
144
    $update['language'] = $menu['language'];
145
  }
146

    
147
  // Non translatable menu.
148
  if (!($menu['i18n_mode'] & I18N_MODE_TRANSLATE)) {
149
    $tsids = db_select('menu_links')
150
      ->fields('menu_links', array('i18n_tsid'))
151
      ->groupBy('i18n_tsid')
152
      ->condition('menu_name', $menu['menu_name'])
153
      ->condition('customized', 1)
154
      ->condition('i18n_tsid', 0, '<>')
155
      ->execute()
156
      ->fetchCol(0);
157
    if (!empty($tsids)) {
158
      foreach ($tsids as $tsid) {
159
        if ($translation_set = i18n_translation_set_load($tsid)) {
160
          $translation_set->delete();
161
        }
162
      }
163
    }
164
    $update['i18n_tsid'] = 0;
165
  }
166

    
167
  if (!empty($update)) {
168
    db_update('menu_links')
169
      ->fields($update)
170
      ->condition('menu_name', $menu['menu_name'])
171
      ->condition('customized', 1)
172
      ->execute();
173
  }
174

    
175
  // Update strings, always add translation if no language
176
  if (!i18n_object_langcode($menu)) {
177
    i18n_string_object_update('menu', $menu);
178
  }
179

    
180
  // Clear all menu caches.
181
  menu_cache_clear_all();
182
}
183

    
184
/**
185
 * Implements hook_menu_delete()
186
 */
187
function i18n_menu_menu_delete($menu) {
188
  i18n_string_object_remove('menu', $menu);
189
}
190

    
191
/**
192
 * Implements hook_menu_link_alter().
193
 *
194
 * This function is invoked from menu_link_save() before default
195
 * menu link options (menu_name, module, etc.. have been set)
196
 */
197
function i18n_menu_menu_link_alter(&$item) {
198
  // We just make sure every link has a valid language property.
199
  if (!i18n_object_langcode($item)) {
200
    $item['language'] = LANGUAGE_NONE;
201
    $item['i18n_tsid'] = 0;
202
  }
203
}
204

    
205
/**
206
 * Implements hook_menu_link_insert()
207
 */
208
function i18n_menu_menu_link_insert($link) {
209
  i18n_menu_menu_link_update($link);
210
}
211

    
212
/**
213
 * Implements hook_menu_link_update().
214
 */
215
function i18n_menu_menu_link_update($link) {
216
  // Stores the fields to update.
217
  $fields = array();
218
  $menu_mode = i18n_menu_mode($link['menu_name']);
219

    
220
  if ($menu_mode & I18N_MODE_TRANSLATE && isset($link['language'])) {
221
    // Multilingual menu links, translatable, it may be part of a
222
    // translation set.
223
    if (i18n_object_langcode($link)) {
224
      if (!empty($link['translation_set'])) {
225
        // Translation set comes as parameter, we may be creating a translation,
226
        // add link to the set.
227
        $translation_set = $link['translation_set'];
228
        $translation_set
229
          ->add_item($link)
230
          ->save(TRUE);
231
      }
232
    }
233
    elseif ($link['language'] === LANGUAGE_NONE && !empty($link['original_item']['i18n_tsid'])) {
234
      if ($translation_set = i18n_translation_set_load($link['original_item']['i18n_tsid'])) {
235
        $translation_set->remove_language($link['original_item']['language']);
236
        // If there are no links left in this translation set, delete the set.
237
        // Otherwise update the set.
238
        $translation_set->update_delete();
239
      }
240
      $fields['i18n_tsid'] = 0;
241
    }
242
  }
243
  // For multilingual menu items, always set a language and mark them for
244
  // 'alter' so they can be processed later by
245
  // hook_translated_link_menu_alter().
246
  if ($menu_mode) {
247
    if (!isset($link['language'])) {
248
      $link['language'] = LANGUAGE_NONE;
249
    }
250
    if (_i18n_menu_link_check_alter($link) && empty($link['options']['alter'])) {
251
      $fields['options'] = $link['options'];
252
      $fields['options']['alter'] = TRUE;
253
    }
254
    // We cannot unmark links for altering because we don't know what other
255
    // modules use it for.
256
  }
257
  // Update language field if the link has a language value.
258
  if (isset($link['language'])) {
259
    $fields['language'] = $link['language'];
260
  }
261

    
262
  if (!empty($fields)) {
263
    // If link options are to be updated, they need to be serialized.
264
    if (isset($fields['options'])) {
265
      $fields['options'] = serialize($fields['options']);
266
    }
267
    db_update('menu_links')
268
      ->fields($fields)
269
      ->condition('mlid', $link['mlid'])
270
      ->execute();
271
  }
272
  // Update translatable strings if any for customized links that belong to a
273
  // localizable menu.
274
  if (_i18n_menu_link_is_localizable($link)) {
275
    i18n_string_object_update('menu_link', $link);
276
  }
277
  else {
278
    i18n_string_object_remove('menu_link', $link);
279
  }
280
}
281

    
282
/**
283
 * Implements hook_menu_delete()
284
 */
285
function i18n_menu_menu_link_delete($link) {
286
  // If a translation set exists for this link, remove this link from the set.
287
  if (!empty($link['i18n_tsid'])) {
288
    if ($translation_set = i18n_translation_set_load($link['i18n_tsid'])) {
289
      $translation_set->get_translations();
290

    
291
      $translation_set->remove_language($link['language']);
292

    
293
      // If there are no links left in this translation set, delete the set.
294
      // Otherwise update the set.
295
      $translation_set->update_delete();
296
    }
297
  }
298

    
299
  i18n_string_object_remove('menu_link', $link);
300
}
301

    
302
/**
303
 * Get menu mode or compare with given one
304
 */
305
function i18n_menu_mode($name, $mode = NULL) {
306
  $menu = menu_load($name);
307
  if (!$menu || !isset($menu['i18n_mode'])) {
308
    return isset($mode) ? FALSE : I18N_MODE_NONE;
309
  }
310
  else {
311
    return isset($mode) ? $menu['i18n_mode'] & $mode : $menu['i18n_mode'];
312
  }
313
}
314

    
315
/**
316
 * Implements hook_translated_menu_link_alter().
317
 *
318
 * Translate localizable menu links on the fly.
319
 * Filter out items that have a different language from current interface.
320
 *
321
 * @see i18n_menu_menu_link_alter()
322
 */
323
function i18n_menu_translated_menu_link_alter(&$item) {
324
  // Only process links to be displayed not processed before by i18n_menu.
325
  if (_i18n_menu_link_process($item)) {
326
    if (!_i18n_menu_link_is_visible($item)) {
327
      $item['hidden'] = TRUE;
328
    }
329
    elseif (_i18n_menu_link_is_localizable($item)) {
330
      // Item has undefined language, it is a candidate for localization.
331
      _i18n_menu_link_localize($item);
332
    }
333
  }
334
}
335

    
336
/**
337
 * Implements hook_help().
338
 */
339
function i18n_menu_help($path, $arg) {
340
  switch ($path) {
341
    case 'admin/help#i18n_menu' :
342
      $output = '<p>' . t('This module adds support for multilingual menus. You can setup multilingual options for each menu:') . '</p>';
343
      $output .= '<ul>';
344
      $output .= '<li>' . t('Menus can be fully multilingual with translatable (or localized) menu items.') . '</li>';
345
      $output .= '<li>' . t('Menus can be configured to have a fixed language. All menu items share this language setting and the menu will be visible in that language only.') . '</li>';
346
      $output .= '<li>' . t('Menus can also be configured to have no translations.') . '</li>';
347
      $output .= '</ul>';
348
      $output .= '<p>' . t('The multilingual options of a menu must be configured before individual menu items can be translated. Go to the <a href="@menu-admin">Menus administration page</a> and follow the "edit menu" link to the menu in question.', array('@menu-admin' => url('admin/structure/menu') ) ) . '</p>';
349
      $output .= '<p>' . t('To search and translate strings, use the <a href="@translate-interface">translation interface</a> pages.', array('@translate-interface' => url('admin/config/regional/translate'))) . '</p>';
350
      return $output;
351

    
352
    case 'admin/config/regional/i18n':
353
      $output = '<p>' . t('Menus and menu items can be translated on the <a href="@configure_menus">Menu administration page</a>.', array('@configure_menus' => url('admin/structure/menu'))) . '</p>';
354
      return $output;
355
  }
356
}
357

    
358
/**
359
 * Implements hook_variable_info_alter()
360
 */
361
function i18n_menu_variable_info_alter(&$variables, $options) {
362
  // Make menu variables translatable
363
  $variables['menu_main_links_source']['localize'] = TRUE;
364
  $variables['menu_secondary_links_source']['localize'] = TRUE;
365
  $variables['menu_parent_[node_type]']['localize'] = TRUE;
366
  $variables['menu_options_[node_type]']['localize'] = TRUE;
367
}
368

    
369
/**
370
 * Get localized menu tree.
371
 *
372
 * @param string $menu_name
373
 *   The menu the translated tree has to be fetched from.
374
 * @param string $langcode
375
 *   Optional language code to get the menu in, defaults to request language.
376
 * @param bool $reset
377
 *   Whether to reset the internal i18n_menu_translated_tree cache.
378
 */
379
function i18n_menu_translated_tree($menu_name, $langcode = NULL, $reset = FALSE) {
380
  $menu_output = &drupal_static(__FUNCTION__);
381
  $langcode = $langcode ? $langcode : i18n_language_interface()->language;
382
  if (!isset($menu_output[$langcode][$menu_name]) || $reset) {
383
    $tree = menu_tree_page_data($menu_name);
384
    $tree = i18n_menu_localize_tree($tree, $langcode);
385
    $menu_output[$langcode][$menu_name] = menu_tree_output($tree);
386
  }
387
  return $menu_output[$langcode][$menu_name];
388
}
389

    
390
/**
391
 * Localize menu tree.
392
 */
393
function i18n_menu_localize_tree($tree, $langcode = NULL) {
394
  $langcode = $langcode ? $langcode : i18n_language_interface()->language;
395
  foreach ($tree as $index => &$item) {
396
    $link = $item['link'];
397
    // We only process links that are visible and not processed before.
398
    if (_i18n_menu_link_process($item['link'])) {
399
      if (!_i18n_menu_link_is_visible($item['link'], $langcode)) {
400
        // Remove links for other languages than current.
401
        // Links with language wont be localized.
402
        unset($tree[$index]);
403
        // @todo Research whether the above has any advantage over:
404
        // $item['hidden'] = TRUE;
405
      }
406
      else {
407
        if (_i18n_menu_link_is_localizable($item['link'])) {
408
          // Item has undefined language, it is a candidate for localization.
409
          _i18n_menu_link_localize($item['link'], $langcode);
410
        }
411
        // Localize subtree.
412
        if (!empty($item['below'])) {
413
          $item['below'] = i18n_menu_localize_tree($item['below'], $langcode);
414
        }
415
      }
416
    }
417
  }
418
  return $tree;
419
}
420

    
421
/**
422
 * Localize menu renderable array
423
 */
424
function i18n_menu_localize_elements(&$elements) {
425
  foreach (element_children($elements) as $mlid) {
426
    $elements[$mlid]['#title'] = i18n_string(array('menu', 'item', $mlid, 'title'), $elements[$mlid]['#title']);
427
    if (!empty($tree[$mlid]['#localized_options']['attributes']['title'])) {
428
      $elements[$mlid]['#localized_options']['attributes']['title'] = i18n_string(array('menu', 'item', $mlid, 'description'), $tree[$mlid]['#localized_options']['attributes']['title']);
429
    }
430
    i18n_menu_localize_elements($elements[$mlid]);
431
  }
432
}
433

    
434
/**
435
 * Return an array of localized links for a navigation menu.
436
 *
437
 * Localized version of menu_navigation_links()
438
 */
439
function i18n_menu_navigation_links($menu_name, $level = 0) {
440
  // Don't even bother querying the menu table if no menu is specified.
441
  if (empty($menu_name)) {
442
    return array();
443
  }
444

    
445
  // Get the menu hierarchy for the current page.
446
  $tree = menu_tree_page_data($menu_name, $level + 1);
447
  $tree = i18n_menu_localize_tree($tree);
448

    
449
  // Go down the active trail until the right level is reached.
450
  while ($level-- > 0 && $tree) {
451
    // Loop through the current level's items until we find one that is in trail.
452
    while ($item = array_shift($tree)) {
453
      if ($item['link']['in_active_trail']) {
454
        // If the item is in the active trail, we continue in the subtree.
455
        $tree = empty($item['below']) ? array() : $item['below'];
456
        break;
457
      }
458
    }
459
  }
460

    
461
  // Create a single level of links.
462
  $router_item = menu_get_item();
463
  $links = array();
464
  foreach ($tree as $item) {
465
    if (!$item['link']['hidden']) {
466
      $class = '';
467
      $l = $item['link']['localized_options'];
468
      $l['href'] = $item['link']['href'];
469
      $l['title'] = $item['link']['title'];
470
      if ($item['link']['in_active_trail']) {
471
        $class = ' active-trail';
472
        $l['attributes']['class'][] = 'active-trail';
473
      }
474
      // Normally, l() compares the href of every link with $_GET['q'] and sets
475
      // the active class accordingly. But local tasks do not appear in menu
476
      // trees, so if the current path is a local task, and this link is its
477
      // tab root, then we have to set the class manually.
478
      if ($item['link']['href'] == $router_item['tab_root_href'] && $item['link']['href'] != $_GET['q']) {
479
        $l['attributes']['class'][] = 'active';
480
      }
481
      // Keyed with the unique mlid to generate classes in theme_links().
482
      $links['menu-' . $item['link']['mlid'] . $class] = $l;
483
    }
484
  }
485
  return $links;
486
}
487

    
488
/**
489
 * Get localized menu title
490
 */
491
function _i18n_menu_link_title($link, $langcode = NULL) {
492
  $key = i18n_object_info('menu_link', 'key');
493
  return i18n_string_translate(array('menu', 'item', $link[$key], 'title'), $link['link_title'], array('langcode' => $langcode, 'sanitize' => FALSE));
494
}
495

    
496
/**
497
 * Localize menu item title and description.
498
 *
499
 * This will be invoked always after _menu_item_localize()
500
 *
501
 * Link properties to manage:
502
 * - title, menu router title
503
 * - link_title, menu link title
504
 * - options.attributes.title, menu link description.
505
 * - localized_options.attributes.title,
506
 *
507
 * @see _menu_item_localize()
508
 * @see _menu_link_translate()
509
 */
510
function _i18n_menu_link_localize(&$link, $langcode = NULL) {
511
  // Only translate title if it has no special callback.
512
  if (empty($link['title callback']) || $link['title callback'] === 't') {
513
    $link['title'] = _i18n_menu_link_title($link, $langcode);
514
  }
515
  if ($description = _i18n_menu_link_description($link, $langcode)) {
516
    $link['localized_options']['attributes']['title'] = $description;
517
  }
518
}
519

    
520
/**
521
 * Get localized menu description
522
 */
523
function _i18n_menu_link_description($link, $langcode = NULL) {
524
  if (!empty($link['options']['attributes']['title'])) {
525
    $key = i18n_object_info('menu_link', 'key');
526
    return i18n_string_translate(array('menu', 'item', $link[$key], 'description'), $link['options']['attributes']['title'], array('langcode' => $langcode));
527
  }
528
  else {
529
    return NULL;
530
  }
531
}
532

    
533
/**
534
 * Check whether this link is to be processed by i18n_menu and start processing.
535
 */
536
function _i18n_menu_link_process(&$link) {
537
  // Only links that have a language property and haven't been processed before.
538
  // We also translate links marked as hidden because core breadcrumbs ignore
539
  // that flag and excluding them would basically interfere with core behaviour.
540
  // We also check that they belong to a menu with language options.
541
  if (empty($link['i18n_menu']) && !empty($link['language']) && !empty($link['access']) && i18n_menu_mode($link['menu_name'])) {
542
    // Mark so it won't be processed twice.
543
    $link['i18n_menu'] = TRUE;
544
    // Skip if administering this menu or this menu item.
545
    if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'menu') {
546
      if (arg(3) == 'manage' && $link['menu_name'] == arg(4)) {
547
        return FALSE;
548
      }
549
      elseif (arg(3) == 'item' && arg(4) == $link['mlid']) {
550
        return FALSE;
551
      }
552
    }
553
    // Skip if administering this menu item through the node edit form.
554
    elseif (arg(0) == 'node' && arg(2) == 'edit' && $link['link_path'] == arg(0) . '/' . arg(1)) {
555
      return FALSE;
556
    }
557
    return TRUE;
558
  }
559
  else {
560
    return FALSE;
561
  }
562
}
563

    
564
/**
565
 * Check whether this menu item should be marked for altering.
566
 *
567
 * Menu items that have a language or that have any localizable strings
568
 * will be marked to be run through hook_translated_menu_link_alter().
569
 *
570
 * @see i18n_menu_translated_menu_link_alter()
571
 */
572
function _i18n_menu_link_check_alter($link) {
573
  return i18n_menu_mode($link['menu_name']) && (i18n_object_langcode($link) || _i18n_menu_link_is_localizable($link, TRUE));
574
}
575

    
576
/**
577
 * Check whether this link should be localized by i18n_menu.
578
 *
579
 * @param array $link
580
 *   Menu link array.
581
 * @param bool $check_strings
582
 *   Whether to check if the link has actually localizable strings. Since this
583
 *   is a more expensive operation, it will be just checked when editing menu
584
 *   items.
585
 *
586
 * @return boolean
587
 *   Returns TRUE if link is localizable.
588
 */
589
function _i18n_menu_link_is_localizable($link, $check_strings = FALSE) {
590
  return !empty($link['customized']) && !i18n_object_langcode($link) && i18n_menu_mode($link['menu_name'], I18N_MODE_LOCALIZE) &&
591
  (!$check_strings || _i18n_menu_link_localizable_properties($link));
592
}
593

    
594
/**
595
 * Check whether this menu link is visible for current/given language.
596
 */
597
function _i18n_menu_link_is_visible($link, $langcode = NULL) {
598
  $langcode = $langcode ? $langcode : i18n_language_interface()->language;
599
  return $link['language'] == LANGUAGE_NONE || $link['language'] == $langcode;
600
}
601

    
602
/**
603
 * Get localizable properties for menu link checking agains the router item.
604
 */
605
function _i18n_menu_link_localizable_properties($link) {
606
  $props = array();
607
  $router = !empty($link['router_path']) ? _i18n_menu_get_router($link['router_path']) : NULL;
608
  if (!empty($link['link_title'])) {
609
    // If the title callback is 't' and the link title matches the router title
610
    // it will be localized by core, not by i18n_menu.
611
    if (!$router ||
612
        (empty($router['title_callback']) || ($router['title_callback'] != 't' || !empty($link['customized']))) ||
613
        (empty($router['title']) || $router['title'] != $link['link_title'])
614
    ) {
615
      $props[] = 'title';
616
    }
617
  }
618
  if (!empty($link['options']['attributes']['title'])) {
619
    // If the description matches the router description, it will be localized
620
    // by core.
621
    if (!$router || empty($router['description']) || ($router['description'] != $link['options']['attributes']['title']) || !empty($link['customized'])) {
622
      $props[] = 'description';
623
    }
624
  }
625
  return $props;
626
}
627

    
628
/**
629
 * Get the menu router for this router path.
630
 *
631
 * We need the untranslated title to compare, and this will be fast.
632
 * There's no api function to do this?
633
 *
634
 * @param string $path
635
 *   The path to fetch from the router.
636
 */
637
function _i18n_menu_get_router($path) {
638
  $cache = &drupal_static(__FUNCTION__, array());
639
  if (!array_key_exists($path, $cache)) {
640
    $cache[$path] = db_select('menu_router', 'mr')
641
      ->fields('mr', array('title', 'title_callback', 'description'))
642
      ->condition('path', $path)
643
      ->execute()
644
      ->fetchAssoc();
645
  }
646
  return $cache[$path];
647
}
648

    
649
/**
650
 * Implements hook_form_FORM_ID_alter().
651
 */
652
function i18n_menu_form_menu_edit_menu_alter(&$form, &$form_state) {
653
  $menu = menu_load($form['old_name']['#value']);
654
  $i18n_mode = $menu && isset($menu['i18n_mode']) ? $menu['i18n_mode'] : I18N_MODE_NONE;
655
  $langcode = $menu && isset($menu['language']) ? $menu['language'] : LANGUAGE_NONE;
656

    
657
  $form += i18n_translation_mode_element('menu', $i18n_mode, $langcode);
658
}
659

    
660
/**
661
 * Implements hook_form_FORM_ID_alter().
662
 *
663
 * Add a language selector to the menu_edit_item form and register a submit
664
 * callback to process items.
665
 */
666
function i18n_menu_form_menu_edit_item_alter(&$form, &$form_state) {
667
  $item = &$form['original_item']['#value'];
668
  $item['language'] = i18n_menu_item_get_language($item);
669
  // Check whether this item belongs to a node object and it is a supported type.
670
  $node_item = ($node = i18n_menu_item_get_node($item)) && i18n_menu_node_supported_type($node->type);
671
  if (!$node_item && i18n_menu_mode($item['menu_name'], I18N_MODE_TRANSLATE)) {
672
    //$form['i18n'] = array('#type' => 'fieldset');
673
    $form['i18n']['language'] = array(
674
      '#description' => t('This item belongs to a multilingual menu. You can set a language for it.'),
675
    ) + i18n_element_language_select($item);
676

    
677
    // If the term to be added will be a translation of a source term,
678
    // set the default value of the option list to the target language and
679
    // create a form element for storing the translation set of the source term.
680
    if (isset($_GET['translation']) && isset($_GET['target']) && ($source_item = menu_link_load($_GET['translation']))) {
681
      if (!empty($source_item['i18n_tsid'])) {
682
        $translation_set = i18n_translation_set_load($source_item['i18n_tsid']);
683
      }
684
      else {
685
        // Create object and stick the source information in the translation set.
686
        $translation_set = i18n_translation_set_build('menu_link')
687
          ->add_item($source_item);
688
      }
689
      $form['link_path']['#default_value'] = $source_item['link_path'];
690

    
691
      // Maybe we should disable the 'link_path' and 'parent' form elements?
692
      // $form['link_path']['#disabled'] = TRUE;
693
      // $form['parent']['#disabled'] = TRUE;
694

    
695
      $form['i18n']['language']['#default_value'] = $_GET['target'];
696
      $form['i18n']['language']['#disabled'] = TRUE;
697

    
698
      drupal_set_title(t('%language translation of menu item %title', array('%language' => locale_language_name($_GET['target']), '%title' => $source_item['link_title'])), PASS_THROUGH);
699
    }
700
    elseif (!empty($item['i18n_tsid'])) {
701
      $translation_set = i18n_translation_set_load($item['i18n_tsid']);
702
    }
703

    
704
    // Add the translation set to the form so we know the new menu item
705
    // needs to be added to that set.
706
    if (!empty($translation_set)) {
707
      $form['translation_set'] = array(
708
        '#type' => 'value',
709
        '#value' => $translation_set,
710
      );
711

    
712
      // If the current term is part of a translation set,
713
      // remove all other languages of the option list.
714
      if ($translations = $translation_set->get_translations()) {
715
        unset($form['i18n']['language']['#options'][LANGUAGE_NONE]);
716
        foreach ($translations as $langcode => $translation) {
717
          if ($translation['mlid'] !== $item['mlid']) {
718
            unset($form['i18n']['language']['#options'][$langcode]);
719
          }
720
        }
721
      }
722
    }
723
  }
724
  else {
725
    $form['language'] = array(
726
      '#type' => 'value',
727
      '#value' => $item['language'],
728
    );
729
  }
730
  if ($node_item && i18n_langcode($item['language'])) {
731
    $form['i18n']['message'] = array(
732
      '#type' => 'item',
733
      '#title' => t('Language'),
734
      '#markup' => i18n_language_name($item['language']),
735
      '#description' => t('This menu item belongs to a node, so it will have the same language as the node and cannot be localized.'),
736
    );
737
  }
738
  array_unshift($form['#validate'], 'i18n_menu_menu_item_prepare_normal_path');
739
}
740

    
741
/**
742
 * Implements hook_form_FORM_ID_alter().
743
 * FORM_ID = menu-overview-form.
744
 * Add a "translate" link in operations column for each menu item.
745
 */
746
function i18n_menu_form_menu_overview_form_alter(&$form, &$form_state) {
747
  if (i18n_menu_mode($form['#menu']['menu_name'], I18N_MODE_MULTIPLE)) {
748
    foreach (element_children($form) as $element) {
749
      if (substr($element, 0, 5) == 'mlid:') {
750
        $item = $form[$element]["#item"];
751
        $mlid = $form[$element]['#item']['mlid'];
752
        if (i18n_get_object('menu', $mlid)->get_translate_access()) {
753
          $form[$element]['operations']['translate'] = array(
754
            '#type' => 'link',
755
            '#title' => t('translate'),
756
            '#href' => "admin/structure/menu/item/{$mlid}/translate",
757
          );
758
          $form[$element]['title']['#markup'] = l(_i18n_menu_link_title($item), $item['href'], $item['localized_options']);
759
        }
760
      }
761
    }
762
  }
763
}
764

    
765
/**
766
 * Normal path should be checked with menu item's language to avoid
767
 * troubles when a node and it's translation has the same url alias.
768
 */
769
function i18n_menu_menu_item_prepare_normal_path($form, &$form_state) {
770
  $item = &$form_state['values'];
771
  $item['link_path'] = i18n_prepare_normal_path($item['link_path'], $item['language']);
772
}
773

    
774
/**
775
 * Get language for menu item
776
 */
777
function i18n_menu_item_get_language($item) {
778
  if (isset($item['language'])) {
779
    return $item['language'];
780
  }
781
  else {
782
    $menu = menu_load($item['menu_name']);
783
    if (!isset($menu['i18n_mode'])) {
784
      return LANGUAGE_NONE;
785
    }
786
    switch ($menu['i18n_mode']) {
787
      case I18N_MODE_LANGUAGE:
788
        return $menu['language'];
789
      case I18N_MODE_NONE:
790
      case I18N_MODE_LOCALIZE:
791
        return LANGUAGE_NONE;
792
      default:
793
        if (!empty($item['mlid'])) {
794
          return db_select('menu_links', 'm')
795
            ->fields('m', array('language'))
796
            ->condition('mlid', $item['mlid'])
797
            ->execute()
798
            ->fetchField();
799
        }
800
        else {
801
          return LANGUAGE_NONE;
802
        }
803
    }
804
  }
805
}
806

    
807
/**
808
 * Implements hook_form_node_form_alter().
809
 *
810
 * Add language to menu settings of the node form, as well as setting defaults
811
 * to match the translated item's menu settings.
812
 */
813
function i18n_menu_form_node_form_alter(&$form, &$form_state, $form_id) {
814
  if (isset($form['menu'])) {
815
    $node = $form['#node'];
816
    $link = $node->menu;
817
    if (!empty($link['mlid'])) {
818
      // Preserve the menu item language whatever it is.
819
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $link['language']);
820
    }
821
    elseif (i18n_menu_node_supported_type($node->type)) {
822
      // Set menu language to node language but only if it is a supported node type.
823
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $node->language);
824
    }
825
    else {
826
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => LANGUAGE_NONE);
827
    }
828
    // Customized must be set to 1 to save language.
829
    $form['menu']['link']['customized'] = array('#type' => 'value', '#value' => 1);
830
  }
831
}
832

    
833
/**
834
 * Check whether a node type has multilingual support (but not entity translation).
835
 */
836
function i18n_menu_node_supported_type($type) {
837
  $supported = &drupal_static(__FUNCTION__);
838
  if (!isset($supported[$type])) {
839
    $mode = variable_get('language_content_type_' . $type, 0);
840
    $supported[$type] = $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
841
  }
842
  return $supported[$type];
843
}
844

    
845
/**
846
 * Get the node object for a menu item.
847
 */
848
function i18n_menu_item_get_node($item) {
849
  return isset($item['router_path']) && $item['router_path'] == 'node/%' ? node_load(arg(1, $item['link_path'])) : NULL;
850
}
851

    
852
/**
853
 * Implements hook_node_presave()
854
 *
855
 * Set menu link language to node language
856
 */
857
function i18n_menu_node_presave($node) {
858
  if (!empty($node->menu) && isset($node->language) && i18n_menu_node_supported_type($node->type)) {
859
    $node->menu['language'] = i18n_object_langcode($node, LANGUAGE_NONE);
860
    // Store node type with menu item so we can quickly access it later.
861
    $node->menu['options']['node_type'] = $node->type;
862
  }
863
}
864

    
865
/**
866
 * Implements hook_node_prepare_translation().
867
 */
868
function i18n_menu_node_prepare_translation($node) {
869
  if (empty($node->menu['mlid']) && !empty($node->translation_source)) {
870
    $tnode = $node->translation_source;
871
    // Prepare the tnode so the menu item will be available.
872
    node_object_prepare($tnode);
873
    $node->menu['link_title'] = $tnode->menu['link_title'];
874
    $node->menu['weight'] = $tnode->menu['weight'];
875
  }
876
}
877

    
878
/**
879
 * Process menu and menu item add/edit form submissions.
880
 *
881
 * @todo See where this fits
882
 */
883
/*
884
function i18n_menu_edit_item_form_submit($form, &$form_state) {
885
  $mid = menu_edit_item_save($form_state['values']);
886
  db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", array($form_state['values']['language'], $mid));
887
  return 'admin/build/menu';
888
}
889
*/
890

    
891
/**
892
 * Load translation set. Menu loading callback.
893
 */
894
function i18n_menu_translation_load($tsid) {
895
  return i18n_translation_set_load($tsid, 'menu_link');
896
}
897

    
898
/**
899
 * Load menu item by path, language
900
 */
901
function i18n_menu_link_load($path, $langcode) {
902
  $query = db_select('menu_links', 'ml');
903
  $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
904
  $query->fields('ml');
905
  // Weight should be taken from {menu_links}, not {menu_router}.
906
  $query->addField('ml', 'weight', 'link_weight');
907
  $query->fields('m');
908
  $query->condition('ml.link_path', $path);
909
  $query->condition('ml.language', $langcode);
910
  if ($item = $query->execute()->fetchAssoc()) {
911
    $item['weight'] = $item['link_weight'];
912
    _menu_link_translate($item);
913
    return $item;
914
  }
915
}
916

    
917
/**
918
 * Implements hook_query_TAG_alter() for features_menu_links.
919
 * Add needed fields to properly serialize localization information.
920
 */
921
function i18n_menu_query_features_menu_link_alter($query) {
922
  $query->fields('menu_links', array('language', 'customized'));
923
}
924

    
925
/**
926
 * Implements hook_query_TAG_alter()
927
 *
928
 * Using tag 'preferred_menu_links' added in menu_link_get_preferred().
929
 * See http://drupal.org/node/1854134
930
 */
931
function i18n_menu_query_preferred_menu_links_alter(QueryAlterableInterface $query) {
932
  global $language;
933
  // Get queried tables.
934
  $tables = $query->getTables();
935

    
936
  foreach ($tables as $alias => $table) {
937
    if ($table['table'] == 'menu_links') {
938
      // Add language filter, ensuring that we don't have any collision when
939
      // determining the active menu trail when there are multiple menu items
940
      // with same link path but different languages.
941
      if ($language) {
942
        $query->condition('language', array($language->language, LANGUAGE_NONE), 'IN');
943
      }
944
      break;
945
    }
946
  }
947
}
948