Hi everybody,

I am having a seemingly simply problem yet do not know how to solve it properly: Into a certain node's body text I need to insert a link to another node (to node 1234, say). This latter node (node 1234) is in English but it has several translations (node 1235 = French, etc.). What I want is to insert a HTML link into the original node that points to the translation of node 1234 that is in the current locale. So if the visitor is viewing the original node in French and clicks on the link, she'll be directed to node 1235; if the visitor is viewing the original node in English, she'd be directed to 1234.

What do I have to enter in the href of the HTML link in order to get this behaviour?

Many thanks for a hint!
Kaspar

Comments

stmh’s picture

Here's a small module which will solve your problem (I had it too) Perhaps it can be integrated with localizer.

1) paste this code into a file called localizer-link.info

name = Localizer Link
description = provides a menu callback to load a localized page from a given path
dependencies = localizer localizernode
package = Localizer
version = "1.0dev"

2.) paste this code into a file named localizer-link.module

<?php

/**
 * implementation of hook_menu, registers a menu-callback localized, which will jump to the localized page
 */
function localizer_link_menu($may_cache) {
  
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'localized',
      'title' => t('load a localized node'),
      'callback' => 'localizer_link_load',
      'access' => user_access('access localizer'),
      'type' => MENU_CALLBACK);
  
  }
  return $items;
}


/**
 * gets the localized path of a gien path and redirects to the localized page (if any)
 */
function localizer_link_load() {
  $args = func_get_args();
  $path = implode('/', $args);
  $target = localizernode_get_localizedpath($path, localizer_get_uilocale());
  drupal_goto($target);
}

3.) create a folder in your modules-directory called localizer_link
4.) copy the two created files there
5.) got to admin/build/modules and enable the localizer link module

To insert links into your posts use "/localized/node/1234" instead of "/node/1234"

if node 1234 has a translation for the current chosen locale, then you'll be redirected to the translated node instead.

Note: this works also for path-aliases, say:
node 1234 has an alias "mysuperalias"
then localized/mysuperalias will jump to the translated node.

Another note: this works vice-versa, say node 1235 is a translation of node 1234 then localized/1235 will jump to 1234 if needed.

Hope that helps,
Stephan

Roberto Gerola’s picture

Yes, this is a very good idea.

I have only to decide if putting these features under localizernode sub module
or under localizer main module.

It will be added, in every case.

Many thanks.

hbfkf’s picture

Dear Stephan,

Thanks a lot for your (sub)module. Very nice: Using it, I can link to a "localized" page by using "/localized/node/1234" or, if I need it, directly to a specific translation, using "node/1234" – great!

As to the question whether this should be a submodule or be part of localizer, I vote for inclusion in localizer. It seems to be such "basic need" when you do localization.

Thanks a lot for sharing!
Kaspar

svihel’s picture

Dont work for me :(

Followed your procedure as you described and it doesnt work. It show error404.

I use normal linking <a href="/localized/node/124">asdf</a> is it ok, or I missed something about linking in drupal?

Roberto Gerola’s picture

It should be :
asdf

Note the href : remove the first slash.

napher’s picture

Has this been added to localizer yet?

napher’s picture

Does this work with clean urls off?

Cheers,
Keven

stmh’s picture

I just tested my script with clean urls set to off and it works just fine.

<a href="?q=/localized/node/124">bla</a>

Note the "?q="

cheers,
Stephan