From a3d75e547f62174fe9fa2b5c3f9684b620612b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Hojtsy?= Date: Sat, 30 Jun 2007 21:00:51 +0000 Subject: #141996 by Jose A Reyero and myself: translation block to switch interface languages by default, and even content languages, when a translation module provides the alternate links --- modules/translation/translation.module | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'modules/translation/translation.module') diff --git a/modules/translation/translation.module b/modules/translation/translation.module index 674492d98..c7f464cd2 100644 --- a/modules/translation/translation.module +++ b/modules/translation/translation.module @@ -336,3 +336,44 @@ function translation_node_get_translations($tnid) { function translation_supported_type($type) { return variable_get('language_' . $type, 0) == TRANSLATION_ENABLED; } + +/** + * Return paths of all translations of a node, based on + * its Drupal path. + * + * @param $path + * A Drupal path, for example node/432. + * @return + * An array of paths of translations of the node accessible + * to the current user keyed with language codes. + */ +function translation_path_get_translations($path) { + $paths = array(); + // Check for a node related path, and for its translations. + if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) { + foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) { + $paths[$language] = 'node/'. $translation_node->nid . $matches[2]; + } + } + return $paths; +} + +/** + * Implementation of hook_alter_translation_link(). + * + * Replaces links with pointers to translated versions of the content. + */ +function translation_translation_link_alter(&$links, $path) { + if ($paths = translation_path_get_translations($path)) { + foreach ($links as $langcode => $link) { + if (isset($paths[$langcode])) { + // Translation in a different node. + $links[$langcode]['href'] = $paths[$langcode]; + } + else { + // No translation in this language, or no permission to view. + unset($links[$langcode]); + } + } + } +} -- cgit v1.2.3