0

I'm using Drupal 7 with i18n modules. I have 6 languages on my site. The relative paths are working fine except for: the relative paths in blocks for the languages other than the default language.

On my front page I have several blocks. If I add the relative path in a block on my English (default language) front page (www.example.com) it works. If I add a relative path in a block to my French front page (www.example.com/fr)

<a href="page-in-french">Link in block on the French page</a>

Then my expectation was to get to this page

www.example.com/fr/page-in-french

but instead I get to the page:

www.example.com/page-in-french

which obviously does not exist.

I know that I can manually add in the relative path the language code

  <a href="fr/page-in-french">Link in block on the French page</a>

but since the URL that I'm accessing is www.example.com/fr , then a relative path should not contain the "fr/" since it is already in the URL.

How can I set the relative path to just "page-in-french"?

Thank you

0

There are 2 components in the multilingual Drupal WRT which language to show: Content and Interface. /page-in-french refers to Content, /fr/ refers to the Interface language.

Without some additional "guidance" Drupal will link to exactly the link you tell it to. In D7 translations of nodes are new nodes (with different IDs) so it makes sense there is nothing to show for /page-in-french since there are no nodes in English with that name.

Use multilink to redirect to content in language matching the interface language

If you want to make the Drupal be aware of language context in such a way to redirect to a relevant content translation to match the interface language use Multi-Language Link and Redirect (MultiLink).

It works by enabling it on the text Input filter you're using.

It comes with a special syntax to include links to nodes [NID:text] but you don't have to use it! The redirect will work even for HTML links with classes and IDs. Which means you don't have to edit the links you already have.

If you want to control which text to display in the link you can do this in your English block:

<a class='someclass' href='/page-in-english'>Custom English text</a> 

and this in your Spanish block:

<a class='someclass' href='/page-in-english'>Custom Spanish text</a> 

The href attribute stays the same, only the link text changes.

If you want to display node title as the link text (the title of the relevant language translation) you can rely on Multilink syntax:

[34: $]

where the 34 is the NID of the English node $ represent relevant node title. This will display node 34 on the English side with node 34 title, and on the Spanish side it will display whichever node is the Spanish translation of node 34, and the link text will be the title of the translated node.

Check your Languages Detection and selection settings

Make sure you have URL set as the first enabled option. This will make sure the redirect respect the Interface language you're using.

If you give advantage to the User option, and a user has Spanish set in their profile, they would never be allowed to see English pages, as long as they're logged in they'd always be redirected to Spanish.

5
  • Thank you prkos, the problem is only in the blocks, in the normal nodes, the relative paths are working fine, I can write straight "page-in-french" to link to another page in french from www.example.com/fr, it's just in blocks that it does not work. This is why I'm making mistakes, since I'm used to the normal relative paths in nodes, but in blocks I need to add the language in front of the relative path... – MMT May 19 '18 at 17:02
  • I'd expect links in node text would behave the same as the ones in blocks. Are you using any special modules for that? You can limit Multilink to only work on blocks. Create a special Input filter, copy settings you have on your default one, only also enable Multilink, then use that input filter when editing blocks. – prkos May 19 '18 at 17:42
  • No special modules for blocks. These are normal drupal blocks (translatable and translated) and the standard i18n drupal modules – MMT May 19 '18 at 20:05
  • It doesn't have to be a module related to Blocks, maybe it's your Input filter or something related to translation. Is your Input filter on Blocks different from the one you're using on Nodes? What modules are you using that could affect paths? Anything translation related? How are you checking the link destination on Nodes? In Edit node, or on View node? Are they the same? – prkos May 19 '18 at 23:26
  • The blocks that I'm using are footer blocks, thus visible in all pages. I have recently found that actually the relative paths work fine in the blocks for all non-front page pages, but on the front page itself, the relative paths do not work anymore. Let's say I add a relative path in the footer block for the French translation of that block "sitemap" , then on a particular page www.example.com/fr/1 the link at the bottom in the footer would show properly the link www.example.com/fr/sitemap, but on my frontpage the link would be www.example.com/sitemap, thus loosing the "fr". – MMT Jun 26 '18 at 5:13
0

Given more information in your latest comment it seems like your problem isn't related to Drupal at all, it's just the way you structure your href attribute.

This might be down to literally omitting the starting / in your href attribute. Without the starting / the link is assumed to be relative to the current page!

With the starting / the link is assumed to be relative to the baseurl.

Since you're planning on showing the link on many different pages, you can't use URL that is relative to the current page, you have to use the URL that is relative to the domain name: <a href="/fr/page-in-french">, and not: <a href="fr/page-in-french">.

The way you describe it in your latest comment, that links turn out correctly on all pages except from the front page, is only a coincidence. You obviously don't have any deeper links (/fr/first/second), you only have one level after the lang (/fr/first). On the "one level pages" your link is evaluated to be relative to that page: /fr/your-link.

On a page with deeper path structure your link would evaluate to: /fr/first/your-link. Which doesn't exist and you don't want that ;)

You must use the starting / in your href attributes if you want your link to always be relative to the baseurl, and not to the page you're currently viewing.

When you structure your href to /fr/page-in-french the links will always be correct, no matter which page they're displayed on.

But it also means the link will always link to the French interface and the French node. Even on English pages with English interface.

If you expect to use that same block on all languages, and you expect to display related language links (if on a French page show link to French node with /fr/, if on an English page show link to English node without the lang code), then you need to use the Multilink module, as I described in my previous answer.

Another option is to create different blocks (or Translate the existing block) so different blocks/translations are shown on different languages. This is a good solution if you don't have a lot of links. You'd have to manually writeup all the links correctly for all the languages (all English links in English block, then separately all French links with /fr/ in French block), and check them all. So it's doable if you don't have a lot of links and languages.

For all other cases I'd recommend Multilink module as the only sensible solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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