i18n_forum.module 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * i18n forum module
  5. *
  6. * Internationalization (i18n) package.
  7. */
  8. /**
  9. * Implements hook_help().
  10. */
  11. function i18n_forum_help($path, $arg) {
  12. if ($path == 'admin/structure/forum' && ($vocabulary = i18n_forum_vocabulary())) {
  13. $base_path = 'admin/structure/taxonomy/' . $vocabulary->machine_name;
  14. return t('To translate the forum, <a href="@edit">edit and make it translatable</a>, then <a href="@translate">translate the forum</a> and <a href="@list">its containers and sub-forums</a> on the taxonomy administration page.', array(
  15. '@edit' => url($base_path . '/edit'),
  16. '@translate' => url($base_path . '/translate'),
  17. '@list' => url($base_path . '/list'))
  18. );
  19. }
  20. }
  21. /**
  22. * Implements hook_menu_local_tasks_alter().
  23. */
  24. function i18n_forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  25. // Translate link to 'node/add/forum' on 'forum' sub-pages.
  26. if ($root_path == 'forum' || $root_path == 'forum/%') {
  27. $tid = (isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0);
  28. $forum_term = forum_forum_load($tid);
  29. if ($forum_term) {
  30. // Loop through all bundles for forum taxonomy vocabulary field.
  31. $vid = variable_get('forum_nav_vocabulary', 0);
  32. if ($vid && ($vocabulary = taxonomy_vocabulary_load($vid)) && ($field = field_info_field('taxonomy_' . $vocabulary->machine_name))) {
  33. foreach ($field['bundles']['node'] as $type) {
  34. if (isset($data['actions']['output'][$type])) {
  35. $data['actions']['output'][$type]['#link']['title'] = t('Add new @node_type', array('@node_type' => i18n_node_type_name($type)));
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_form_FORM_ID_alter()
  44. */
  45. function i18n_forum_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
  46. $vid = variable_get('forum_nav_vocabulary', 0);
  47. if ($vid && !isset($form_state['confirm_delete']) && isset($form['vid']) && $form['vid']['#value'] == $vid) {
  48. // Only two options for this vocabulary
  49. $replacements = array(
  50. '@item_name_multiple' => t('forum containers'),
  51. '@item_name_multiple_capitalized' => t('Forum containers'),
  52. );
  53. $form['i18n_translation']['i18n_mode']['#options'] = i18n_translation_options_list($replacements, array(I18N_MODE_LOCALIZE, I18N_MODE_TRANSLATE));
  54. }
  55. }
  56. /**
  57. * Implements hook_node_view().
  58. *
  59. * Localize breadcrumb for forum nodes.
  60. */
  61. function i18n_forum_node_view($node, $view_mode, $langcode) {
  62. if (_forum_node_check_node_type($node)) {
  63. if ($view_mode == 'full' && node_is_page($node)) {
  64. $vid = variable_get('forum_nav_vocabulary', 0);
  65. $vocabulary = taxonomy_vocabulary_load($vid);
  66. // Breadcrumb navigation
  67. $breadcrumb[] = l(t('Home'), NULL);
  68. $breadcrumb[] = l(i18n_taxonomy_vocabulary_name($vocabulary), 'forum');
  69. if ($parents = taxonomy_get_parents_all($node->forum_tid)) {
  70. $parents = array_reverse($parents);
  71. foreach ($parents as $parent) {
  72. $breadcrumb[] = l(i18n_taxonomy_term_name($parent), 'forum/' . $parent->tid);
  73. }
  74. }
  75. drupal_set_breadcrumb($breadcrumb);
  76. }
  77. }
  78. }
  79. /**
  80. * Implements hook_i18n_translate_path()
  81. */
  82. function i18n_forum_i18n_translate_path($path) {
  83. if (strpos($path, 'forum/') === 0 && i18n_forum_mode() & I18N_MODE_TRANSLATE) {
  84. return i18n_taxonomy_translate_path($path, 'forum/');
  85. }
  86. }
  87. /**
  88. * Translate forums list.
  89. */
  90. function i18n_forum_preprocess_forum_list(&$variables) {
  91. if (i18n_forum_mode() & I18N_MODE_LOCALIZE) {
  92. foreach ($variables['forums'] as $id => $forum) {
  93. $variables['forums'][$id]->description = i18n_string('taxonomy:term:' . $forum->tid . ':description', $forum->description);
  94. $variables['forums'][$id]->name = i18n_string('taxonomy:term:' . $forum->tid . ':name', $forum->name);
  95. }
  96. }
  97. }
  98. /**
  99. * Translate forum page.
  100. */
  101. function i18n_forum_preprocess_forums(&$variables) {
  102. if (i18n_forum_mode()) {
  103. $vocabulary = i18n_forum_vocabulary();
  104. if (isset($variables['links']['forum'])) {
  105. $variables['links']['forum']['title'] = i18n_string('nodetype:type:forum:post_button', 'Post new Forum topic');
  106. }
  107. // This one is from advanced forum, http://drupal.org/project/advanced_forum
  108. if (!empty($variables['forum_description'])) {
  109. $variables['forum_description'] = i18n_string('taxonomy:term:' . $variables['tid'] . ':description', $variables['forum_description']);
  110. }
  111. // Translate breadrumb and page title.
  112. $title = $vocabulary_name = !empty($vocabulary->name) ? i18n_taxonomy_vocabulary_name($vocabulary) : '';
  113. $breadcrumb[] = l(t('Home'), NULL);
  114. if ($variables['tid']) {
  115. $breadcrumb[] = l($vocabulary_name, 'forum');
  116. }
  117. if ($variables['parents']) {
  118. $variables['parents'] = array_reverse($variables['parents']);
  119. foreach ($variables['parents'] as $p) {
  120. if ($p->tid == $variables['tid']) {
  121. $title = i18n_taxonomy_term_name($p);
  122. }
  123. else {
  124. $breadcrumb[] = l(i18n_taxonomy_term_name($p), 'forum/' . $p->tid);
  125. }
  126. }
  127. }
  128. drupal_set_breadcrumb($breadcrumb);
  129. drupal_set_title($title);
  130. }
  131. }
  132. /**
  133. * Get forum vocabulary.
  134. */
  135. function i18n_forum_vocabulary() {
  136. if ($vid = variable_get('forum_nav_vocabulary', 0)) {
  137. return taxonomy_vocabulary_load($vid);
  138. }
  139. else {
  140. return NULL;
  141. }
  142. }
  143. /**
  144. * Get forum vocabulary translation mode.
  145. */
  146. function i18n_forum_mode($mode = NULL) {
  147. if ($vocabulary = i18n_forum_vocabulary()) {
  148. return i18n_taxonomy_vocabulary_mode($vocabulary);
  149. }
  150. else {
  151. return I18N_MODE_NONE;
  152. }
  153. }