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
Thank you very much
This is very useful sometimes :)
I've been able to do an
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:
<?phpglobal $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
Your approach is an interesting alternative to treating translations as separate nodes. Could you tell us a bit more about your workflow?
Hello No. I don't assign
Hello
Gracias!
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
any idea about working with drupal 5?
isnt true
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