0

I need to translate a part of my wordpress site content using google translate, For example,

$text="translate this content"

to

$translated_text="traduci questo contenuto"

I do not need to find the browser language, etc,. Just direct translation I need. Tried this, seems like it is deprecated. Got it from another question answered here.

 function translate($from_lan, $to_lan, $text){
 $json = json_decode(file_get_contents('https://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;
 }

I don't mind paying for the google API, I need to know how to handle the translation in php code.

  • https://rapidapi.com/blog/lp/google-translate-api/?utm_source=google&utm_medium=cpc&utm_campaign=Alpha&utm_term=google%20translation%20api_e&gclid=EAIaIQobChMIqrvRvdXw7QIVSeDtCh2UjgI3EAAYASAAEgLWzPD_BwE – RiggsFolly Dec 28 '20 at 12:23

1 Answers1

0

google translate currently posts a "application/x-www-form-urlencoded" with a f.req like

[[["AVdN8","[\"je voudrais bien te voir demain\",\"fr\",\"nl\"]",null,"generic"]]]

For an example in posting with PHP, see PHP post request

Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17