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/locale/locale.module | 45 ++++++++++++++++++++++++++++++++++ modules/translation/translation.module | 41 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 7c31351ea..c889241e7 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -53,6 +53,12 @@ function locale_help($path, $arg) { return '

'. t("This page allows you to export Drupal strings. The first option is to export a translation so it can be shared. The second option generates a translation template, which contains all Drupal strings, but without their translations. You can use this template to start a new translation using various software packages designed for this task.") .'

'; case 'admin/build/translate/search': return '

'. t("It is often convenient to get the strings from your setup on the export page, and use a desktop Gettext translation editor to edit the translations. On this page you can search in the translated and untranslated strings, and the default English texts provided by Drupal.", array("@export" => url("admin/build/translate/export"))) .'

'; + + case 'admin/build/block/configure': + if ($arg[4] == 'locale' && $arg[5] == 0) { + return '

'. t("This block is only shown if you have at least two languages enabled and you have a language negotiation setting different from 'none', so you have different web addresses for different language versions.", array('@languages' => url('admin/settings/language'), '@configuration' => url('admin/settings/language/configure'))) .'

'; + } + break; } } @@ -491,3 +497,42 @@ function _locale_batch_import($filepath, &$context) { $context['results'][] = $filepath; } } + +// --------------------------------------------------------------------------------- +// Language switcher block + +/** + * Implementation of hook_block(). + * Displays a language switcher. Translation links may be provided by other modules. + */ +function locale_block($op = 'list', $delta = 0) { + if ($op == 'list') { + $block[0]['info'] = t('Language switcher'); + return $block; + } + + // Only show if we have at least two languages and language dependent + // web addresses, so we can actually link to other language versions. + elseif ($op == 'view' && variable_get('language_count', 1) > 1 && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) { + $languages = language_list('enabled'); + $links = array(); + foreach ($languages[1] as $language) { + $links[$language->language] = array( + 'href' => $_GET['q'], + 'title' => $language->native, + 'language' => $language, + 'attributes' => array('class' => 'language-link'), + ); + } + + // Allow modules to provide translations for specific links. + // A translation link may need to point to a different path or use + // a translated link text before going through l(), which will just + // handle the path aliases. + drupal_alter('translation_link', $links, $_GET['q']); + + $block['subject'] = t('Languages'); + $block['content'] = theme('links', $links, array()); + return $block; + } +} 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