Installing i18n without the block interface

ledelboy - April 24, 2006 - 13:27

I have seen several installations of the wonderful module i18n that do not use the block & flags that come standard with the module, but my search for a recipe on how to put the language choice in a link (and if possible in a primary or secondary link) has only yieleded a couple of references that are too obscure to me. Would it be possible to someone in this community to show me how to move the i18n functions from the block to a link?

i looking for the same solution

pablov2 - May 17, 2006 - 08:17

I want to change the block to use only th flags..

i18n links, flags, etc.

adixon - May 18, 2006 - 21:45

there are two issues here:

1. to remove the flags, just override the theme function:

Here's what i18n defines:

/**
* Produces a language link with the right flag
*/
function theme_i18n_link($text, $target, $lang, $separator=' '){
  $output = '<span class="i18n-link">';
  $attributes = ($lang == i18n_get_lang()) ? array('class' => 'active') : NULL;
  $output .= l(theme('i18n_language_icon', $lang), $target, $attributes, NULL, NULL, FALSE, TRUE);
  $output .= $separator;
  $output .= l($text, $target, $attributes, NULL, NULL, FALSE, TRUE);
  $output .= '</span>';
  return $output;
}

Here's what you put in your template.php file in your theme:

function phptemplate_i18n_link($text, $target, $lang, $separator='&nbsp;'){
  $output = '<span class="i18n-link">';
  $attributes = ($lang == i18n_get_lang()) ? array('class' => 'active') : NULL;
#  $output .= l(theme('i18n_language_icon', $lang), $target, $attributes, NULL, NULL, FALSE, TRUE);
#  $output .= $separator;
  $output .= l($text, $target, $attributes, NULL, NULL, FALSE, TRUE);
  $output .= '</span>';
  return $output;
}

(just comment out two lines)

2. to add language switching links at telecentre.org, I did two things. First I added this function into my template.php file (but call it what you want)

function civicspace_language_links($nid,$language) {
  $q = $_GET['q'];
  $links = array();
  if (function_exists('i18n_supported_languages')) {
    $languages = i18n_supported_languages();
    if (preg_match('#^node/\d+$#',$q)) {
      $translations = translation_node_get_translations($nid);
      foreach ($languages as $lang => $longname) {
          if ($trnode = $translations[$lang]) {
            $links[$lang]= l($longname, $lang.'/node/'.$trnode->nid, NULL, NULL, NULL, FALSE, TRUE);
          }
          else {
            $links[$lang]= $longname;
          }
       }
    }
    else {
      foreach ($languages as $lang => $longname) {
        if ($lang != $language) {
          $links[$lang]= l($longname, $lang.'/'.$q, NULL, NULL, NULL, FALSE, TRUE);
        }
        else $links[$lang] = $longname;
      }
    }
  }
  return $links;
}

and then this in my page.tpl.php

  /* generate language links */
  if (function_exists('civicspace_language_links')) {
    foreach(civicspace_language_links($node->nid,$language) as $language_link) {
      $language_links[] = $language_link;
    }
  }
  /* update logo for language */
  $logo = '/'.path_to_theme().'/logo_'.$language.'.gif';

and then the following piece in the page.tpl.php where I wanted the links to go:

  <!-- START: LANGUAGE SELECTION      ||||||||||||||||||||||||||||||||| -->
  <div id="navigation">
    <h3 class="hide">Language Selection</h3>
      <ul id="global-language" class="global-navigation">
      <?php foreach(civicspace_language_links($node->nid,$language) as $lang => $link): ?>
        <li id="language-link-<?php print ($lang == $language) ? 'self' : $lang; ?>"><span><?php print $link; ?></span></li>
      <?php endforeach; ?>
      </ul>
  </div>
  <!-- END: LANGUAGE SELECTION     |||||||||||||||||||||| -->

Some tips regarding i18n

alin.selicean - October 15, 2006 - 17:05

Hi adixon, hi all drupal users..

I just started using Drupal and CMS in general. Also it's my very first contact with PHP and open source community. So please be gentle on me (this time :) ).

It's my first post here and I have a bunch of questions to ask. Please excuse me if there are other posts that might answer my questions, tried to find my answers, but didn't managed. Maybe it's just me...

Anyway, here I go:
1. I liked adixon's telecentre.org intro page very much and I am struggeling with i18n to make it change current page when user's changing the language. I can't make it work. Here's the stuff: www.boinc.ro/newsite (there might be some broken links as the site is under development). So my question for this would be : how to ? :)
2. I also want that this site to be availble for mobile users (PDAs, smartphones and stuff). I looked for some themes and didn't find anything. So I thought to develop one. But here I stumbled upon another issue. I know how to find out if a user is using a mobile browser, but how to tell Drupal to switch the theme to one that suits the browser in question. Will "include" directives work for this ? Is this a good approach ?
3. @adixon: how did you managed to have the rollover effect for the menu without a JS behind ? Can you share the trick with me ? Thanks...

That would be all by now. If I manage to have that PDA theme, I promise to come back here and share it you all, guys.

Thanks again for yout patience and time. Any help (tips, urls, samples, etc) greatly appreciated.

-- Alin

How could add i18n without blocks in Drupal 5.0?

robdinardo - March 28, 2007 - 19:36

Hello, I hope I am not the only one with this request. I hope that someone could show me how to achieve this in Drupal 5.x ?

I would like to have English / French links in the header of each page without flags.

I was able to use the code by adixon, but it returned errors. I also tried the
$translations = translation_node_get_translations(array('nid' => $nid));
fix, but it seems to not work properly. Sometimes I would get an error and sometimes not.

Thanks in advance.

think positive

Adding language switching links in native language

rfay - March 22, 2008 - 18:59

This snippet does the same thing as the one above for displaying language links without the block, but it uses the code in the translations module, so the languages show up in the native name (español for Spanish, for example) - but ONLY if you have used the native name of the language in the locale module when setting up the languages.

<?php
$query
= drupal_query_string_encode($_GET, array('q'));
print
theme('item_list', translation_get_links($_GET['q'], empty($query) ? NULL : $query));
?>

On phptemplate

JordiTR - May 23, 2006 - 08:51

I only added

<div id="langs">
   <?php print theme("i18n_links", $flags = 0, $names = 1, $delim1 = ' ' , $delim2 = ' | ') ?>
</div>

to my phptemplate theme wherever I need to placed it. If you want flags or names just set one of them or both to 0 or 1 (1 means display, 0 means doesn't display). Obviously I didn't activated the language block.

On http://drupal.org/node/52387 you'll find some patches to make the language tags appear on its proper language (each one) and not translated to the present language. I applied them and works.

getting this to work in 4.7?

njehlen - May 28, 2006 - 23:22

I just tried JordiTR's solution in my 4.7 site - the function doesn't seem to be working (it returns nothing). Is there something different I need to do for 4.7? I tried adixon's solution as well, but got an unexpected $ error for the template addition. Has anyone gotten this to work in 4.7?

Still on 4.6

JordiTR - May 29, 2006 - 08:33

Ops, sorry, I'm still on 4.6.

yes, that was 4.7

adixon - May 29, 2006 - 15:01

and i just put it in myself (after posting the solution) - try looking to see where the syntax error is, it's probably just a small typo. Note that you only need the template addition to remove the flags.

got it

njehlen - May 29, 2006 - 17:35

Thanks adixon! I'm not sure where my typo was, but I started from scratch and now it works great. Much appreciated!

invalid argument for foreach()

njehlen - May 30, 2006 - 02:52

This seems to still be fine for the most part, but I noticed strange errors that comes up only when I'm logged in as an administrator. I get a series of two errors - both exactly the same:

warning: Invalid argument supplied for foreach() in
/usr/www/users/xxx/fff/modules/i18n/translation.module on line 389.
warning: Invalid argument supplied for foreach() in
/usr/www/users/xxx/fff/modules/i18n/translation.module on line 389.

And the errors appear under very specific circumstances: The last page the browser was on has to have had the "view" "edit" and "translations" tabs on it. If i'm coming from a page that is generated by a module (where those tabs don't appear), these errors do not occur. And, just to make it more complicated, the language links show up on every page, regardless of errors, but if those language tabs are on the page, none of the language links are actually links - they're just text.

The error refers to this section of the translate module:

388 function translation_node_get_translations($params, $getall = TRUE) {
389  foreach($params as $field => $value) {
390    $conds[] = "b.$field = '%s'";
391    $values[] = $value;
392  }

Any thoughts on what might cause this error? I'm using 4.7.

version issue ...

adixon - May 30, 2006 - 14:56

I'm guessing you're trying part 2. of my supplied code, which is based on an early version of i18n/4.7. I think that it's my use of
translation_node_get_translations($nid)
which I believe should be
translation_node_get_translations(array('nid' => $nid))
It looks like calling it just with a nid is now deprecated.

Looks like I should update my code as well.

that fixed it

njehlen - May 30, 2006 - 15:50

Yep - that did the trick. Thanks again!

ok - close, but still one problem

njehlen - May 30, 2006 - 23:52

I was sure that worked - but there's one piece that's still giving me trouble. Module and Drupal-generated pages work great, but on Pages, the text for the language links appears, but they're not actually links. Any ideas?

as designed?

adixon - May 31, 2006 - 12:17

The language links should behave like that whenever there's no corresponding translation for a node page. If you actually do have a translation (e.g., if you've got the translation links at the bottom of the page), then there might be something wrong with the code.

module not detecting currently translated nodes?

njehlen - May 31, 2006 - 17:11

That does seem to be the problem - when I click on "translate" for a node, it lists the possible languages and then says "Not Translated" for each - problem is, some of the ones that it says aren't translated have been translated already. So - this seems to be a problem with the module, not your links addition. Any idea why this would happen? I've tried both methods - clicking "create translation" and then entering the translation, and also "select node" and selecting a translated node. Neither results in it detecting a translation next time i visit the node, but when if I use the language block to pick the translated node, it works fine. Maybe I'm missing something about how to create a proper translation?

On phptemplate: where?

Alex Schenkman - May 30, 2006 - 16:15

Hi:

I'd like to try your suggestion but I don't know where I should wirte your code.
I'd like to have the flags on the "primary links". Is that possible?

Thanks in advance!

I mean:

<?php
print theme("i18n_links", $flags = 0, $names = 1, $delim1 = ' ' , $delim2 = ' | ')
?>

[Solved] Found it!

Alex Schenkman - May 31, 2006 - 11:38

I have found the file.
I had to install PHPTemplate and then a PHPTemplate theme...

But, I do get the flags as a column and I'd like them as a row.

working for 4.7?

lisa - May 31, 2006 - 15:57

Did you get that code working for 4.7?

I am running Drupal 4.7 with the cvs version (for 4.7) of i18n. And using the box_grey theme (which is a phpTemplate theme).

I went to the page.tpl.php file in my theme and tried adding the code:

<div id="langs">
  <?php print theme("i18n_links", $flags = 1, $names = 0, $delim1 = ' ' , $delim2 = ' | ') ?>
</div>

But it didn't work - it didn't display anything. Can anyone explain how to get this working in 4.7? I just want to display the flags.

Or how to modify the Translation block so it only displays flags?

Thanks

working on 4.6

Alex Schenkman - June 1, 2006 - 08:11

No, I tried it on 4.6.5

for 4.7.2

Alex Schenkman - June 24, 2006 - 11:11

Hi:

To get it working in 4.7.2 you can add the following to your page.tpl.php:

<?php print theme('item_list', translation_get_links($_GET['q'],0)) ?>

The last 0 is for only flags. Use 1 for flags and names.

it works - thanks :)

lisa - June 27, 2006 - 07:50

thanks Alex.

well, mostly works :)

lisa - June 29, 2006 - 14:29

That snippet will get flags to display in a column (vertically) but how do I get them to display in a row (horizontally)?

Alex, you were trying to get 'flags as a row' earlier - did you figure it out and if so how?

Thanks

Flags on a row

Alex Schenkman - July 11, 2006 - 08:12

Sorry I hadn't check my lasts posts in a while...

Here comes the code for flags in a row in 4.7.2

<ul>
<?php foreach (translation_get_links($_GET['q'],0) as $link): ?>
<li> <?php print $link?> </li>
<?php endforeach; ?>
</ul>

Remove active link in language menu

florianr - October 8, 2006 - 10:35

How could I prevent the active language to get a link?
I use this in page.tpl.php:

<ul id="lmen">
<?php foreach (translation_get_links($_GET['q'],0) as $link): ?>
<li> <?php print $link?> </li>
<?php endforeach; ?>
</ul>

What gives me the language flags, but the active language should be <span class="active">LANGUAGE</span> istead of <a href="xyz" class="active">LANGUAGE</a>

Anyone with an idea? I have the repalce active link function .. how could I get that together?

<?php
function replace_active_link($text) {
 
$pattern = "/<a[^>]* class=\"[^\"]*active[^\"]*\"[^>]*>([^<]*)<\/a>/";
 
$replace = "<span class=\"active\">\${1}</span>";
  return
preg_replace($pattern, $replace, $text);
}
?>

Thanks for your help ...

Problem with regexp

florianr - October 9, 2006 - 11:54

print replace_active_link just removes the link from the Text, not from the image, so the regexpr is the problem, perhaps someone could help, to get a expression, that matches a link with image as well?

<a href="/nl/references" class="active"><img src="/modules/i18n/flags/nl.png"  class="i18n-icon" width="16" height="12" alt="Dutch" /></a>

idea

thepeter - October 12, 2006 - 09:35

see here last comment .. I didn't tryed that but if shouldn't be difficult
I see you wrote that comment before :)

Problem is the regexp

florianr - October 12, 2006 - 22:39

Thanks for your answer,

But my Problem is to find the right regexp, matching the link with image ... For the Links with text the regexpr works, but not for the links with the flag (image) only.

 
 

Drupal is a registered trademark of Dries Buytaert.