1

I'm translating the site with my custom theme to another language. The problem is in menu links. On the original language they are fine, but with another one they are looks like "node/id". I've tried drupal_lookup_path with and without language, drupal_get_normal_path, drupal_get_path_alias but links are still the same.

I'm feeling like i've missed something in preference, but have no idea how to make links looks properly.

some code from my draw_menu function:

$items = get_menu_weight($menuName, $language->language);
foreach($items as $item) {
   $alias = drupal_lookup_path('alias',$item->link_path,$item->language);

...

    if ($item->link_path == "http://"){
    $drawItems .= '<li><i>'.$item->link_title.'</i>';
   }else{
    if($alias == "/"){
        $drawItems .= '<li><a href="http://'.$_SERVER['HTTP_HOST'].'/" title="'.$item->link_title.'">'.$item->link_title.'</a>';
    }else{
        $drawItems .= '<li><a href="http://'.$_SERVER['HTTP_HOST'].'/'.$alias.'" title="'.$item->link_title.'">'.$item->link_title.'</a>';
    }
   }

...

}

Thank you for your advises.

2 Answers 2

0

I don't know what get_menu_weight() returns (it is not in the standard Drupal API, is it?), so the following is the answer for a general case in Drupal 7.

In short, as long as the menu item is linked to a node, and if the node has at least one translation, the following code should work to get a path-alias of the node for all the translatable languages:

$hstpath = translation_path_get_translations('node/123');
array_walk($hstpath, function(&$v, $k){$v = drupal_get_path_alias($v,$k);});

gives an associated aray like:

Array(
  [en] => info/basic.html
  [fr] => info/basic.html
)

where both English (en) and French (fr) versions have the same path-alias of info/basic.html.

How to convert this path-string to a link to the node to view in the desired language depends on both your language-detection configuration and with which language environment you would like to display the node.

For example, if the URL with the path-alias is given the highest priority, then add the prefix of /fr/ to the French node (as long as French is not configured to be the default language with no path-prefix defined). In addition, you may need to add base_path() at the head, if Drupal is not installed in the top directory of the website.

For a reference, a candidate answer to "Menu links not being translated" explains some basic for the Drupal menu with i18n.

1
  • Thank you, your example helped me a lot. Have no idea why my solution doesnt work, but i've changed it a bit according to your string array_walk($hstpath, function(&$v, $k){$v = drupal_get_path_alias($v,$k);}); end everything is fine now.
    – user28740
    Commented Oct 28, 2014 at 16:19
0

The menu links are being shown without the URL alias because you are displaying them in a language which is not the language of the content. The alias is per-language, therefore it falls back to node/123.

One workaround would be to set the node as "language neutral" (the option must be enabled in your content-type settings). Then you can use a menu block or other Drupal methods to display the menu items without extra code.

Looking around, the only solution I found in order to have language-neutral aliases for language-specific content is this blog post.

If I understand correctly, the author proposes the following snippet in a custom module:

function mymodule_path_update($path) {
  db_update('url_alias')->fields(array('alias' => $path['alias'], 'language' => LANGUAGE_NONE))->condition('pid', $path['pid'])->execute();
}

This automatically sets the alias to language-neutral, but does so for all aliases. So if you have some aliases that do have translations, it's not optimal (for example, on a French+English website, the /contact alias can point to different nodes). I would modify that snippet to make it check if there are any translations already existing for the given node, if not, set as language-neutral. Also needs to check when creating a new translation, to remove the language-neutral alias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.