Page Index Toggle Pages: 1 Print
Adding a mouse-over Google Translator to YaBB 2.2+ (Read 3687 times)
OH Eng
Ex Member


Adding a mouse-over Google Translator to YaBB 2.2+
Jun 13th, 2008 at 3:28am
Print Post  
This modification was contributed by BenF and has not been tested by other users (yet).

* * * Be sure you MAKE A BACKUP before modifying your files * * *

Below, are the changes for onMouseOver translation. Note a few limitations:
The Google translate API will only translate strings up to 500 characters in length. For this reason, the source string sent for translation is truncated at 500. With a bit more coding, we could get round this but I haven't the time to do it at present.
The languages offered need to be edited into the javascript code. This is because the YaBB names for languages don't match the Google's language IDs ("English" / "en"). A neater way to handle this would be to have an external file linking language name to language ID.

in Templates/default/default.html:

Find:

Code
Select All
<title>{yabb title}</title>  





Insert below:

Code
Select All
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">

    google.load("language", "1");

    function tr(orig,trans,language) {
	var tobject = document.getElementById(orig);
	tobject.borderLeftWidth = 0;
	setTimeout("do_tr('" + orig + "','" + trans + "','" + language + "')",300);
	tobject.onmouseout = function(){
	  tobject.borderLeftWidth = 1;
	}
    }
    function do_tr(orig,trans,language) {
	var tobject = document.getElementById(orig);
	var text = tobject.innerHTML;
	var targ_lang = "en";
	if (language == "Spanish") targ_lang = "es";
	if (language == "Norsk") targ_lang = "no";
	text = text.substring(0,500)
	if (tobject.style.color == 'gray') {
	  return;
	}
	if (tobject.borderLeftWidth == 1) {
	  return;
	}
	if (tobject.style.lang == targ_lang) return;
	google.language.detect(text, function(result) {
	  tobject.style.lang = result.language
	  if (targ_lang == result.language) return;
	  tobject.style.color = 'gray';

	  if (!result.error && result.language) {

			google.language.translate(text, result.language, targ_lang, function(result) {
				if (!result.error) {
				  var container = document.getElementById(trans);
				  container.innerHTML = "<br>" + result.translation;
				}
		  });
		}

	});
    }

</script>
 







in Sources/MessageIndex.pl


Find:

Code
Select All
$tempbar =~ s/({|<)yabb subjectlink(}|>)/$msublink/g; 






Change to:

           
Code
Select All
# set up vars for translation enabling
		$tras1 = "<span id=\"ts${counter}\" onMouseOver=tr(\"ts${counter}\",\"trs${counter}\",\"$language\")>";
		$tras2 = "</span><span id=\"trs${counter}\"></span>";

		$tempbar =~ s/({|<)yabb subjectlink(}|>)/$tras1$msublink$tras2/g;

 




Find:

     
Code
Select All
$messageindex_template =~ s/({|<)yabb board(}|>)/$boardlink/g;

 





Change to:

     
Code
Select All
# set up vars for translation enabling
	$tras1 = "<span id=\"ts\" onMouseOver=tr(\"ts\",\"trs\",\"$language\")>";
	$tras2 = "</span><span id=\"trs\"></span>";

	$tram1 = "<span id=\"tm\" onMouseOver=tr(\"tm\",\"trm\",\"$language\")>";
	$tram2 = "</span><span id=\"trm\"></span>";

	$messageindex_template =~ s/({|<)yabb board(}|>)/$tras1$boardlink$tras2/g;

 





Find:

                 
Code
Select All
$boarddescription	=~ s/({|<)yabb boarddescription(}|>)/$bdescrip/g;

 




Change to:


                 
Code
Select All
$boarddescription	=~ s/({|<)yabb boarddescription(}|>)/$tram1$bdescrip$tram2/g;

 




in Sources/BoardIndex.pl

Find:

           
Code
Select All
$tmptemplateblock .= $templatecat; 







Insert after:


              
Code
Select All
$counter = 0;  






Find:

                 
Code
Select All
foreach my $boardinfo (@goodboards) {  






Insert after:

Code
Select All
$counter++;  






Find:

                       
Code
Select All
$templateblock =~ s/({|<)yabb boardname(}|>)/$boardname/g;
				$templateblock =~ s/({|<)yabb boarddesc(}|>)/$bddescr/g;  






Change to:

                       
Code
Select All
# set up vars for translation enabling
				$tras1 = "<span id=\"ts${counter}\" onMouseOver=tr(\"ts${counter}\",\"trs${counter}\",\"$language\")>";
				$tras2 = "</span><span id=\"trs${counter}\"></span>";

				$tram1 = "<span id=\"tm${counter}\" onMouseOver=tr(\"tm${counter}\",\"trm${counter}\",\"$language\")>";
				$tram2 = "</span><span id=\"trm${counter}\"></span>";

				$templateblock =~ s/({|<)yabb boardname(}|>)/$tras1$boardname$tras2/g;
				$templateblock =~ s/({|<)yabb boarddesc(}|>)/$tram1$bddescr$tram2/g; 







in Sources/Display.pl

Find:

Code
Select All
$outblock =~ s/({|<)yabb subject(}|>)/$msub/g;  






Change to:


           
Code
Select All
# set up vars for translation enabling
		$tras1 = "<span id=\"ts${counter}\" onMouseOver=tr(\"ts${counter}\",\"trs${counter}\",\"$language\")>";
		$tras2 = "</span><span id=\"trs${counter}\"></span>";

		$tram1 = "<span id=\"tm${counter}\" onMouseOver=tr(\"tm${counter}\",\"trm${counter}\",\"$language\")>";
		$tram2 = "</span><span id=\"trm${counter}\"></span>";


		$outblock =~ s/({|<)yabb subject(}|>)/$tras1$msub$tras2/g;
 





Find:

Code
Select All
$outblock =~ s/({|<)yabb message(}|>)/$message/g;  






Change to:

Code
Select All
$outblock =~ s/({|<)yabb message(}|>)/$tram1$message$tram2/g;  





                                                                                                                      by BenF                                                                                                
« Last Edit: Jun 13th, 2008 at 4:02pm by »  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Print
 
  « Board Index ‹ Board  ^Top