i18n.pages.inc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * Generic translation pages
  5. *
  6. * It handles generic 'translation' tabs, redirecting to the right module depending on
  7. * object type and properties.
  8. */
  9. /**
  10. * Create menu items for translatable objecs
  11. */
  12. function i18n_page_menu_items() {
  13. $items = array();
  14. // Menus are rebuilt right after other modules are disabled, if no reset we get tabs for disabled modules.
  15. drupal_static_reset('i18n_object_info');
  16. foreach (i18n_object_info() as $type => $info) {
  17. // These objects should have a 'translate tab' property
  18. if (!empty($info['translate tab'])) {
  19. $path = $info['translate tab'];
  20. $localize = module_exists('i18n_string') && !empty($info['string translation']);
  21. $translate = module_exists('i18n_translation') && i18n_translation_set_info($type);
  22. if ($translate && $localize) {
  23. $page_callback = 'i18n_page_translate_tab';
  24. }
  25. elseif ($translate) {
  26. $page_callback = 'i18n_page_translate_translation';
  27. }
  28. elseif ($localize) {
  29. $page_callback = 'i18n_page_translate_localize';
  30. }
  31. // Find the position for the object placeholder. We assume the first one.
  32. $placeholder = key($info['placeholders']);
  33. $parts = explode('/', $path);
  34. $position = array_search($placeholder, $parts);
  35. // Now create items with the right callbacks
  36. if ($translate || $localize) {
  37. $items[$path] = array(
  38. 'title' => 'Translate',
  39. 'page callback' => $page_callback,
  40. 'page arguments' => array($type, $position),
  41. 'access callback' => 'i18n_object_translate_access',
  42. 'access arguments' => array($type, $position),
  43. 'type' => MENU_LOCAL_TASK,
  44. 'file' => 'i18n.pages.inc',
  45. 'weight' => 10,
  46. );
  47. }
  48. if ($localize) {
  49. $items[$path . '/%i18n_language'] = array(
  50. 'title' => 'Translate',
  51. 'page callback' => $page_callback,
  52. 'page arguments' => array($type, $position, count($parts)),
  53. 'access callback' => 'i18n_object_translate_access',
  54. 'access arguments' => array($type, $position),
  55. 'type' => MENU_CALLBACK,
  56. 'file' => 'i18n.pages.inc',
  57. );
  58. }
  59. }
  60. }
  61. return $items;
  62. }
  63. /**
  64. * Translate or localize page for object
  65. */
  66. function i18n_page_translate_tab($type, $object, $language = NULL) {
  67. // Check whether object should be part of a translation set
  68. switch (i18n_object($type, $object)->get_translate_mode()) {
  69. case I18N_MODE_TRANSLATE:
  70. return i18n_page_translate_translation($type, $object);
  71. case I18N_MODE_LOCALIZE:
  72. return i18n_page_translate_localize($type, $object, $language);
  73. default:
  74. drupal_access_denied();
  75. }
  76. }
  77. /**
  78. * Translate object, create translation set
  79. */
  80. function i18n_page_translate_translation($type, $object) {
  81. module_load_include('pages.inc', 'i18n_translation');
  82. return i18n_translation_object_translate_page($type, $object);
  83. }
  84. /**
  85. * Translate object, string translation
  86. */
  87. function i18n_page_translate_localize($type, $object, $language = NULL) {
  88. module_load_include('pages.inc', 'i18n_string');
  89. return i18n_string_object_translate_page($type, $object, $language);
  90. }