Switching HTML based on active i18n language

This snippet works for multi-lingual sites that use the Internationalization module (aka i18n) . Depending on the current active language setting this snippet will render different HTML. E.g. if you want a different image, javascript or unique layout for French than for English.

<?php 
   
global $i18n_langpath;
    if (
$i18n_langpath == fr) {
     print
"HTML français ici";
     } else {
     print
"English HTML here";
     }
?>

For other languages use their appropriate two letter language code, e.g. "es" for Spanish, "de" for German.

You can add more 'if else ()' for sites with more than two languages.

Note:

This should be used as a last resort. Best practice is to use i18n native menu translation function and t() function to translate any text, e.g.
<?php print t("Sentance to be translated here."); ?>

Hope you find this helpful!

:-)

Thank you very much This is

hedac - October 2, 2006 - 17:31

Thank you very much
This is very useful sometimes :)

I've been able to do an

hedac - October 6, 2006 - 06:41

I've been able to do an interesting thing with this. I have a CCK content type with many fields .. but ONLY ONE (the description field) needs translattion. So I don't want to use the standard way of translating those nodes by using the translate tab and generating another node with different language for only changing one field!.. So what I did is creating both description fields (only two languages in my case) in the CCK content type... field_description in english and field_descripcion in spanish. So when creating this kind of content you have to fill boths descriptions in the same node... But only want to display the current language one. So in the contemplate template... I put this:

<?php
   
global $i18n_langpath;
    if (
$i18n_langpath == es) $mytranslatedfield=$field_descripcion;
    else
$mytranslatedfield=$field_description;
?>

<div class="clearb field field-type-text field-field-description">
  <h3 class="field-label"><?php print t("Description"); ?></h3>
  <div class="field-items">
<?php foreach ((array)$mytranslatedfield as $item) { ?>
<div class="field-item"><?php print $item['view'] ?></div>
<?php } ?>
  </div>
</div>

note also the t("description") to translate the label of the field. in the standard way.

it works perfectly. Veeery nice... This saves database space too.

multilingual cck fields

kdebaas - January 9, 2007 - 12:51

Your approach is an interesting alternative to treating translations as separate nodes. Could you tell us a bit more about your workflow?

  • Do you assign a language to your multilingual cck type, or does it get language=none?
  • How do you display nodes of which a description / translation is still missing? Will it revert to the original language?
  • Do you mix this approach (of translating on the level of fields within a node) with translations as separate nodes on your site? And if so, how does that affect your workflow (i.e. what issues are there to watch out for?

Hello No. I don't assign

hedac - January 16, 2007 - 10:41

Hello

  • No. I don't assign language to the node... it is not needed to set the content type to alow to be translated either.
  • it would be easy to make another check... to see if... (in that example) $field_descripcion has a value.. or is a empty string... so depending on that, you can assign the one with content if the displayed language one has not been filled.
  • I don't mix both ways.... but I think it should not be a problem.

Gracias!

kdebaas - January 17, 2007 - 10:08

I have started trying out this approach, and here is what I wrote to make the field revert to my standard language if no translation exists (in this case I am working with a custom teaser field that I created:

<?php
   
global $i18n_langpath;
 
    if (
$i18n_langpath == en && ($field_teaser_english[0]['value'])) {
     
$mytranslatedteaser=$field_teaser_english;
    } else {
     
$mytranslatedteaser=$field_teaser_nederlands;
    }
?>


<div class="field field-type-text field-field-teaser-nederlands">
  <div class="field-items">
    <?php foreach ((array)$mytranslatedteaser as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>

It took me, a know-nothing about php, a while to get to ($field_teaser_english[0]['value']), instead of ($field_teaser_english), and perhaps there is a better, even more efficient way of testing for a value in the array. If so, it would be a good thing to post and correct!

dont work with Drupal 5

koffer - May 3, 2008 - 21:45

any idea about working with drupal 5?

isnt true

koffer - May 3, 2008 - 23:20

it is my mistake, the code work, the only problems is if you need to put some html code like:

<a href=\"http://www.mayteprida.com/?q=en/node/60\" target=\"_self\"><img src=\"http://www.mayteprida.com/themes/english/images/superior.jpg\" alt=\"superior\" width=\"209\" height=\"134\" id=\"Image1\" onmouseover=\"MM_swapImage('Image1','','http://www.mayteprida.com/themes/english/images/superiorover.jpg',1)\" onmouseout="MM_swapImgRestore()" border=\"0" /></a>

you need to put a \ after any " a simple mistake if you never put html code in php

you can see more about this in this tutorial

http://www.kirupa.com/web/phphtml.htm

 
 

Drupal is a registered trademark of Dries Buytaert.