How to use i18n for menu link to "blog" (D6)

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
Mobile Michael's picture

I am running a trilingual blog on a charity website on Drupal6 + i18n module but can't configure the primary link to the blog to show up in the choosen language only.

Internationlisation is working like charme for the blog:
www.artandaid.org/en/blog/1 => blog shows up in English
www.artandaid.org/de/blog/1 => blog shows up in German
www.artandaid.org/blog/1 => blog shows in the users language
Wonderful!

Now I would like to add two links to the primaries

Our Blog => en/blog/1
Unser Blog => de/blog/1

But I keep recieving the error message "The path 'en/blog/1' is either invalid or you do not have access to it."

When I configure the primaries like this

Our Blog => blog/1
Unser Blog => blog/1

then both menu item show up, regardless of the users language. How to filter this links for the respective language? Does anybody know a trick?
Kind regards, Michael

Comments

Try setting a language prefix for English

ar-jan's picture

Probably your site's default language is English, and it has no langauge prefix. Add it at admin/settings/language > edit.
In order to only have the right menu item show I think you should do either one or both of the following:
* At admin/settings/i18n set Content selection mode to 'Only current language and no language' (you probably had already done that)
* Use i18n Multilingual menu and set the right language for the menu items 'Our Blog' and 'Unser Blog'

My solution for the (themed) menu problem

albertmatyi's picture

I've made the modifications you specified, but without success, and I'm starting to believe nonsie

The themed menu still shows all the translations (the block menu is working fine).

After some code digging i realized that i can filter the menus myself from the template page, adding the following code in my case in the garland theme file (themes/garland/page.tpl.php) :

<?php
foreach($primary_links as &$menuitem)
{
    if(
strcmp($menuitem['langcode'], $language->language) != 0)
       
$menuitem = null; // i don't understand why but unset($menuitem) did not work
}
?>

same thing can be applied for the secondary menu.

If you know / come across a better solution please notify me.

thanks!

Matyas

p.s. i'm sorry if i misuse some terms but 1. i'm not a native english speaker 2. i'm new to drupal (2nd day)

I may be misunderstanding

Andy Inman's picture

I may be misunderstanding the problem, but here is a possible solution:

Our Blog => blog/1

only that link, then create a translation (translated string) Our Blog -> Unser Blog

Not sure if this will work, and in any case I'm not 100% clear as to what the problem is.



Currently part of the team at https://lastcallmedia.com in a senior Drupal specialist role.

If you wanna do this hack in

aurels's picture

If you wanna do this hack in a cleaner fashion, consider putting this (extended-from-yours) code in your template.php's phptemplate_preprocess_page(&$vars) function:

foreach($vars['primary_links'] as &$menuitem)
  {
      if(strcmp($menuitem['langcode'], $vars['language']->language) != 0)
         $menuitem = null;
  }

aurels

better hack

abaddon's picture

better hack :-) (checks for language neutral menus as well, for these the language variable is empty..)

<?php
$current_language
= $vars['language']->language ;
foreach ( array(
'primary_links', 'secondary_links') as $menu_name ) {
        foreach (
$vars[$menu_name] as $menu_key => $menu_item ) {
                if (
$menu_item['langcode'] != $current_language && ! empty($menu_item['langcode']) ) {
                        unset(
$vars[$menu_name][$menu_key]) ;
                }
        }
}
?>

should add check for i18n or so.. im not sure what happens if you disable that

Check for null

Dmi167's picture

Don't forget to check for null:

<?php
  $current_language
= $vars['language']->language ;
  foreach ( array(
'primary_links', 'secondary_links') as $menu_name ) {
    if (! empty(
$vars[$menu_name])) {
          foreach (
$vars[$menu_name] as $menu_key => $menu_item ) {
                  if (
$menu_item['langcode'] != $current_language && ! empty($menu_item['langcode']) ) {
                          unset(
$vars[$menu_name][$menu_key]) ;
                  }
          }
    }
  }
?>

For Acquia Marina theme it is not enough. Because this theme using Drupal API function “menu_tree”. So I modified this function straight in the page.tpl.php file.
Changed one string of code:

<?php
 
print menu_tree($menu_name = 'primary-links');
?>

On this one:

<?php
 
// Overloaded "menu_tree" function to support multilingual primary links
 
$menu_name = 'primary-links';
 
  static
$menu_output = array();
 
  if (!isset(
$menu_output[$menu_name])) {
   
$tree = menu_tree_page_data($menu_name);
   
   
// Leave only current language menus and language neutral menus
   
$current_language = $language->language;
    if ( ! empty(
$tree) ) {
      foreach (
$tree as $menu_key => $menu_link ) {
            if (
$menu_link['link']['options']['langcode'] != $current_language &&
                 ! empty(
$menu_link['link']['options']['langcode'] ) ) {
              unset(
$tree[$menu_key] ) ;
            }
      }
    }
   
   
$menu_output[$menu_name] = menu_tree_output($tree);
  }
 
print_r ($menu_output[$menu_name]);
?>

In general this is the same like in previous point, but with knowledge of menu tree structure.

Better approach for Acquia Marina theme.

vipaware's picture

For Acquia Marina theme it's better to use i18nmenu_translated_tree(..) function which is a "i18nmenu's" module rework of menu_tree(..) function that already does the job the very right way.

Replace this string:

<?php
 
print menu_tree($menu_name = 'primary-links');
?>

with
<?php
 
print i18nmenu_translated_tree('primary-links');
?>

Make sure i18nmenu module is turned on.

Thank you!

WildKitten's picture

Thanks a lot for this i18nmenu_translated_tree() function. I've been searching 2 days, and trying to display translated primary-links with submenu and sub-submenu. Some menu items where displayed translated, but some stayed in default language. It was frustrating untill I red you post. And this one worked like a charm!

P.S. I had all settings posted exactly the way that they should be. I even tried Translation Table before this one. And without any results. By the way, I used different theme ;)

checking for the module

abaddon's picture

checking for the module should be something like this, wrap the whole block in the "if"

<?php
if (module_exists('i18nmenu')) {
?>

Better hack in D6.14

karimbolivia's picture

Doesn't work for me. But I made this one and it works fine:

if (module_exists('i18nmenu')) {
   // Get the current language
    $current_language = $language->language;
    // Go inside $primary_links
   foreach ($primary_links as $key => $menu_name ) {
           //check if the menu item has the same language and if it's exists
         if ( $menu_name['langcode'] != $current_language && !empty($menu_name['langcode']) ){
              unset($primary_links[$key]) ;
          }
  }
}

Hope that could help.

But what to do with custom menu

xalexas's picture

This is OK for primary/secondary links, but how to do this with custom menu?

custom menu is not rendered

abaddon's picture

custom menu is not rendered directly in theme, if it is then apply the same logic as above
its usually rendered through a block, which i18n already takes care of (translation of menu block), see handbook

loading a link.tpl.php

brenes's picture

Hello Dzmitry I found your code and its absolutely what I was searching for to use with my menus. I have one question regarding the possibility to load a tpl.php file for the links. In my current footer menu I am calling a file called links-secondary.tpl.php which is formatting the footer links with a delimiter.

<?php print
$footer_links = theme('links_secondary',menu_navigation_links('menu-footernavi'),array('class' => 'links', 'id' => 'footlist'));
?>

Is there a way to merge this with your approach?

Probably too late but I also

totsubo's picture

Probably too late but I also had this same problem. Drupal doesn't make it obvious (or even possible!) to translate menus. I finally found this module which makes it much easier:

http://drupal.org/project/translation_table

Better late than never.

dvb's picture

To translate a custom menu that i created, whose drupal7 id is menu-top-menu, I found that using i18n_menu_navigation_links was the correct thing to do. I struggled with menu_navigation_links to no avail. Thanks to the following issue for shedding light on what to do: http://drupal.org/node/514108.

So in my case the entire function call was

i18n_menu_navigation_links('menu-top-menu', 0);

Hopes this really helps someone.

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: