summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_menu/i18n_menu.module
blob: 8f91fc5793d9320dacc72c34bbebcabc5041154c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
<?php

/**
 * @file
 * Internationalization (i18n) submodule: Menu translation.
 *
 * @author Jose A. Reyero, 2005
 *
 */

/**
 * Implements hook_menu()
 */
function i18n_menu_menu() {
  $items['admin/structure/menu/manage/translation'] = array(
    'title' => 'Translation sets',
    'page callback' => 'i18n_translation_set_list_manage',
    'page arguments' => array('menu_link'),
    'access arguments' => array('administer menu'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/structure/menu/manage/translation/add'] = array(
    'title' => 'Add translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_menu_translation_form'),
    'access arguments' => array('administer menu'),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'i18n_menu.admin.inc',
  );
  $items['admin/structure/menu/manage/translation/edit/%i18n_menu_translation'] = array(
    'title' => 'Edit translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_menu_translation_form', 6),
    'access arguments' => array('administer menu'),
    'type' => MENU_CALLBACK,
    'file' => 'i18n_menu.admin.inc',
  );
  $items['admin/structure/menu/manage/translation/delete/%i18n_menu_translation'] = array(
    'title' => 'Delete translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_translation_set_delete_confirm', 6),
    'access arguments' => array('administer menu'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_menu_alter()
 */
function i18n_menu_menu_alter(&$items) {
  $items['admin/structure/menu/item/%menu_link'] = $items['admin/structure/menu/item/%menu_link/edit'];
  $items['admin/structure/menu/item/%menu_link']['type'] = MENU_CALLBACK;
  $items['admin/structure/menu/item/%menu_link/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
}

/**
 * Implements hook_block_view().
 */
function i18n_menu_block_view_alter(&$data, $block) {
  if (($block->module == 'menu' || $block->module == 'system') && (i18n_menu_mode($block->delta) & I18N_MODE_MULTIPLE)) {
    $menus = menu_get_menus();
    if (isset($menus[$block->delta])) {
      if (empty($block->title)) {
        $data['subject'] = i18n_string_plain(
          array('menu', 'menu', $block->delta, 'title'),
          $menus[$block->delta]
        );
      }
      // Add contextual links for this block.
      if (!empty($data['content'])) {
        $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
      }
    }
  }
}

/**
 * Implements hook_i18n_translate_path()
 */
function i18n_menu_i18n_translate_path($path) {
  $item = i18n_menu_link_load($path, i18n_langcode());
  if ($item && ($set = i18n_translation_object('menu_link', $item))) {
    $links = array();
    foreach ($set->get_translations() as $lang => $link) {
      $links[$lang] = array(
        'href' => $link['link_path'],
        'title' => $link['link_title'],
        'i18n_type' => 'menu_link',
        'i18n_object' => $link,
      );
    }
    return $links;
  }
}

/**
 * Implements hook_menu_insert()
 */
function i18n_menu_menu_insert($menu) {
  i18n_menu_menu_update($menu);
}

/**
 * Implements hook_menu_update()
 */
function i18n_menu_menu_update($menu) {
  // Stores the fields of menu links which need an update.
  $update = array();

  if (!isset($menu['i18n_mode'])) {
    $menu['i18n_mode'] = I18N_MODE_NONE;
  }
  if (!($menu['i18n_mode'] & I18N_MODE_LANGUAGE)) {
    $menu['language'] = LANGUAGE_NONE;
  }
  db_update('menu_custom')
    ->fields(array('language' => $menu['language'], 'i18n_mode' => $menu['i18n_mode']))
    ->condition('menu_name', $menu['menu_name'])
    ->execute();
  if (!$menu['i18n_mode']) {
    $update['language'] = LANGUAGE_NONE;
  }
  elseif ($menu['i18n_mode'] & I18N_MODE_LANGUAGE) {
    $update['language'] = $menu['language'];
  }

  // Non translatable menu.
  if (!($menu['i18n_mode'] & I18N_MODE_TRANSLATE)) {
    $tsids = db_select('menu_links')
      ->fields('menu_links', array('i18n_tsid'))
      ->groupBy('i18n_tsid')
      ->condition('menu_name', $menu['menu_name'])
      ->condition('customized', 1)
      ->condition('i18n_tsid', 0, '<>')
      ->execute()
      ->fetchCol(0);
    if (!empty($tsids)) {
      foreach ($tsids as $tsid) {
        if ($translation_set = i18n_translation_set_load($tsid)) {
          $translation_set->delete();
        }
      }
    }
    $update['i18n_tsid'] = 0;
  }

  if (!empty($update)) {
    db_update('menu_links')
      ->fields($update)
      ->condition('menu_name', $menu['menu_name'])
      ->condition('customized', 1)
      ->execute();
  }

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

  // Clear all menu caches.
  menu_cache_clear_all();
}

/**
 * Implements hook_menu_delete()
 */
function i18n_menu_menu_delete($menu) {
  i18n_string_object_remove('menu', $menu);
}

/**
 * Implements hook_menu_link_alter().
 *
 * This function is invoked from menu_link_save() before default
 * menu link options (menu_name, module, etc.. have been set)
 */
function i18n_menu_menu_link_alter(&$item) {
  // We just make sure every link has a valid language property.
  if (!i18n_object_langcode($item)) {
    $item['language'] = LANGUAGE_NONE;
  }
}

/**
 * Implements hook_menu_link_insert()
 */
function i18n_menu_menu_link_insert($link) {
  i18n_menu_menu_link_update($link);
}

/**
 * Implements hook_menu_link_update().
 */
function i18n_menu_menu_link_update($link) {
  // Stores the fields to update.
  $fields = array();
  $menu_mode = i18n_menu_mode($link['menu_name']);

  if ($menu_mode & I18N_MODE_TRANSLATE && isset($link['language'])) {
    // Multilingual menu links, translatable, it may be part of a
    // translation set.
    if (i18n_object_langcode($link)) {
      if (!empty($link['translation_set'])) {
        // Translation set comes as parameter, we may be creating a translation,
        // add link to the set.
        $translation_set = $link['translation_set'];
        $translation_set
          ->add_item($link)
          ->save(TRUE);
      }
    }
    elseif ($link['language'] === LANGUAGE_NONE && !empty($link['original_item']['i18n_tsid'])) {
      if ($translation_set = i18n_translation_set_load($link['original_item']['i18n_tsid'])) {
        $translation_set->remove_language($link['original_item']['language']);
        // If there are no links left in this translation set, delete the set.
        // Otherwise update the set.
        $translation_set->update_delete();
      }
      $fields['i18n_tsid'] = 0;
    }
  }
  // For multilingual menu items, always set a language and mark them for
  // 'alter' so they can be processed later by
  // hook_translated_link_menu_alter().
  if ($menu_mode) {
    if (!isset($link['language'])) {
      $link['language'] = LANGUAGE_NONE;
    }
    if (_i18n_menu_link_check_alter($link) && empty($link['options']['alter'])) {
      $fields['options'] = $link['options'];
      $fields['options']['alter'] = TRUE;
    }
    // We cannot unmark links for altering because we don't know what other
    // modules use it for.
  }
  // Update language field if the link has a language value.
  if (isset($link['language'])) {
    $fields['language'] = $link['language'];
  }

  if (!empty($fields)) {
    // If link options are to be updated, they need to be serialized.
    if (isset($fields['options'])) {
      $fields['options'] = serialize($fields['options']);
    }
    db_update('menu_links')
      ->fields($fields)
      ->condition('mlid', $link['mlid'])
      ->execute();
  }
  // Update translatable strings if any for customized links that belong to a
  // localizable menu.
  if (_i18n_menu_link_is_localizable($link)) {
    i18n_string_object_update('menu_link', $link);
  }
  else {
    i18n_string_object_remove('menu_link', $link);
  }
}

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

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

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

  i18n_string_object_remove('menu_link', $link);
}

/**
 * Get menu mode or compare with given one
 */
function i18n_menu_mode($name, $mode = NULL) {
  $menu = menu_load($name);
  if (!$menu || !isset($menu['i18n_mode'])) {
    return isset($mode) ? FALSE : I18N_MODE_NONE;
  }
  else {
    return isset($mode) ? $menu['i18n_mode'] & $mode : $menu['i18n_mode'];
  }
}

/**
 * Implements hook_translated_menu_link_alter().
 *
 * Translate localizable menu links on the fly.
 * Filter out items that have a different language from current interface.
 *
 * @see i18n_menu_menu_link_alter()
 */
function i18n_menu_translated_menu_link_alter(&$item) {
  // Only process links to be displayed not processed before by i18n_menu.
  if (_i18n_menu_link_process($item)) {
    if (!_i18n_menu_link_is_visible($item)) {
      $item['hidden'] = TRUE;
    }
    elseif (_i18n_menu_link_is_localizable($item)) {
      // Item has undefined language, it is a candidate for localization.
      _i18n_menu_link_localize($item);
    }
  }
}

/**
 * Implements hook_help().
 */
function i18n_menu_help($path, $arg) {
  switch ($path) {
    case 'admin/help#i18n_menu' :
      $output = '<p>' . t('This module adds support for multilingual menus. You can setup multilingual options for each menu:') . '</p>';
      $output .= '<ul>';
      $output .= '<li>' . t('Menus can be fully multilingual with translatable (or localized) menu items.') . '</li>';
      $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>';
      $output .= '<li>' . t('Menus can also be configured to have no translations.') . '</li>';
      $output .= '</ul>';
      $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>';
      $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>';
      return $output;

    case 'admin/config/regional/i18n':
      $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>';
      return $output;
  }
}

/**
 * Implements hook_variable_info_alter()
 */
function i18n_menu_variable_info_alter(&$variables, $options) {
  // Make menu variables translatable
  $variables['menu_main_links_source']['localize'] = TRUE;
  $variables['menu_secondary_links_source']['localize'] = TRUE;
  $variables['menu_parent_[node_type]']['localize'] = TRUE;
  $variables['menu_options_[node_type]']['localize'] = TRUE;
}

/**
 * Get localized menu tree.
 *
 * @param string $menu_name
 *   The menu the translated tree has to be fetched from.
 * @param string $langcode
 *   Optional language code to get the menu in, defaults to request language.
 * @param bool $reset
 *   Whether to reset the internal i18n_menu_translated_tree cache.
 */
function i18n_menu_translated_tree($menu_name, $langcode = NULL, $reset = FALSE) {
  $menu_output = &drupal_static(__FUNCTION__);
  $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  if (!isset($menu_output[$langcode][$menu_name]) || $reset) {
    $tree = menu_tree_page_data($menu_name);
    $tree = i18n_menu_localize_tree($tree, $langcode);
    $menu_output[$langcode][$menu_name] = menu_tree_output($tree);
  }
  return $menu_output[$langcode][$menu_name];
}

/**
 * Localize menu tree.
 */
function i18n_menu_localize_tree($tree, $langcode = NULL) {
  $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  foreach ($tree as $index => &$item) {
    $link = $item['link'];
    // We only process links that are visible and not processed before.
    if (_i18n_menu_link_process($item['link'])) {
      if (!_i18n_menu_link_is_visible($item['link'], $langcode)) {
        // Remove links for other languages than current.
        // Links with language wont be localized.
        unset($tree[$index]);
        // @todo Research whether the above has any advantage over:
        // $item['hidden'] = TRUE;
      }
      else {
        if (_i18n_menu_link_is_localizable($item['link'])) {
          // Item has undefined language, it is a candidate for localization.
          _i18n_menu_link_localize($item['link'], $langcode);
        }
        // Localize subtree.
        if (!empty($item['below'])) {
          $item['below'] = i18n_menu_localize_tree($item['below'], $langcode);
        }
      }
    }
  }
  return $tree;
}

/**
 * Localize menu renderable array
 */
function i18n_menu_localize_elements(&$elements) {
  foreach (element_children($elements) as $mlid) {
    $elements[$mlid]['#title'] = i18n_string(array('menu', 'item', $mlid, 'title'), $elements[$mlid]['#title']);
    if (!empty($tree[$mlid]['#localized_options']['attributes']['title'])) {
      $elements[$mlid]['#localized_options']['attributes']['title'] = i18n_string(array('menu', 'item', $mlid, 'description'), $tree[$mlid]['#localized_options']['attributes']['title']);
    }
    i18n_menu_localize_elements($elements[$mlid]);
  }
}

/**
 * Return an array of localized links for a navigation menu.
 *
 * Localized version of menu_navigation_links()
 */
function i18n_menu_navigation_links($menu_name, $level = 0) {
  // Don't even bother querying the menu table if no menu is specified.
  if (empty($menu_name)) {
    return array();
  }

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

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

  // Create a single level of links.
  $router_item = menu_get_item();
  $links = array();
  foreach ($tree as $item) {
    if (!$item['link']['hidden']) {
      $class = '';
      $l = $item['link']['localized_options'];
      $l['href'] = $item['link']['href'];
      $l['title'] = $item['link']['title'];
      if ($item['link']['in_active_trail']) {
        $class = ' active-trail';
        $l['attributes']['class'][] = 'active-trail';
      }
      // Normally, l() compares the href of every link with $_GET['q'] and sets
      // the active class accordingly. But local tasks do not appear in menu
      // trees, so if the current path is a local task, and this link is its
      // tab root, then we have to set the class manually.
      if ($item['link']['href'] == $router_item['tab_root_href'] && $item['link']['href'] != $_GET['q']) {
        $l['attributes']['class'][] = 'active';
      }
      // Keyed with the unique mlid to generate classes in theme_links().
      $links['menu-' . $item['link']['mlid'] . $class] = $l;
    }
  }
  return $links;
}

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

/**
 * Localize menu item title and description.
 *
 * This will be invoked always after _menu_item_localize()
 *
 * Link properties to manage:
 * - title, menu router title
 * - link_title, menu link title
 * - options.attributes.title, menu link description.
 * - localized_options.attributes.title,
 *
 * @see _menu_item_localize()
 * @see _menu_link_translate()
 */
function _i18n_menu_link_localize(&$link, $langcode = NULL) {
  // Only translate title if it has no special callback.
  if (empty($link['title callback']) || $link['title callback'] === 't') {
    $link['title'] = _i18n_menu_link_title($link, $langcode);
  }
  if ($description = _i18n_menu_link_description($link, $langcode)) {
    $link['localized_options']['attributes']['title'] = $description;
  }
}

/**
 * Get localized menu description
 */
function _i18n_menu_link_description($link, $langcode = NULL) {
  if (!empty($link['options']['attributes']['title'])) {
    $key = i18n_object_info('menu_link', 'key');
    return i18n_string_translate(array('menu', 'item', $link[$key], 'description'), $link['options']['attributes']['title'], array('langcode' => $langcode));
  }
  else {
    return NULL;
  }
}

/**
 * Check whether this link is to be processed by i18n_menu and start processing.
 */
function _i18n_menu_link_process(&$link) {
  // Only visible links that have a language property and haven't been processed
  // before. We also check that they belong to a menu with language options.
  if (empty($link['i18n_menu']) && !empty($link['language']) && !empty($link['access']) && empty($link['hidden']) && i18n_menu_mode($link['menu_name'])) {
    // Mark so it won't be processed twice.
    $link['i18n_menu'] = TRUE;
    // Skip if administering this menu or this menu item.
    if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'menu') {
      if (arg(3) == 'manage' && $link['menu_name'] == arg(4)) {
        return FALSE;
      }
      elseif (arg(3) == 'item' && arg(4) == $link['mlid']) {
        return FALSE;
      }
    }
    return TRUE;
  }
  else {
    return FALSE;
  }
}

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

/**
 * Check whether this link should be localized by i18n_menu.
 *
 * @param array $link
 *   Menu link array.
 * @param bool $check_strings
 *   Whether to check if the link has actually localizable strings. Since this
 *   is a more expensive operation, it will be just checked when editing menu
 *   items.
 *
 * @return boolean
 *   Returns TRUE if link is localizable.
 */
function _i18n_menu_link_is_localizable($link, $check_strings = FALSE) {
  return !empty($link['customized']) && !i18n_object_langcode($link) && i18n_menu_mode($link['menu_name'], I18N_MODE_LOCALIZE) &&
  (!$check_strings || _i18n_menu_link_localizable_properties($link));
}

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

/**
 * Get localizable properties for menu link checking agains the router item.
 */
function _i18n_menu_link_localizable_properties($link) {
  $props = array();
  $router = !empty($link['router_path']) ? _i18n_menu_get_router($link['router_path']) : NULL;
  if (!empty($link['link_title'])) {
    // If the title callback is 't' and the link title matches the router title
    // it will be localized by core, not by i18n_menu.
    if (!$router ||
        (empty($router['title_callback']) || $router['title_callback'] != 't') ||
        (empty($router['title']) || $router['title'] != $link['link_title'])
    ) {
      $props[] = 'title';
    }
  }
  if (!empty($link['options']['attributes']['title'])) {
    // If the description matches the router description, it will be localized
    // by core.
    if (!$router || empty($router['description']) || $router['description'] != $link['options']['attributes']['title']) {
      $props[] = 'description';
    }
  }
  return $props;
}

/**
 * Get the menu router for this router path.
 *
 * We need the untranslated title to compare, and this will be fast.
 * There's no api function to do this?
 *
 * @param string $path
 *   The path to fetch from the router.
 */
function _i18n_menu_get_router($path) {
  $cache = &drupal_static(__FUNCTION__, array());
  if (!array_key_exists($path, $cache)) {
    $cache[$path] = db_select('menu_router', 'mr')
      ->fields('mr', array('title', 'title_callback', 'description'))
      ->condition('path', $path)
      ->execute()
      ->fetchAssoc();
  }
  return $cache[$path];
}

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

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

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

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

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

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

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

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

      // If the current term is part of a translation set,
      // remove all other languages of the option list.
      if ($translations = $translation_set->get_translations()) {
        unset($form['i18n']['language']['#options'][LANGUAGE_NONE]);
        foreach ($translations as $langcode => $translation) {
          if ($translation['mlid'] !== $item['mlid']) {
            unset($form['i18n']['language']['#options'][$langcode]);
          }
        }
      }
    }
  }
  else {
    $form['language'] = array(
      '#type' => 'value',
      '#value' => $item['language'],
    );
  }
  if ($node_item && i18n_langcode($item['language'])) {
    $form['i18n']['message'] = array(
      '#type' => 'item',
      '#title' => t('Language'),
      '#markup' => i18n_language_name($item['language']),
      '#description' => t('This menu item belongs to a node, so it will have the same language as the node and cannot be localized.'),
    );
  }
  array_unshift($form['#validate'], 'i18n_menu_menu_item_prepare_normal_path');
}

/**
 * Implements hook_form_FORM_ID_alter().
 * FORM_ID = menu-overview-form.
 * Add a "translate" link in operations column for each menu item.
 */
function i18n_menu_form_menu_overview_form_alter(&$form, &$form_state) {
  foreach (element_children($form) as $element) {
    if (substr($element, 0, 5) == 'mlid:') {
      $mlid = $form[$element]['#item']['mlid'];
      if (i18n_get_object('menu', $mlid)->get_translate_access()) {
        $form[$element]['operations']['translate'] = array(
          '#type' => 'link',
          '#title' => t('translate'),
          '#href' => "admin/structure/menu/item/{$mlid}/translate",
        );
      }
    }
  }
}

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

/**
 * Get language for menu item
 */
function i18n_menu_item_get_language($item) {
  if (isset($item['language'])) {
    return $item['language'];
  }
  else {
    $menu = menu_load($item['menu_name']);
    if (!isset($menu['i18n_mode'])) {
      return LANGUAGE_NONE;
    }
    switch ($menu['i18n_mode']) {
      case I18N_MODE_LANGUAGE:
        return $menu['language'];
      case I18N_MODE_NONE:
      case I18N_MODE_LOCALIZE:
        return LANGUAGE_NONE;
      default:
        if (!empty($item['mlid'])) {
          return db_select('menu_links', 'm')
            ->fields('m', array('language'))
            ->condition('mlid', $item['mlid'])
            ->execute()
            ->fetchField();
        }
        else {
          return LANGUAGE_NONE;
        }
    }
  }
}

/**
 * Implements hook_form_node_form_alter().
 *
 * Add language to menu settings of the node form, as well as setting defaults
 * to match the translated item's menu settings.
 */
function i18n_menu_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['menu'])) {
    $node = $form['#node'];
    $link = $node->menu;
    if (!empty($link['mlid'])) {
      // Preserve the menu item language whatever it is.
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $link['language']);
    }
    elseif (i18n_menu_node_supported_type($node->type)) {
      // Set menu language to node language but only if it is a supported node type.
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $node->language);
    }
    else {
      $form['menu']['link']['language'] = array('#type' => 'value', '#value' => LANGUAGE_NONE);
    }
    // Customized must be set to 1 to save language.
    $form['menu']['link']['customized'] = array('#type' => 'value', '#value' => 1);
  }
}

/**
 * Check whether a node type has multilingual support (but not entity translation).
 */
function i18n_menu_node_supported_type($type) {
  $supported = &drupal_static(__FUNCTION__);
  if (!isset($supported[$type])) {
    $mode = variable_get('language_content_type_' . $type, 0);
    $supported[$type] = $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
  }
  return $supported[$type];
}

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

/**
 * Implements hook_node_presave()
 *
 * Set menu link language to node language
 */
function i18n_menu_node_presave($node) {
  if (!empty($node->menu) && isset($node->language) && i18n_menu_node_supported_type($node->type)) {
    $node->menu['language'] = i18n_object_langcode($node, LANGUAGE_NONE);
    // Store node type with menu item so we can quickly access it later.
    $node->menu['options']['node_type'] = $node->type;
  }
}

/**
 * Implements hook_node_prepare_translation().
 */
function i18n_menu_node_prepare_translation($node) {
  if (empty($node->menu['mlid']) && !empty($node->translation_source)) {
    $tnode = $node->translation_source;
    // Prepare the tnode so the menu item will be available.
    node_object_prepare($tnode);
    $node->menu['link_title'] = $tnode->menu['link_title'];
    $node->menu['weight'] = $tnode->menu['weight'];
  }
}

/**
 * Process menu and menu item add/edit form submissions.
 *
 * @todo See where this fits
 */
/*
function i18n_menu_edit_item_form_submit($form, &$form_state) {
  $mid = menu_edit_item_save($form_state['values']);
  db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", array($form_state['values']['language'], $mid));
  return 'admin/build/menu';
}
*/

/**
 * Load translation set. Menu loading callback.
 */
function i18n_menu_translation_load($tsid) {
  return i18n_translation_set_load($tsid, 'menu_link');
}

/**
 * Load menu item by path, language
 */
function i18n_menu_link_load($path, $langcode) {
  $query = db_select('menu_links', 'ml');
  $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  $query->fields('ml');
  // Weight should be taken from {menu_links}, not {menu_router}.
  $query->addField('ml', 'weight', 'link_weight');
  $query->fields('m');
  $query->condition('ml.link_path', $path);
  $query->condition('ml.language', $langcode);
  if ($item = $query->execute()->fetchAssoc()) {
    $item['weight'] = $item['link_weight'];
    _menu_link_translate($item);
    return $item;
  }
}

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

/**
 * Implements hook_init().
 */
function i18n_menu_init() {

  // The only way to override the default preferred menu link for a path is to
  // inject it into the static cache of the function menu_link_get_preferred().

  // The problem with the default implementation is that it does not take the
  // language of a menu link into account. Whe having different menu trees for
  // different menus, this means that the active trail will not work for all but
  // one language.

  // The code below is identical to the mentioned function except the added
  // language condition on the query.

  // TODO: Adding an alter tag to the query would allow to do this with a simple
  // hook_query_alter() implementation.

  $preferred_links = &drupal_static('menu_link_get_preferred');

  $path = $_GET['q'];

  // Look for the correct menu link by building a list of candidate paths,
  // which are ordered by priority (translated hrefs are preferred over
  // untranslated paths). Afterwards, the most relevant path is picked from
  // the menus, ordered by menu preference.
  $item = menu_get_item($path);
  $path_candidates = array();
  // 1. The current item href.
  $path_candidates[$item['href']] = $item['href'];
  // 2. The tab root href of the current item (if any).
  if ($item['tab_parent'] && ($tab_root = menu_get_item($item['tab_root_href']))) {
    $path_candidates[$tab_root['href']] = $tab_root['href'];
  }
  // 3. The current item path (with wildcards).
  $path_candidates[$item['path']] = $item['path'];
  // 4. The tab root path of the current item (if any).
  if (!empty($tab_root)) {
    $path_candidates[$tab_root['path']] = $tab_root['path'];
  }
  // Retrieve a list of menu names, ordered by preference.
  $menu_names = menu_get_active_menu_names();
  // Use an illegal menu name as the key for the preferred menu link.
  $selected_menu = MENU_PREFERRED_LINK;
  // Put the selected menu at the front of the list.
  array_unshift($menu_names, $selected_menu);

  $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  $query->fields('ml');
  // Weight must be taken from {menu_links}, not {menu_router}.
  $query->addField('ml', 'weight', 'link_weight');
  $query->fields('m');
  $query->condition('ml.link_path', $path_candidates, 'IN');

  // Only look menu links with none or the current language.
  $query->condition('ml.language', array(LANGUAGE_NONE, i18n_language_interface()->language), 'IN');

  // Sort candidates by link path and menu name.
  $candidates = array();
  foreach ($query->execute() as $candidate) {
    $candidate['weight'] = $candidate['link_weight'];
    $candidates[$candidate['link_path']][$candidate['menu_name']] = $candidate;
    // Add any menus not already in the menu name search list.
    if (!in_array($candidate['menu_name'], $menu_names)) {
      $menu_names[] = $candidate['menu_name'];
    }
  }

  // Store the most specific link for each menu. Also save the most specific
  // link of the most preferred menu in $preferred_link.
  foreach ($path_candidates as $link_path) {
    if (isset($candidates[$link_path])) {
      foreach ($menu_names as $menu_name) {
        if (empty($preferred_links[$path][$menu_name]) && isset($candidates[$link_path][$menu_name])) {
          $candidate_item = $candidates[$link_path][$menu_name];
          $map = explode('/', $path);
          _menu_translate($candidate_item, $map);
          if ($candidate_item['access']) {
            $preferred_links[$path][$menu_name] = $candidate_item;
            if (empty($preferred_links[$path][MENU_PREFERRED_LINK])) {
              // Store the most specific link.
              $preferred_links[$path][MENU_PREFERRED_LINK] = $candidate_item;
            }
          }
        }
      }
    }
  }
}