Passing Langage Values to Drupal Themes

Submitted by mgifford on

I solved this problem a while ago, but didn't have the docs handy or in my blog, so decided to post this quickly here. I needed to pass along the current language along to a variable in my template, but had to define it first as phpTemplates doesn't define it by default. It's a pretty simple process if you have a handle on PHP. Go into your themes folder and look for a template.php file and the page-tpl.php file.

Create a template.php file if it doesn't already exist. It will need something like this in it in order to pass along the new variables:

If this file already exists and there is a call to the _phptemplate_variables function all you'll need to do is insert $vars['lang'] = $i18n_langpath; before the function is returned.

Then add the following in your page.tpl.php file:

<code><code> <!--?php
    print $footer_message . ' <a href="/' . $lang . '/user"-->' . t('User Login') . '&nbsp;&nbsp;<a href="/' . $lang . 
    '/sitemap">' . t('Site Map') . '</a>&nbsp;&nbsp;<a href="/' 
    . $lang . '/search/node">' . t('Search') . '</a>'; ?&gt; 

This will produce links to the User Login, Site Map & Search that are all language specific.

You can insert other useful variables for use in your templates by playing with this concept. Here's a list of variables available to your page-tpl.php by default.

  • Computing
  • Software engineering
  • Computer programming
  • Web application frameworks
  • Template engines
  • PHPTemplate
  • Drupal
  • Subroutine
  • Dollar/Peso sign
  • php
  • php

Section: 

Comments

Just small suggestion

Just small suggestion (improvement?)
In case that i18n module is (for some reason) disabled, your links will change from

.../lang/link...

into double slash

...//link...

instead of

.../link...

So, IMHO it could be better to change (in template.php)

$vars['lang'] = $i18n_langpath . '/';

and (in page.tpl.php)

print $footer_message . ' <a href="/' . $lang . 'user">' ...

Thanks!

Appreciate the contribution. And agreed that this would be an enhancement too.

Drupal 6?

Is this valid for Drupal 6? I'm running Drupal 6.3 with i18n enabled. The Drupal documentation says to add an array to your settings.php file: http://drupal.org/node/67824. This didn't work to language dependant logos for me though.

I tried you method above but it didn't work for me. Is this valid for Drupal 6? If it is would you mind posted the scenario for switching logos so I can try this out. I'm finding it hard to located documentation on this so I'm sure it would be useful for others as well.

Thanks!!

Icon alternatives

Hi Patricia,

I haven't had to stumble across this yet, but you should be able to just do:

 <img src="/files/icon-<?php print i18n_get_lang(); ?>.jpg">

and hard code the image into the theme could you not?

That is generally how I'd deal with language dependent logos.

Multilingual Logos

I'm not sure what you mean by hard-coding the logos. I have the logo option in the template but am only able to load one, not an additional logo per language. My other site-wide variables, such as the site name, are however working. I simply added the variable to my setting file and uploaded each language dependent component through my administrative panel. I thought that the theme settings should work in the same way.

Forgot to change the input format

Now that you can see my example in my previous comment I hope it makes more sense. I think it might be possible to go through the settings file, but that's not how we've done it historically. Partly this is because we've been applying solutions like this since 4.6 and know that they work.

Works For Drupal 6

Thanks, this works for Drupal 6 as well. But how do you create the links from the logos to your front pages?

The Drupal documentation says to use multilingual variables in the settings file but there must but steps missing.

Not as pretty as uploading it to the theme

But it is an extension of what I provided above:

<a href="/<?php print i18n_get_lang(); ?>"><img src="/files/icon-<?php print i18n_get_lang(); ?>.jpg"></a>

You can put more logic into this if you actually need to switch domains.

<?php
$current_lang = i18n_get_lang();
if ($language == 'fr') {
  $switch_domain = 'http://english.ca';
  $change_path = str_replace('/fr/', '/en/', request_uri());
} else {
  $switch_domain = 'http://french.ca';
  $change_path = str_replace('/en/', '/fr/', request_uri());
}
?>
<a href="/<?php print $switch_domain . $change_path; ?>"><img src="/files/icon-<?php print i18n_get_lang(); ?>.jpg"></a>

Works with a 301 Redirect

The first snippet worked for my French and Spanish pages but because the default language is English, the front page just goes to mysite.com, not mysite.com/en and therefore the link was giving me a page not found error. I just added a 301 redirect to the .htaccess file and now everything works perfectly.

I tried the second option but just couldn't wrap my brain around how to add the third language, especially since the French and the Spanish have the same domain.

Anyway, thanks very much for walking me through this. I've linked this back to a Drupal thread for others to benefit from. I really think there should be a page in the internationalization documentation dealing with language dependent theme settings. And I wish I had the chops to write one!

Much appreciated,
Patricia

The more languages there are the trickier it is

I"m used to generally just jumping between two. Sounds also like this was a unilingual site that is being expanded to be a trilingual site.

I haven't actually tested this or anything, but this might work for you a bit better:

<?php
$current_lang = i18n_get_lang();
$lang_array = array('fr'=>'français', 'en'=>'English', 'es'=>'español');
$current_url = $_GET['q'];
foreach ($lang_array AS $lang => $lang_name) {
  if ($lang != $current_lang) {
    $trans_node[$lang] = translation_url($current_url, $switch_lang);
    $trans_path[$lang] = str_replace(array('en/', 'fr/'), '', drupal_get_path_alias($trans_node));
    echo "<a id=\"link2\" href=\"$trans_path[$lang]\">$lang_name</a>";
  } else {
    echo $lang_name;
  }
}
?>

Mind you, there's always the Translations for Content block that is available as part of the i18n suite of modules. It requires some more styling via css to get it to fit in, but it works quite nicely.

Add new comment