I've made the following code mod to my instance of translation.module so that the translation block provides the URLs to the translated version of the current URL when the path is created by the taxonomy_menu module. I don't know if this should be included in the main version of this or if it should be included as a separate module (I wouldn't know how to do that) but I've found that this makes it so that I can use the Language Switcher + Translation Block setup proposed in the i18n page with taxonomy_menu.

Regards,

Juan Felipe

Comments

jose reyero’s picture

Status: Needs review » Needs work

Patch looks ok. But can't we use the same 'translation_taxonomy_tids' for both taxonomy and taxonomy_menu ?

juanfe’s picture

I think i tried it, and ran into a problem with that approach because I have a nested taxonomy. You know the code much better than I do, but reconstructing why I ended up doing it this way, here's why.

Say the taxonomy tree looks like this:

tID 1
term a
tID 11
subterm 1
tID 12
subterm 2
tID 13
subterm 3
tID 2
término a
tID 14
sub-término 1
tID 15
sub-término 2
tID 16
sub-término 3
tID 3
termo a
tID 17
sub-termo 1
tID 18
sub-termo 2
tID 19
sub-termo 3

so taxonomy_menu's urls when I'm looking at subterm 2 would be:
?q=en/taxonomy_menu/1/12

and the translated URL in portuguese would be
?q=pt/taxonomy_menu/3/18

This differs from the standard taxonomy/term/12 or taxonomy/term/1+12 URL structure that you'd see without taxonomy_menu. translation_taxonomy_tids assumes that $str_tids will be 12 or 1+12 or 1,12 or that. This means you'd have to change translation_taxonomy_tids to account for the nested structure of the taxonomy_menu URLs.

I was also not able to get it working.

I tried implementing your suggestion by adding

  else if (preg_match('/^([0-9]+\/)*[0-9]+$/', $str_tids)) {
    $separator = '/';
    $tids = explode('/', $str_tids);
  }

in translation_taxonomy_tids() and changing my code to

elseif (preg_match("/^(taxonomy_menu\/)(.*)$/",$url,$matches)) {
if ($str_tids = translation_taxonomy_tids($matches[2], $lang)) {
$url = "taxonomy_menu/$str_tids";
}

in translation_url() but I wasn't able to get the results -- when I look at my term_data table, my trids are '0's whereas when I look in i18n_node my trids are populated (which is why I'm using translation_node_nid() to translate the terms rather than translation_get_term_translations. I think this has to do with how I had to structure my categories to display properly using taxonomy_menu...

What I'll do is set this up in a standalone instance (separate from my current site) and validate this using only taxonomy_menu and i18n module with a simple category, and will let you know.

Regards,

Juan Felipe

pulpzebra’s picture

Version: master » 5.x-1.x-dev

I don't know if the patch provided is for 4.7.x, but for sure it doesn't work for 5.x.

I had the same problem and following the hints from this patch I made the translation module work with taxonomy menu this way:

Modify the function translation_url this way:

function translation_url($url, $lang) {
  global $i18n_langpath;
  // If !url get from original request
  if (!$url) {
    $url = _i18n_get_original_path();
  }
  // If url has lang_prefix, remove it
  i18n_get_lang_prefix($url, true);
  // are we looking at a node?
  if (preg_match("/^(node\/)([0-9]*)$/",$url,$matches)) {
    if ($nid = translation_node_nid($matches[2], $lang)) {
      $url = "node/$nid";
    }
  }
  // or a taxonomy term
  elseif (preg_match("/^(taxonomy\/term\/)([^\/]*)(.*)$/",$url,$matches)) {//or at a taxonomy-listing?
    if ($str_tids = translation_taxonomy_tids($matches[2], $lang)) {
      $url = "taxonomy/term/$str_tids". $matches[3];
    }
  }
 <strong> // or a taxonomy_menu link
  elseif (preg_match("/^(taxonomy_menu\/)(.*)$/",$url,$matches)) {
    $tids = explode ("/", $matches[2]);
    $vid = array_shift($tids);    
    if ($str_tids = translation_taxonomy_menu_tids($tids, $lang)) {
      $url = "taxonomy_menu/$vid/$str_tids";
      }
  }</strong>
  return $url;
}

Where $tids is the array that holds VOCABULARY_ID/TERM1_ID/TERM2_ID. $vid is the ID of Vocabulary that we have to strip off the chain of taxonomy terms.

2) Then, add the translation_taxonomy_menu_tids function

/**
 *Returns an url for the translated taxonomy_menu listing, if exists 
 *
 *  $tids: array of term ids at the tail end of a taxonomy_menu URL
*/ 
function translation_taxonomy_menu_tids($tids, $lang) {

$translated_tids = array();
$separator = "/";
foreach ($tids as $tid)
{
  
  if ($translated_tid = translation_term_tid($tid, $lang)) {
      $translated_tids[] = $translated_tid;
    }
}


if (!empty($translated_tids[0]))
{
  return implode($separator, $translated_tids);
}
else
{
  return implode($separator, $tids);
}
}

This function looks different than the one provided in the patch, since it uses the translation_term_tid rather than translation_node_nid function to translate the chain of terms.

I made it work in a simple environment with VOCABULARY/TERM (no nested taxonomy).

Could someone make further test about this and provide an official patch?

Thank you,

Paolo.

pulpzebra’s picture

Errata Corrige

In the first code chunk above I mentioned to put the changed code between STRONG tags... but...

Anyway the correct code is that:

 // or a taxonomy_menu link
  elseif (preg_match("/^(taxonomy_menu\/)(.*)$/",$url,$matches)) {
    $tids = explode ("/", $matches[2]);
    $vid = array_shift($tids);   
    if ($str_tids = translation_taxonomy_menu_tids($tids, $lang)) {
      $url = "taxonomy_menu/$vid/$str_tids";
      }
  }

Sorry guys...

ñull’s picture

StatusFileSize
new9.29 KB

I just post a back port of this code for version 4.7 here. Please apply it to that version too for others to enjoy. Works correctly with multilevel taxonomies.

For version 4.7 the same code is inserted in function translation_url as proposed by pulpzebra :

// or a taxonomy_menu link
  elseif (preg_match("/^(taxonomy_menu\/)(.*)$/",$url,$matches)) {
    $tids = explode ("/", $matches[2]);
    $vid = array_shift($tids);   
    if ($str_tids = translation_taxonomy_menu_tids($tids, $lang)) {
      $url = "taxonomy_menu/$vid/$str_tids";
      }
  }

The function translation_taxonomy_menu_tids is different however. In 4.7 the function translation_taxonomy_tids doesn't seem to exist yet, so I just used translation_term_get_translations instead:

/**
*  Returns an url for the translated taxonomy_menu listing, if exists 
*
*  $nids: array of node ids at the tail end of a taxonomy_menu URL
*/
function translation_taxonomy_menu_tids($tids, $lang) {

$translated_tids = array();
$separator = "/";
foreach ($tids as $tid)
{
 
  if ($translated_tid = translation_term_get_translations(array('tid' =>$tid))) {
      $translated_tids[] = $translated_tid[$lang]->tid;
    }
}


if (!empty($translated_tids[0]))
{
  return implode($separator, $translated_tids);
}
else
{
  return implode($separator, $tids);
}
}


Sorry, but no time to create a patch now. I just attached the modified module.

gagarine’s picture

In comment http://drupal.org/node/108070#comment-280343 I don't understant the preg_match...

(preg_match("/^(taxonomy_menu\/)(.*)$/",$url,$matches))

Perhaps it's me but why the "^" operator (négation)? Whe search the "taxonomy_menu" chain... no?
I think this is more correct:

(preg_match("/(taxonomy_menu\/)(.*)$/",$url,$matches))

No?

Junesun’s picture

Thank you, guys, you are my salvation! I needed a functioning multilingual menu for my site www.esperanto.info and the other solutions, e. g. translating menu items through string translation, just didn't cut it. I'm now using pulpzebra's solution in a nested taxonomy setting and it works great. Thanks so much! Could somebody add this to the next official release?

Now the only thing that I'd like to see with regard to taxonomies in the menu is that they automatically jump to the article if somebody clicks on a taxonomy menu item/category with only one article in it, while still listing every article if there are several. This would add to usability imho. Unfortunately even as a PHP programmer I'm utterly lost in Drupalese code, but I'm ready to pay $50 to whoever can implement this little extra, ideally as something you can switch on or off somewhere in the admin settings. If you're interested in taking on the task, contact me at yutian.mei@gmail.com .

Judith

pkej’s picture

I don't like to apply a patch without understanding what is happening. It seems to be something Judith likes, and probably I would like it as well, but please explain in different words what this patch is solving. (I haven't started making the translated menus yet, but I do know how the use strings for translation works).

summit’s picture

Subscribing,
greetings,
Martijn

nickysworld.net’s picture

I am too am trying to override the urls that taxonomy_menu produces and this thread was the closest thing I could find I'm sorry for the outdated response.

It changes forum urls such as

drupalSite.com/forum/17

with a 'category' url such as

drupalSite.com/1/2/17

I want to switch 'category' with 'forum' in the path and strip the 1/2/ so that the output matches the original url again.

Thank you for your insight.

Hello?

Aaaaaaaah!

Is anybody still here.

summit’s picture

Hi,
I did exactly this! You can change category to forum on admin/settings/taxonomy_menu for taxonomy_menu.
You can strip the 1/2 with custom_url_rewrite in your settings.php.
Is this enough info to get you going?

greetings,
Martijn

jose reyero’s picture

Status: Needs work » Closed (won't fix)

Only critical bug fixes for 5.x.