Google translate v2 api returning non UTF-8 characters in PHP

632 views
Skip to first unread message

Paul Konstanski

unread,
Apr 29, 2015, 1:41:31 PM4/29/15
to google-tra...@googlegroups.com

I am sending a PHP CURL call to send english text to be returned in french. The french accent character comes back with improper coding.

Here is the code:

$url = 'https://www.googleapis.com/language/translate/v2?key='.$api_key.'&q='.rawurlencode($text);
$url .= '&target='.$target;
if($source) $url .= '&source='.$source;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);                 
curl_close($ch);

If I send the text of: Be sure that your students love and know you

I get back: Assurez-vous que vos élèves aiment et vous savez

If I manually go out to: http://translate.google.com I get this. Assurez-vous que vos élèves aiment et vous savez

I know it's an issue with the UTF-8 Encoding, but I'm not sure how to get it to correctly display.

Any hints would be helpful.

Thanks.

Patrice

unread,
May 19, 2015, 12:07:28 PM5/19/15
to google-tra...@googlegroups.com
Hi Paul,

This kind of question is better suited for Stack Overflow. I would suggest making sure your question is clear, following the guidelines of the website, and posting there for the best answer.

Cheers!

Jesse Scherer (Google Cloud Support)

unread,
May 19, 2015, 12:25:45 PM5/19/15
to google-tra...@googlegroups.com
As a followup, some folks on our Translate team tried to reproduce what you reported, and got properly encoded characters. If you do follow up on StackOverflow perhaps you can share some more about your environment.

As an aside, "Post on StackOverflow" is not tantamount to "Go Away!" We also hang out on StackOverflow and monitor the "google-translate" tag, but often times somebody from outside Google will have great ideas.

In any case, I hope you were able to find a resolution here.

Paul Konstanski

unread,
May 19, 2015, 12:29:07 PM5/19/15
to google-tra...@googlegroups.com
Sorry I didn't update this thread... I had posted the question in a few different places and I forgot to get back to this one.

I eventually realized that the issue was not related to Google Translate but instead was how my data was being saved to the mySQL database. The saved characters were not being saved as UTF8 event thought the DB was encoded that way. Once that got resolved, the issue went away. (Sounds simply to put it on paper, but it took me nearly 15 hours of asking/testing/fixing to get it resolved).

So bottom line, I'm all set. Thanks for the followup.

Reply all
Reply to author
Forward
0 new messages