is it problem when retrivinng data from database.

As long as you don't put html code inside the display function it should work fine.

As long as you don't put html code inside the display function it should work fine.

thanks for ur greate support.

so it takes lots of tim to change every page. is there any another choice.

google has a translator that works with your browser so I would give them a try to see if they have a developers api to use it.

Try this.

<?
if (!isset($_GET['language']) || empty($_GET['language'])) {
$_GET['language']='en';
}
/**
 * Translating language with Google API
 * @author gabe@fijiwebdesign.com
 * @version $id$
 * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
 * 
 * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding
 * 
 */
class Google_Translate_API {

	/**
	 * Translate a piece of text with the Google Translate API
	 * @return String
	 * @param $text String
	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
	 * @param $to String[optional] Language to translate $text to
	 */
	function translate($text, $from = '', $to = 'en') {
		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
		$response = file_get_contents(
			$url,
			null,
			stream_context_create(
				array(
					'http'=>array(
					'method'=>"GET",
					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
					)
				)
			)
		);
		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
			return self::_unescapeUTF8EscapeSeq($matches[1]);
		}
		return false;
	}
	
	/**
	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
	 * @return UTF-8 String
	 * @param $str String
	 */
	function _unescapeUTF8EscapeSeq($str) {
		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
	}
}


$input= "http://localhost/addmultiplelanguajes-2009-08-15/k1.php";
function display($input) {
$trans_text = Google_Translate_API::translate($input, '', $_GET['language']);
    if ($trans_text !== false) {
        echo $trans_text;
    }
}




echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml">'."\n";
echo '<head>'."\n";
echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />'."\n";
echo '<title>'; display('Untitled Document'); echo '</title>'."\n";
echo '</head>'."\n";
echo "\n";
echo '<body>'."\n";
echo '<form name="language" action="'.$_SERVER['PHP_SELF'].'" method="GET">'."\n";
echo '	<select onchange = "document.language.submit()"  name="language">'."\n";
echo '    	<option selected="selected">'; display('select-language'); echo '</option>'."\n";
echo '        <option value="en">'; display('English'); echo '</option>'."\n";
echo '        <option value="es">'; display('Spanish'); echo '</option>'."\n";
echo '        <option value="fr">'; display('French'); echo '</option>'."\n";
echo '     </select><br>'."\n";
echo "\n";
echo '<p>'; display('Larry Brandt grew up in a small rural town in Missouri where hunting, fishing and outdoor life was an everyday experience.He was fortunate to live close to the Swan Lake National Wildlife Refuge.The Refuge is over 10,000 acres and provides nesting, resting and feeding areas for Waterfowl, primarily ducks.

The migrating populations of ducks and geese at Swan Lake number in the thousands.These waterfowl provided the artist great opportunities to study and learn about their species.

Although the artist has been woodcarving only a few years, he has been painting for over fifty years.'); echo '</p>'."\n";
echo '</form>'."\n";
echo '</body>'."\n";
echo '</html>'."\n";
?>

how to write echo for javascript with in the php code? for suppose in above example may have javascript so how to write echo for that?

how to write echo for javascript with in the php code? for suppose in above example may have javascript so how to write echo for that?

Could you give an example. From my understanding it would be as follows.

<?php
echo '<script>'."\n";
echo 'document.write("';
display('some text');
echo '");'."\n";
echo '</script>';
?>

Could you give an example. From my understanding it would be as follows.

<?php
echo '<script>'."\n";
echo 'document.write("';
display('some text');
echo '");'."\n";
echo '</script>';
?>

haaaa.............haaaaa....it is very complecated procedure. it is not easy to do. very carefully do this.
there is no other option for this echo ing each and every html tag.
otherwise take in php function in class and using this class call this funtion where ever we want that function.
is it possible?
why b'coz i have to do convert more html pages.

haaaa.............haaaaa....it is very complecated procedure. it is not easy to do. very carefully do this.
there is no other option for this echo ing each and every html tag.
otherwise take in php function in class and using this class call this funtion where ever we want that function.
is it possible?
why b'coz i have to do convert more html pages.

It might be possible to use regex to convert all the data in the one hit but could be hard to make. Could you post an example file which includes these complex javascripts.

As long as you don't put html code inside the display function it should work fine.

if you dont mind see my code. i am trying like this. but it is not working.
is it possible to do like this.

<?
if (!isset($_GET['language']) || empty($_GET['language'])) {
$_GET['language']='en';
}
/**
 * Translating language with Google API
 * @author gabe@fijiwebdesign.com
 * @version $id$
 * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
 * 
 * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding
 * 
 */
class Google_Translate_API {

	/**
	 * Translate a piece of text with the Google Translate API
	 * @return String
	 * @param $text String
	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
	 * @param $to String[optional] Language to translate $text to
	 */
	function translate($text, $from = '', $to = 'en') {
		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
		$response = file_get_contents(
			$url,
			null,
			stream_context_create(
				array(
					'http'=>array(
					'method'=>"GET",
					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
					)
				)
			)
		);
		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
			return self::_unescapeUTF8EscapeSeq($matches[1]);
		}
		return false;
	}
	
	/**
	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
	 * @return UTF-8 String
	 * @param $str String
	 */
	function _unescapeUTF8EscapeSeq($str) {
		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
	}
}


class lang {
function display($input) {
$trans_text = Google_Translate_API::translate($input, '', $_GET['language']);
    if ($trans_text !== false) {
        echo $trans_text;
    }}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
 <form name="language" action="'.$_SERVER['PHP_SELF'].'" method="GET">
	<select onchange = "document.language.submit()"  name="language">
  	<option selected="selected"> select-language</option>
       <option value="en"> English</option>
     <option value="es"> Spanish</option>
       <option value="fr"> French</option>
    </select>
	<p class="normal_text"><? $lang->display('A woman`s body performing to its utmost capability- that`s what pregnancy is. From conception to delivery, the female form is tried and tested to carry and bring forth new life. The natural action can be quite exhausting! If incorporating a pregnancy massage, the benefits to the expecting mother are incredible.');?></p></form>
</body>
</html>

Would this do the trick?

<?php
    if (!isset($_GET['language']) || empty($_GET['language'])) {
    $_GET['language']='en';
    }
    class Google_Translate_API {
 
	/**
	 * Translate a piece of text with the Google Translate API
	 * @return String
	 * @param $text String
	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
	 * @param $to String[optional] Language to translate $text to
	 */
	function translate($text, $from = '', $to = 'en') {
		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
		$response = file_get_contents(
			$url,
			null,
			stream_context_create(
				array(
					'http'=>array(
					'method'=>"GET",
					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
					)
				)
			)
		);
		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
			return self::_unescapeUTF8EscapeSeq($matches[1]);
		}
		return false;
	}
 
	/**
	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
	 * @return UTF-8 String
	 * @param $str String
	 */
	function _unescapeUTF8EscapeSeq($str) {
		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
	}
}
 
 
function display($input) {
$trans_text = Google_Translate_API::translate($input, '', $_GET['language']);
    if ($trans_text !== false) {
        return $trans_text;
    }}
function displayhtml($input) {
    preg_match_all('#<[^>]+>#',$input,$htmlout);
    $input=preg_split('#<[^>]+>#',$input);
    $output='';
    $i=0;
    foreach ($input AS $val) {
        $output.=str_replace(array('\r','\n'),array("\r","\n"),display($val)).$htmlout[0][$i];
        $i++;
        }
    return $output;
    }
echo displayhtml('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
 
<body>
 <form name="language" action="'.$_SERVER['PHP_SELF'].'" method="GET">
	<select onchange = "document.language.submit()"  name="language">
  	<option selected="selected"> select-language</option>
       <option value="en"> English</option>
     <option value="es"> Spanish</option>
       <option value="fr"> French</option>
    </select>
	<p class="normal_text">A woman`s body performing to its utmost capability- that`s what pregnancy is. From conception to delivery, the female form is tried and tested to carry and bring forth new life. The natural action can be quite exhausting! If incorporating a pregnancy massage, the benefits to the expecting mother are incredible.</p></form>
</body>
</html>');
?>

i have a small doubt. phpcode works in this? i am using php for retriving data from database in html . is it works? i tried but colors not changing.

Would this do the trick?

<?php
    if (!isset($_GET['language']) || empty($_GET['language'])) {
    $_GET['language']='en';
    }
    class Google_Translate_API {

    /**
     * Translate a piece of text with the Google Translate API
     * @return String
     * @param $text String
     * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
     * @param $to String[optional] Language to translate $text to
     */
    function translate($text, $from = '', $to = 'en') {
        $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
        $response = file_get_contents(
            $url,
            null,
            stream_context_create(
                array(
                    'http'=>array(
                    'method'=>"GET",
                    'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
                    )
                )
            )
        );
        if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
            return self::_unescapeUTF8EscapeSeq($matches[1]);
        }
        return false;
    }

    /**
     * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
     * @return UTF-8 String
     * @param $str String
     */
    function _unescapeUTF8EscapeSeq($str) {
        return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
    }
}


function display($input) {
$trans_text = Google_Translate_API::translate($input, '', $_GET['language']);
    if ($trans_text !== false) {
        return $trans_text;
    }}
function displayhtml($input) {
    preg_match_all('#<[^>]+>#',$input,$htmlout);
    $input=preg_split('#<[^>]+>#',$input);
    $output='';
    $i=0;
    foreach ($input AS $val) {
        $output.=str_replace(array('\r','\n'),array("\r","\n"),display($val)).$htmlout[0][$i];
        $i++;
        }
    return $output;
    }
echo displayhtml('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
 <form name="language" action="'.$_SERVER['PHP_SELF'].'" method="GET">
    <select onchange = "document.language.submit()"  name="language">
    <option selected="selected"> select-language</option>
       <option value="en"> English</option>
     <option value="es"> Spanish</option>
       <option value="fr"> French</option>
    </select>
    <p class="normal_text">A woman`s body performing to its utmost capability- that`s what pregnancy is. From conception to delivery, the female form is tried and tested to carry and bring forth new life. The natural action can be quite exhausting! If incorporating a pregnancy massage, the benefits to the expecting mother are incredible.</p></form>
</body>
</html>');
?>

end quote.

ASCII values displaying in some of lines.
i changed english to french ...so displaying like this

in english:

A woman`s body performing to its utmost capability- that`s what pregnancy is. From conception to delivery, the female form is tried and tested to carry and bring forth new life. The natural action can be quite exhausting! If incorporating a pregnancy massage, the benefits to the expecting mother are incredible.

in french displaying like this:

Une femme de l `organisme chargé d'effectuer à sa capacité plus grande, celle de l` ce que la grossesse est. De la conception à la livraison, la forme féminine est éprouvée et vérifiée à porter et à donner la vie nouvelle. L'action naturelle peut être assez épuisant! Si la société aura un massage de la grossesse, les avantages pour la femme enceinte sont incroyables.

but french language is too different to compare with google translate and this.
actually in google like this above lines in french.

Une femme de l `organisme chargé d'effectuer à sa capacité plus grande, celle de l` ce que la grossesse est. De la conception à la livraison, la forme féminine est éprouvée et vérifiée à porter et à donner la vie nouvelle. L'action naturelle peut être assez épuisant! Si la société aura un massage de la grossesse, les avantages pour la femme enceinte sont incroyables.

hello....crawn,


this is not supporting any language. you absorb just copy paste this code and test for arabic, hindi, french, spanish. there is lot of difference to compare with google translate content.

<?php
ob_start();
extract($_REQUEST);
extract($_POST);


$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db('murali',$con);
    if (!isset($_GET['language']) || empty($_GET['language'])) {
    $_GET['language']='en';
    }
    class Google_Translate_API {
 
	/**
	 * Translate a piece of text with the Google Translate API
	 * @return String
	 * @param $text String
	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
	 * @param $to String[optional] Language to translate $text to
	 */
	function translate($text, $from = '', $to = 'en') {
		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
		$response = file_get_contents(
			$url,
			null,
			stream_context_create(
				array(
					'http'=>array(
					'method'=>"GET",
					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
					)
				)
			)
		);
		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
			return self::_unescapeUTF8EscapeSeq($matches[1]);
		}
		return false;
	}
 
	/**
	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
	 * @return UTF-8 String
	 * @param $str String
	 */
	function _unescapeUTF8EscapeSeq($str) {
		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
	}
}
 
 
function display($input) {
$trans_text = Google_Translate_API::translate($input, '', $_GET['language']);
    if ($trans_text !== false) {
        return $trans_text;
    }}
function displayhtml($input) {
    preg_match_all('#<[^>]+>#',$input,$htmlout);
    $input=preg_split('#<[^>]+>#',$input);
    $output='';
    $i=0;
    foreach ($input AS $val) {
        $output.=str_replace(array('\r','\n'),array("\r","\n"),display($val)).$htmlout[0][$i];
        $i++;
        }
    return $output;
    }
echo displayhtml('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
 
<body>
 <form name="language" action="'.$_SERVER['PHP_SELF'].'" method="GET">
	<select onchange = "document.language.submit()"  name="language">
  	<option selected="selected"> select-language</option>
       <option value="en"> English</option>
     <option value="es"> Spanish</option>
       <option value="fr"> French</option>
	    <option value="ar"> Arabic</option>
		<option value="hi"> Hindi</option>
    </select>');
	
	$select=mysql_query("select * from sentitems where message_id=1");
$fetch=mysql_fetch_array($select);
echo display($fetch[message_content]);
	echo displayhtml('<p class="normal_text">A woman`s body performing to its utmost capability- that`s what pregnancy is. From conception to delivery, the female form is tried and tested to carry and bring forth new life. The natural action can be quite exhausting! If incorporating a pregnancy massage, the benefits to the expecting mother are incredible.</p></form>
</body>
</html>');
?>

i have a small doubt. phpcode works in this? i am using php for retriving data from database in html . is it works? i tried but colors not changing.

Well remember not to put the <script></script> tag into that function as it can't filter javascript (only html).

Well remember not to put the <script></script> tag into that function as it can't filter javascript (only html).

see once #41,#42 post. plz......

Not supporting every language. Below are the codes for each language. Did you use these short codes?

ar = Arabic
bg = Bulgarian
ca = Catalan
zh-CN = Chinese
hr = Croatian
cs = Czech
da = Danish
nl = Dutch
en = English
tl = Filipino
fi = Finnish
fr = French
de = German
el = Greek
iw = Hebrew
hi = Hindi
id = Indonesian
it = Italian
ja = Japanese
ko = Korean
lv = Latvian
lt = Lithuanian
no = Norwegian
pl = Polish
pt = Portuguese
ro = Romanian
ru = Russian
sr = Serbian
sk = Slovak
sl = Slovenian
es = Spanish
sv = Swedish
uk = Ukrainian
vi = Vietnamese

Not supporting every language. Below are the codes for each language. Did you use these short codes?

ar = Arabic
bg = Bulgarian
ca = Catalan
zh-CN = Chinese
hr = Croatian
cs = Czech
da = Danish
nl = Dutch
en = English
tl = Filipino
fi = Finnish
fr = French
de = German
el = Greek
iw = Hebrew
hi = Hindi
id = Indonesian
it = Italian
ja = Japanese
ko = Korean
lv = Latvian
lt = Lithuanian
no = Norwegian
pl = Polish
pt = Portuguese
ro = Romanian
ru = Russian
sr = Serbian
sk = Slovak
sl = Slovenian
es = Spanish
sv = Swedish
uk = Ukrainian
vi = Vietnamese

yaaa....i used this short codes only.. but it does not supporting urdu, hindi. and also in french appearing é instead of é. i was posted my page in #42. plz look at that once.

Well remember

thanks crawn.....you gave a better and best solution for this. problem solved.

but it is working fine in firefox only. not supporting in exploer. ascii values comming. hindi, arbic convertion problem.

Not supporting every language. Below are the codes for each language. Did you use these short codes?

sorry for disturb..
now working fine in all browsers. but each page taking minimum 50sec. to load. there is any options to avoid this problem?

sorry for disturb..
now working fine in all browsers. but each page taking minimum 50sec. to load. there is any options to avoid this problem?

on the fly translation will take time, and other people's server space, so there isn really a way to speed it up

on the fly translation will take time, and other people's server space, so there isn really a way to speed it up

you mean it could not be problem in server?

Well remember not to put the <script></script> tag into that function as it can't filter javascript (only html).

so.......user entering registeration form in another language in that time it will not support javascript? how to do in javascript also why b'coz alertboxes appear that language alerts ony.

i am getting problem when flash in my webpage? it is not working. plz help me.

flash is a graphic
google translator translates text

flash is a graphic
google translator translates text

Yes and the only way you can translate flash is by designing the flash so all language output uses the google api. I think google might have a flash api too.

another reason to avoid flash

hello crawn,


each page taking minimum 50sec. to load every time. but i have 2mbps fastness in server. it is also takingminimum 40sec to load each and every page. is there any options to avoid this problem?

hello crawn,


each page taking minimum 50sec. to load every time. but i have 2mbps fastness in server. it is also takingminimum 40sec to load each and every page. is there any options to avoid this problem?

It is because there are so many small translations that are being made. Try using the display function less often or less html code in the displayhtml function. The only other option would be to tweak the displayhtml function.

It is because there are so many small translations that are being made. Try using the display function less often or less html code in the displayhtml function. The only other option would be to tweak the displayhtml function.

yaa....i seleted next option that only. in that occuring charecter set problem. when i am using 'charset=iso-8859-1' then translate into other language ascii values are coming insted of some characters. when using charset=UTF-8 some charecters are not translating. it is totally characterset problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.