1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Translate text using google API

Discussion in 'Facepalm' started by Kyd, May 6, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I tried, but Response Status is 400 on website, so there is error, but console dont say..
    PHP:
    function translate1($from_lan$to_lan$text){
        
    $json json_decode(file_get_contents('http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' urlencode($text) . '&langpair=' $from_lan '|' $to_lan));
        
    $translated_text $json->responseData->translatedText;
        return 
    $translated_text;
    }

    public function 
    onChat11(PlayerChatEvent $e){
       
    $e->getPlayer()->sendMessage($this->translate1("en""cz""Hello"));
    }
     
    jasonwynn10 likes this.
  2. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    It produces the following error, which I think that is fairly self-explanatory.
    Code:
    {
      "responseData": null,
      "responseDetails": "Please use Translate v2.  See http://code.google.com/apis/language/translate/overview.html",
      "responseStatus": 403
    }
     
    Last edited: May 6, 2017
    HimbeersaftLP and jasonwynn10 like this.
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I cant find link for api v 2 :D
     
  4. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    try something like this?
    PHP:
    public function translate(string $textstring $to_lang "en"){
        
    $json json_decode(file_get_contents('https://translation.googleapis.com/language/translate/v2?key=YOUR_LICENSE_KEY_HERE&q=' urlencode($text) . '&target=' $to_lang '&format=text'));
        
    $translated_text $json->data->translations[0]->translatedText;
        return 
    $translated_text;
    }
     
  6. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    That's possible o_O ??
     
  7. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Google's translation API is no longer free... the only API that actually works for free right now is Bing, which allows you to translate 2 million characters (not words) before you need a paid subscription.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.