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.

Related Posts

  • Great class. But translate_Text function return not translated result :(, how to fix it?
  • 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.
  • 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 :(.
  • 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!
  • 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
  • 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 - 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=...
    So how can i remove this.
  • yes you can ... you just need to parse the translated result again and replace those links ... youn can use prer_replace for example
  • 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
  • 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/index...
  • you can try several things :
    header('Content-type: text/html; charset=utf-8', true);

    and
    [php]
    echo '
    '.utf8_encode($result).'
    ';
    [/php]
  • Thank, I try those things, but It dosn't work too.

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




    <!---->

    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);
    ?>
  • the header function must be called before any output ( echo )
  • guillermo
    finally I coud translate! thank you
  • i am glad to hear it
  • 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?
  • 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/traduzin...

    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
  • @ 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
  • @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)
  • 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?
  • @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
  • Much better solution is to request Google API directly. Here is an example:

    http://code.google.com/p/gtranslate-api-php/
  • at the time i wrote this class i dont think that there was a google api
  • Any chance of showing us some PHP code for usage?
  • this is going to be so useful, personally i can think of around 5 diff projects to work on using this, hmmm..
blog comments powered by Disqus