3

I statically linked an anchor to my /cart page on page.tpl.php, but since it's a multi language website, when I'm on http://localhost/fr it still goes to http://localhost/cart instead of http://localhost/fr/cart

I'd like to know how to print the current base language path or base_url and concatenate it with /cart

thanks.

  • Are you looking for $GLOBALS['language']->language maybe? – Clive Sep 15 '14 at 15:40
  • I don't know maybe, I just want to make a link to /cart page without destroying the $path if i'm on /fr language – Gus Sep 15 '14 at 15:46
  • 1
    Something like $GLOBALS['base_url'] . $GLOBALS['language']->language . '/cart' would do the trick, but I fancy the i18n module has something more sophisticated than that – Clive Sep 15 '14 at 15:50
  • Nah it didnt work :( , Kojo answer almost work, please check my comment on his answer – Gus Sep 15 '14 at 16:00
  • 1
    There isn't - global $base_url is $GLOBALS['base_url'], exactly the same bit of memory – Clive Sep 15 '14 at 16:04
9

If you want to display the cart link in template you better do it with l or url function, example:

in page.tpl.php file:

<ul class="">
  <li><?php print l(t('Cart', 'cart')) ?></li> // first way
  <li><a href="<?php print url('cart') ?>">Cart</a></li> // the second way
</ul>
  • Wow man, thanks, this is much more efficient way to do so – Gus Sep 15 '14 at 16:18
  • very elegant way indeed. can you comment if l() is able to build the url with or without the lang prefix, I mean without for default and with for others ? – Kojo Sep 15 '14 at 16:20
  • @Kojo they handle languages automatically if links are internal, or you can optionally specify it.. – xurshid29 Sep 15 '14 at 16:21
4

This should work in your page.tpl.php

global $language;
global $base_url;
//get the current language
$current_lang = $language->language;
//get the default language
$default_language = language_default();

switch($current_lang) {
  case($default_language):
    $url = $base_url."/cart";
    break;
  default:
    $url = $base_url."/". $current_lang ."/cart";
}

I added the language_default() part as per Clive suggestion. You'll find more information on global $language.

  • Thank you it kinda works, but not perfectly, because I have EN and FR languages enabled, and my default languega (EN) doesn't use trailing /en, if I go on localhost/ and click on the link it will take me to localhost/en, I only want this for FR language to work Also I get this: Notice: Undefined variable: language_url in include() page--front.tpl.php). – Gus Sep 15 '14 at 15:59
  • well, add a switch to that code. may I update the answer ? – Kojo Sep 15 '14 at 16:00
  • sure man, but what's a switch? I'm a total newbie in PHP – Gus Sep 15 '14 at 16:01
  • 1
    We can't teach you PHP here that needs to happen elsewhere - but you can use language_default() to get the default language, then check that against $language->language – Clive Sep 15 '14 at 16:02
  • I know @Clive, I love you man, but you can just simply help a misguided newbie here, it doesn't hurt that much, but I appreciate it anyway – Gus Sep 15 '14 at 16:10
3

If you just need to print "en" or "nl" somewhere, depending on the current language:

<?php
  global $language ;
  $curlang = $language->language;
?>

This is the prefix of the current language: <?php print $curlang; ?>
0

cptstarling is correct. I was getting error's back from SEO reports asking for Language declarations, and simply created a custom HTML.tpl.php theme template that included:

<!DOCTYPE html lang="<?php echo $language->language; ?>">

I did not declare a variable, as this was the only location that I needed the PHP language to be displayed.

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.