Jump to content

javascript Translation


island

Recommended Posts

Hello,

 

I need to translate some fields from a databese using google translation and show those fields in a webpage

 

i did this code :

 

<html>  
<head>    

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("language", "1");
</script>
    </head>
   <body>
<?PHP 

$db_server="localhost";
$db_user_login="root";
$db_user_pass="";
$db_name1="sdkoom";
mysql_connect($db_server,$db_user_login,$db_user_pass);
$table="sdkiki";
	$sql="select Title, Description from $table limit 9";
	$result=mysql_db_query($db_name1, $sql) or die("Erreur SQL : $sql<br>".mysql_error());	
	$j=0;
while($row = mysql_fetch_array($result))
{
    	echo $row['Title']."<br>";

?>
    <script type="text/javascript">



    function <?echo $j;?>initialize() {

      google.language.translate("<?echo $row['Description'];?>", "en", "fr", function(<?echo $j;?>result) {
        if (!<?echo $j;?>result.error) {
          var <?echo $j;?>container = document.getElementById("translation<?echo $j;?>");
          <?echo $j;?>container.innerHTML = <?echo $j;?>result.translation<?echo $j;?>;
        }
      });
    }
    google.setOnLoadCallback(<?echo $j;?>initialize);

    </script>
<?php

			echo     "<div id=\"translation$j\"></div><br><br>";
			$j++;
}
mysql_close();	

?> 
</body>
</html>

 

but it give me errors, i think the problem is in javascript

 

any one can help me ?

 

Thanks in advance

Link to comment
Share on other sites

yes the error is in javascript, the source give that :

 

<html>  
<head>    

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("language", "1");
</script>
    </head>
   <body>
    <script type="text/javascript">



    function 0initialize() {

      google.language.translate("hello", "en", "fr", function(0result) {
        if (!0result.error) {
          var 0container = document.getElementById("translation0");
          0container.innerHTML = 0result.translation0;
        }
      });
    }
    google.setOnLoadCallback(0initialize);

    </script>
<div id="translation0"></div><br>   <script type="text/javascript">



    function 1initialize() {

      google.language.translate("how are you ?", "en", "fr", function(1result) {
        if (!1result.error) {
          var 1container = document.getElementById("translation1");
          1container.innerHTML = 1result.translation1;
        }
      });
    }
    google.setOnLoadCallback(1initialize);

    </script>
<div id="translation1"></div><br>    <script type="text/javascript">



    function 2initialize() {

      google.language.translate("i'm fine", "en", "fr", function(2result) {
        if (!2result.error) {
          var 2container = document.getElementById("translation2");
          2container.innerHTML = 2result.translation2;
        }
      });
    }
    google.setOnLoadCallback(2initialize);

    </script>
<div id="translation2"></div><br>



</body>
</html>

 

I'm not good in javascript, can you find the error pls ?

 

Thank you

Link to comment
Share on other sites

well for 1 i see you have the functions and variables starting with a number. Javascript doesnt like that.

I think not any scripting or programming language allows this.

 

so remove the numbers from the start at the functions and variables and you can place the numbers at the end i think.

 

Next you need a debugger for you browser i dont use ie for debugging since ie has horrible debugging tools for the stuff that i found. and ie 8 will have better tools for this.

 

but anyway try installing either firebug

http://www.getfirebug.com/(recommended)

or the webdeveloper pluging for firefox

https://addons.mozilla.org/nl/firefox/addon/60

 

that will make you able to track the errors quicker

 

also i see its the same function and varables a couple of times why dont you just rewrite your function so it can be called multiple times and just need the function one time .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.