-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_i18n_redirect.module
51 lines (45 loc) · 1.54 KB
/
custom_i18n_redirect.module
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
<?php
/**
* @file
* Internationalization (i18n) module.
*
* Redirect to language path when we have a translation for the current language.
*/
/**
* Implements hook_help().
*/
function custom_i18n_redirect_help($path, $arg) {
switch ($path) {
case 'admin/config/regional/i18n':
if (!module_exists('i18n_node')) {
$output = '<p>' . t('To have <em>Translation redirect</em> working with your content you should <a href="@admin_modules">enable the <em>Multilingual content</em> module</a>.', array('@admin_modules' => url('admin/modules'))) . '</p>';
return $output;
}
}
}
/**
* Implements hook_init()
*/
function custom_i18n_redirect_init() {
$path = $_GET['q'];
$language = i18n_language_interface();
// Not for logged in users nor for home page
if (!$path || drupal_is_front_page() || !empty($GLOBALS['user']->uid)) {
return;
}
$translations = i18n_get_path_translations($path);
if (isset($translations[$language->language]) && $translations[$language->language]['href'] != $path) {
drupal_goto($translations[$language->language]['href'], array('language' => $language), 301);
}
else {
// Here we working with node, since question about node redirections
$node = menu_get_object();
if ($node && $node->language && ($node->language != $language->language)) {
$languages = language_list();
if (isset($languages[$node->language])) {
$language = $languages[$node->language];
drupal_goto('node/' . $node->nid, array('language' => $language), 301);
}
}
}
}