Spiga

Google Translate Tool Class v0.9

by Gabi Solomon

As almost all of my classes i have developed this class for a project requirement, and i decided to make it public on phpclasses.com that is available for download at this link.

This is quite a simple class can translate text, and webpages using Google translate services.

The class usses curl to make a tranlation request to the Google translate Web page and asking for the translation of the given text or URL from the selected from and to languages. The result is parsed to obtain the translation and returned by the class.

The translation class supports all the languages currently available at google including:
English, German, Spanish, French, Italian, Portuguese, Arabic, Chinese and Russian.

  • http://www.bs1.ru Roman

    Great class. But translate_Text function return not translated result :( , how to fix it?

  • http://www.gsdesign.ro/ Gabi Solomon

    did you run the example from phpclasses ?
    if you still have problems contact me on any IM service you prefer. My id is in the about page.

  • http://www.bs1.ru Roman

    Thank you for response! Yes I downloaded from phpclasses and run it on local xamplite server (curl is on, Windows Vista). Translate page working great, but translate_Text function return the same text with no error :( .

  • http://www.bs1.ru Roman

    I made some changes to googleTranslateTool.class.php:

    http://5896408.googlepages.com/googletranslate (posted there, be cause you blog no accept code).

    and it works great. Thank you for great class!

  • http://www.gsdesign.ro/ Gabi Solomon

    the first change you made is to make curl do a GET request rather then a POST … wich might have caused your error.

    I cant seem to understand the second one

  • http://www.bs1.ru Roman

    Yes, but it work now (when i post the language data it fails).
    I’m in Russia and may be google returns other code for my location.

  • pratyush talukdar

    Hi, i am trying to translate a whole site with this. Is this possible? If so then how to convert the links on the page which comes as a result from the google to my own site. Currently if page is there in your site say – http://www.domain.com/index.php, and there is a link to it, then it will be returned with concating http://66.102.9.104/translate_c?hl=ro&safe=off&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools&langpair=‘.$langpair.’&u=’.urlencode($url);
    So how can i remove this.

  • http://www.gsdesign.ro/ Gabi Solomon

    yes you can … you just need to parse the translated result again and replace those links … youn can use prer_replace for example

  • http://www.solucionmovil.com.ar/traductor/ I cannot tranlate text in arabic, chinnese, etc

    That class is wonderfull!
    but I cannot translate text in some idioms.
    I can translate web very web but I cannot text.
    look here: http://www.solucionmovil.com.ar/traductor/
    upper left i put text But we cannot read very well instead the web is translate very well

    thank…and sorry my bad englis

  • http://www.gsdesign.ro/ Gabi Solomon

    if you are refering to those weird characters you need to change the encoding in the browser output

  • guillermo

    thanks….but I don’t know how I can do that

    web: http://www.solucionmovil.com.ar/traductor/index2.php

  • http://www.gsdesign.ro/ Gabi Solomon

    you can try several things :
    header(‘Content-type: text/html; charset=utf-8′, true);

    and
    [php]
    echo ‘

    ‘.utf8_encode($result).’

    ‘;
    [/php]

  • http://www.solucionmovil.com.ar/traductor/index3.php guillermo

    Thank, I try those things, but It dosn’t work too.

    my code now is: (http://www.solucionmovil.com.ar/traductor/index3.php)

    <!—->

    glr

    translate_Text(‘Hello, this is a test of a translation’);
    if ( $result===false ) echo $translator->return_error();
    else echo $result;
    echo ” \n”;
    header(‘Content-type: text/html; charset=utf-8′, true);
    echo utf8_encode($result);
    ?>

  • http://www.gsdesign.ro/ Gabi Solomon

    the header function must be called before any output ( echo )

  • guillermo

    finally I coud translate! thank you

  • http://www.gsdesign.ro/ Gabi Solomon

    i am glad to hear it

  • http://problemunderphp4.0 Lukasz

    I’m using your script in php 4.0 version (safe mode) free web serwer and I’ve got error langFrom not set. To test it I use php in 5.0 version and it works great. What could be wrong?

  • http://www.gsdesign.ro/ Gabi Solomon

    it should work if u can open a connection using file().
    And that error doesnt not appear to come from safe mode on.
    I will look into it and let u know if i find something

  • Lukasz

    I’ve made example to demonstrate me problem:

    http://domdzieckagw.private.pl/test/example.php

  • Paul Peelen

    Hi,

    I am having some problem translating from swedish to english. I tried houndrets of different things, but can’t get it to work, the problem in the character encoding… but I can’t seem to find.
    It works for me when I put my text in a html file whith UTF-8 as content-type, but not when getting text from the database (Latin1) and sending it to the translator class.

    I tried even an simple test like this one:
    http://clares.wordpress.com/2008/10/17/traduzindo-textos-com-php-google-translate-tool/

    Change the text to “På en ö”, which should be “on an island” in english. I get an weird UTF8 like result.

    Do you have any idea what the problem can be?

    Best regards,
    Paul Peelen

  • http://www.gsdesign.ro/ Gabi Solomon

    @ Paul Peelen
    1. Sorry but i dont have too much experience with encoding
    2. A credit for that tool would be nice ;)

  • choco

    Mr. Gabi, I have ran your script but the result shows original text instead of the proper output.
    What I did is to translate a short sentence from English to Chinese, here is the output I got:
    Text :Hello, this is a test of a translation
    result :Hello, this is a test of a translation
    result with utf8 _encode :Hello, this is a test of a translation

  • http://www.gsdesign.ro/ Gabi Solomon

    @choco
    please check that you have the transtaltion combination set properly (the stings that tell the class from witch and to what language are you translating)

  • http://googlechangedtranslateurl ondrej

    Your tool was working nice, thanks for it! But it seems that a couple days ago google changed something and url translation stopped working (i guess google chaned the url where you send the requests) ..did you notice the same problem?

    Btw do you guys know how much often can you send request for translation and not be blocked by google?

    Thanks,
    Ondrej

  • Dip

    Hey I tried your code but it's generating an error ''langFrom not set".
    Can you help me?

  • http://www.gsdesign.ro/ Gabi Solomon

    @dip
    i think you dont set the from and to language combination corectly

  • Dip

    Sir,
    I have downloaded the code from phpclasses.org and tried to run as it is but in vain.
    Please help me.
    Dip

  • http://alpha.ulx.ru/ Figura

    Much better solution is to request Google API directly. Here is an example:

    http://code.google.com/p/gtranslate-api-php/

    • solomongaby

      at the time i wrote this class i dont think that there was a google api

  • ilovephp

    Any chance of showing us some PHP code for usage?

  • http://www.websitedesign.co.uk/ Louise

    this is going to be so useful, personally i can think of around 5 diff projects to work on using this, hmmm..

  • frank

    hi
    I'm trying to check out your google translate class but I get:
    langFrom not setlangFrom not set
    help?!

  • http://www.gns-bloggers.com Hamza Ahmed

    Thanks for sharing your hard work……!!!

  • http://maverickmoneymakersreview.ws/ Maverick Money Makers

    hello, The class accesses the Google Translate site and emulates … page is returned. you can free!

  • Andreajoy35

    ok asa