1

In my case, I wanted to print the language switch links directly in page.tpl.php, for example with:

<a href="what_to_put_here">English</a>
<a href="what_to_put_here">Deutsch</a>

How can I accomplish this without using the "Language switcher" block? I checked all the modules that comes with the Internationalization project, but I didn't see any hook_block() implementation that outputs the language switcher block.

UPDATED: I wanted something like: This site is on russian, english and kazakh languages. This cannot be done with language switcher block.

i've tried this way in page.tpl.php:

<?php
$way = drupal_is_front_page() ? '<front>' : $_GET['q'];
print '<span id="switcher">'. 'на ' . l('русском', $way). ', ' . l('казахском', 'kk/'.$way). ' и ' . l('английском', 'en/'.$way) .'</span>';
?>

Default language is russian - mysite.com, when i switch to an english - mysite.com/en, as you see it adds prefix 'en'. But now if i want to switch on kazakh language - mysite.com/kk from english the link looks mysite.com/en/kk == it adds en to my URL. How to make this correctly?

  • I'm curious, does the language switcher has the same behavior ? – tostinni Jul 25 '11 at 17:53
  • Have you tried to put the alter in your code drupal_alter('translation_link', $links, $path); and deal with the $links array ? – tostinni Jul 25 '11 at 17:58
  • problem is theme('links', $links, array()); outputs all links together. And i need to put some text before russian, english and kazakh translation links. – Alexander Kim Jul 26 '11 at 10:19
  • Yes sure but you're not obliged to use theme('links' just use $links the way you want with your own code foreach ($links as $key => $link) {. Check out the theme_links documentation. Does the $links array output something useful for you ? – tostinni Jul 26 '11 at 15:07
  • Thanks, can you throw me an example, while im trying to do it myself :) – Alexander Kim Jul 26 '11 at 16:44
2

What doesn't work for you with the language switcher ?
If you want to reproduce its behavior, have a look at its code and try to do something equal.

Here is some code:

$path = drupal_is_front_page() ? '<front>' : $_GET['q'];
$languages = language_list('enabled');
$links = array();
foreach ($languages[1] as $language) {
  $links[$language->language] = array(
    'href' => $path, 
    'title' => $language->native, 
    'language' => $language, 
    'attributes' => array('class' => 'language-link'),
  );
}
drupal_alter('translation_link', $links, $path);
//dpm($links);

foreach ($links as $link) {
  echo t('Link for ') . l($link['title'], $link['href']) . '<br />';
}
  • check my updated question please. – Alexander Kim Jul 25 '11 at 17:15
  • Thanks, but how it would determine which $link for russian and which for english? for example i want to put some russian text before russian translation link, and some english text before english translation link. – Alexander Kim Jul 27 '11 at 4:21
  • tested your code - it doesn't works correctly - when im on russian language and trying to switch to english -> english link href attribut looks like admin/build/block without en prefix. – Alexander Kim Jul 27 '11 at 6:09
  • admin/build/block is an admin path and I think these don't have special paths. Have you tried on a normal node which have two translations ? Because this code is exactly the language switcher code, I just added an echo at the beginning. – tostinni Jul 27 '11 at 6:23
  • I've already tried this code on a normal node with 3 translations - no effect... – Alexander Kim Jul 27 '11 at 10:22
1

Use the language option of the l() function. Same URL, different language and Drupal should take care of it for you:

<?php
$way = drupal_is_front_page() ? '<front>' : $_GET['q'];
$languages = language_list();
print l('slovak', $way, array('language' => $languages['sk'])) . ', '
  . l('czech', $way, array('language' => $languages['cs'])) . ', '
  . l('english', $way, array('language' => $languages['en']));
?>
  • 1
    it doesn't works, mate. Links is now without any prefixes, just for example - admin/build/block (even if im on kazakh version it doesn't adds en prefix) – Alexander Kim Jul 26 '11 at 10:17
  • Sorry, the language option is supposed to be a language object, not just its code. Tested this time. ;-) – Jan Tomka Jul 27 '11 at 1:10
  • it doesn't works :( Same behaviour as before. If i belong at admin/build/block - the link on kazakh and other versions is still admin/build/block without any prefixes... – Alexander Kim Jul 27 '11 at 4:20
  • Hm... Make sure to configure the "Language negotiation" to "Path prefix *" at the Languages | Configure page. – Jan Tomka Jul 27 '11 at 4:27
  • it's already set to path prefix – Alexander Kim Jul 27 '11 at 5:42

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.