Answers:
接下来将其添加到主题的template.php文件中:
<?php
function YOURTHEMENAME_links__locale_block($variables) {
foreach($variables['links'] as $key => $lang) {
if (isset($lang['attributes']['class']) && in_array('locale-untranslated', $lang['attributes']['class'])) {
// Set here any page link.
$variables['links'][$key]['href'] = '<front>';
}
}
return theme_links($variables);
}
现在有一个用于此的模块-Language Switcher Fallback。完全按照您的要求进行操作,如果没有翻译到节点,则将用户定向到首页。看到这里https://drupal.org/project/language_switcher_fallback
最后,我使用了这种方法。
function YOURTHEME_language_switch_links_alter(array &$links, $type, $path) {
$language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
$node = node_load((int) $matches[1]);
if (empty($node->tnid)) {
// If the node cannot be found nothing needs to be done. If it does not
// have translations it might be a language neutral node, in which case we
// must leave the language switch links unaltered. This is true also for
// nodes not having translation support enabled.
if (empty($node) || entity_language('node', $node) == LANGUAGE_NONE || !translation_supported_type($node->type)) {
return;
}
$langcode = entity_language('node', $node);
$translations = array($langcode => $node);
}
else {
$translations = translation_node_get_translations($node->tnid);
}
foreach ($links as $langcode => $link) {
if (isset($translations[$langcode]) && $translations[$langcode]->status) {
// Translation in a different node.
$links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
}
else {
// No translation in this language, or no permission to view.
$links[$langcode]['href'] = '<front>';
}
}
}
}
它将替换原始翻译模块中的此代码片段。
else {
// No translation in this language, or no permission to view.
unset($links[$langcode]['href']);
$links[$langcode]['attributes']['class'][] = 'locale-untranslated';
我很确定其他解决方案会更漂亮,但是该解决方案也可以正常工作。