Download & Extend

Add support for a dropdown instead of flags

Project:Google Translate Links
Version:6.x-2.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Wicher Minnaard
Status:needs work

Issue Summary

I think this module would be much more useful if it provided a dropdown instead of flags.

Two main reasons:
1) Flags are highly problematic in cases where a flag is attributed to language used in many other countries as well.
2) Dropdown would allow many more languages - indeed, all languages that Google offers at any given time.

All you need to do is adapt http://translate.google.com/translate_tools or use the relevant Google API and detect the current (source) page language (either using Drupal or indeed Google can recognize the page language as well).

Comments

#1

I agree with the drop-down concept, since the color of the flags are disruptive for some sites.
But we should leave the option to the user to choose between the two options.

#2

Status:active» needs review

I agree, flag display should remain as an option for users.

Btw, in the meantime I installed the above Google code on one of my sites. The only thing I needed to do was detecting the current node's language and setting is as the source language in the Google code.

$result = '';

// Detect language of the current node.
$nid = arg(1);
$lang_code = 'en';
if (is_numeric($nid)){
  $node = node_load($nid);
  $lang_code = $node->language;
}

// Google translation dropdown from http://translate.google.com/translate_tools
$result .= "<div id=\"google_translate_element\"></div><script>"."\n";
$result .= "function googleTranslateElementInit() {"."\n";
$result .= "  new google.translate.TranslateElement({"."\n";
// Here we enter the current language code
$result .= "    pageLanguage: '".$lang_code."'"."\n";
$result .= "  }, 'google_translate_element');"."\n";
$result .= "}"."\n";
$result .= "</script><script src=\"http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit\"></script>"."\n";

echo $result;

The above code can be used as is, or integrated into this module.

Example - here is a jest text in Slovak which you can translate into any language using the dropdown in the top-right corner :-)

#3

Priority:critical» normal
Assigned to:Anonymous» Wicher Minnaard
Status:needs review» needs work

Thanks!
Not all pages are nodes, though. And since Google can autodetect languages, could you try it without setting pageLanguage?
This could also be a simple solution for people who worry about language/country mismatches.

#4

How can I integrate this code into my site? should I insert it in the theme's page.tpl.php or somewhere else?
any hints is much appreciated. I hope it will be committed in the code, I am ready to QA it.

Check this site: http://labnol.blogspot.com/2005/11/add-language-translation-to-website.html
It gives another example and at the top of that page there is a very nice drop-down with a globe.

#5

How can I integrate this code into my site?

To test vacilando's code, make a new block of the 'php' input type. Paste the code inside the

<?php

?>
tags.
It doesn't work over here, though. I get a 'Error: The server could not complete your request. Try again later.' from Google although it works fine if I just enter the URL into GT directly.
I'm especially interested in leaving out the source language as vacilando's code for extracting the source language relies on the current page being a node, which is not always the case. So, if vacilando's code works for you, please also try this one
<?php
$content
='';

$content.='<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: \'auto\'}, \'google_translate_element\');
}
</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>'
;

echo
$content;
?>

or maybe even this one:

<?php
$content
='';

$content.='<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement(null, \'google_translate_element\');
}
</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>'
;

echo
$content;
?>

Would like to test for myself but Google doesn't let me.

#6

I did create a Block of PHP type, however don't know exactly where to paste the code. Should it be in the Block Body or visibility settings where it says: Show if the following PHP code returns TRUE?

Either one didn't work for me.

#7

Body.

#8

@Wicher #5: I've tested 'auto' where the language code goes, and it seems to work fine for me. Even on a page with mixed EN and SK sentences, Google managed to translated both into the target language I selected.

Since I am sure, however, that this capacity of Google to auto-detect language has limitations (think of identical words in different languages!), I suggest setting page language if it's known (in nodes), and setting it to 'auto' otherwise. Hence my updated code - now running at my site.

<?php
  $result
= '';

 
// Detect language of the current node.
 
$nid = arg(1);
 
$lang_code = 'auto';
  if (
is_numeric($nid)){
   
$node = node_load($nid);
   
$lang_code = $node->language;
  }

 
// Google translation dropdown
 
$result .= '<td valign=top>';
 
$result .= '<div style="padding-top:0px;">';
 
$result .= "<div id=\"google_translate_element\"></div><script>"."\n";
 
$result .= "function googleTranslateElementInit() {"."\n";
 
$result .= "  new google.translate.TranslateElement({"."\n";
 
// Suggest the current language, or set to have it auto-detected.
 
$result .= "    pageLanguage: '".$lang_code."'"."\n";
 
$result .= "  }, 'google_translate_element');"."\n";
 
$result .= "}"."\n";
 
$result .= "</script><script src=\"http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit\"></script>"."\n";

  echo
$result;
?>

What do you think?

I wonder what do you mean 'Google does not let' you to run this on your page???

#9

Well, $nid = arg(1); is not a robust way of determining a nid. Read this thread, it has some good stuff and alternative solutions in it.
But apart from that, Google is not translating just the node. It's translating the whole page, which may have blocks and menu items or $random_stuff which may or may not be in the node's language. Am I correct in this? If so, it's better to let Google work its language detection magic on all of those parts. Good to hear that 'auto' is working fine - the dropdown is becoming a solid alternative.

I wonder what do you mean 'Google does not let' you to run this on your page???

I was referring to the 'Error: The server could not complete your request. Try again later.' Google was showing me. I was not referring to 'letting' in the technical sense, sorry for the confusion ;-)

#10

This auto-detection is not working for my site. It's giving the following error:

We are not yet able to translate from Armenian into French.

check www.keghart.com

and the above code did not work for me at all. It kept displaying the code instead. I put it in the body of a new PHP block.

#11

@Vako, there might be something wrong with the default language setting on that website -- Google thinks it is in Armenian even if most of the text on the page is English. Also straight translationlinks give that error, e.g. http://translate.google.com/translate?u=http%3A%2F%2Fwww.keghart.com%2F&...

@Wicher Minaard yes you may want to validate that arg(1) is indeed the node ID etc, good idea.
Just came across another link you may want to use -- see this simple language detection code: http://code.google.com/apis/ajax/playground/?exp=language#language_detect You could use it to recognize the language of the content area (whether it's node or not).
Do you see a new version of Google Translate Links coming up with this functionality? Or do you think it's for another module? Let me know if I can help with something.

#12

I'd rather let Googles' magic algorithms do the language detection. I'm seeking to stabilize the feature base, waiting for the concrete to settle so I'm reluctant to add complexity such as language detection. However, since there's going to be an admin GUI anyway if users get to choose between flags and a dropdown, there might as well be a setting in that GUI for the source language ("leave empty to let Google determine the source language").
As for the using-Google-to-determine-the-source-language-to-set-the-source-language-in-the-request-to-Google approach, I bet Google already uses these (their own!) algorithms if you set the source language to 'auto' and such seems to works quite well, have a look at http://www.usbeatit.nl/ - there's both Dutch and English content sections on the frontpage and both get neatly translated into whatever language it is you picked from the flag block on the right. You've mentioned this property too (mixed EN and SK sentences).

I'm planning to put this into a 3.x together with what comes out of #592052: User interface design.

>Let me know if I can help with something.
You already have, much appreciated ;-)
I'll put this in CVS soon(ish).
#592052: User interface design is seeking input and the sooner we figure that one out the sooner there's a GUI to add the dropdown-option to.

#13

The message
"Error: The server could not complete your request. Try again later."
happens if you're using Firefox with the Flashblock add-on.
Try another browser, or see http://code.google.com/p/bubble-translate/issues/detail?id=49#c5

#14

Thanks for the helpful comment, Martin.

nobody click here