PHP - Language Translator

From Global Programming Syntax

Jump to: navigation, search
YOU MAY USE THIS FUNCTION AT YOUR OWN RISK.

ALTHOUGH IT WORKS I AM NOT RESPONSIBLE OF ANY LEGAL
MATTERS YOU MAY RUN INTO BY USING THIS SCRIPT.

On an advanced content management system, you may want to offer a feature to convert text into different languages. Well I have created a script which uses the google translator to translate your data and send the translation to your website. So first copy the below function into the header of your php file.

function translate($sentence,$languagefrom,$languageto)
{
$homepage = file_get_contents('http://translate.google.com/translate_t');
if ($homepage==false) {$homepage='';}
preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch);
$botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]);
preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput);
$id=0;
while (isset($botinput[0][$id]))
{
preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmp);
$tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]);
$tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]);
$tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]);
$len=strlen($tmp[0][0]);
$len-=1;
$tmp[0][0]=substr($tmp[0][0],0,$len);
 
preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmpname);
$tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]);
$tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]);
$len=strlen($tmpname[0][0]);
$len-=1;
$tmpname[0][0]=substr($tmpname[0][0],0,$len);
 
if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl')))
{
$vars.=$tmpname[0][0]."=".$tmp[0][0].'&';
}
unset($tmp);
unset($tmpname);
$id+=1;
}
$curl_handle=curl_init('http://translate.google.com/translate_t');
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER,
Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
 
$buffer=strip_tags($buffer,'<div>');
preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match);
$match[0][0]=strip_tags($match[0][0]);
return $match[0][0];
}

Then to use the translator, you call the function like below and below is an example of english to chinese:

echo translate('This is the text','en','zh-CN');

The first field in the above function is the text to convert, second field language from, third field is language to. All languages are abbrievated. So below are the abbrievations you may use and that is it. It is as simple as that.

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
Personal tools
languages
page stats
Toolbox