0

My site is almost entirely in English, but with individual pages in different languages. There's one extra page per language other than English, as a single page "this is a summary of the sites information in your language" sort of deal.

I've used the Drupal "language" module to set up some languages and give some nodes a language other than English. I don't really want anything more complicated than this. Specifically I don't want:

  • To translate the entire site to any other language.
  • To allow users to pick their language.
  • To translate anything other than node (single page) content. Menus, site slogan, etc, will always be in English.

When viewing the pages for those nodes, I want to set some CSS classes and in some cases the text direction (rtl for Arabic / Urdu / Hebrew) based on the nodes language.

For some reason, the global $language variable is always set to English, regardless of the node language.

I can get the nodes language from $node->language and work out my CSS classes and text direction with the following code in my page.tpl.php:

<?php
    $node_lang = 'en';
    if (isset($node) && isset($node->language)) {
        $node_lang = $node->language;
        $rtl = ($node_lang == 'ar' || $node_lang == 'ur' || $node_lang == 'he');
    }
    $en = ($node_lang == 'en' || $node_lang == 'und');
?>
<div id="content"
    class="primaryContent <?php if($rtl){print "rtl";}?>"
    <?php if ($rtl) { print "dir='rtl'"; } ?>
    <?php if (!$en) { print "lang='$node_lang' xml:lang='$node_lang'"; } ?> >
...content...
</div>

Is there a simpler or "more recommended" way of doing the above? Doing my own detection of rtl / ltr feels fairly brittle.

2

If $node is the variable containing the node object, you can use code similar to the following one:

$languages = language_list();
if (isset($languages[$node->language])) {
  $rtl = $languages[$node->language]->direction == LANGUAGE_RTL;
  // $direction = $languages[$node->language]->direction ? 'rtl' : 'ltr';
}

If you need to use the string "rtl" or "ltr" basing on the direction associated with the language, then you can use $direction, instead of $rtl.

| improve this answer | |

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.