How to return apostrophe when using Google Translate API for Android?
Asked Answered
A

4

5

I have an Android application that uses Google Translate API. Everything works great, including when I tried to translate phrases that include apostrophe like "We've eaten" to Spanish.

However, problems occur when the translation result I should be getting back contains an apostrophe. For example, when I translate a Spanish phrase, "A ver", into English, it returns "Let&#39s see" with a ";" after "9". It seems like whenever I have a phrase that should return an apostrophe, it returns "&#39" with a ";" after "9". (Not placing ";" after "9" because it gets converted to an apostrophe by stackoverflow).

I can think of a way to solve it. After I get the translation result, I can match the string for ""&#39" + ";" and replace it with an apostrophe.

However, I don't feel like this is the way I should approach it. It's very unlikely that a user will actually type in "&#39" as an input for translation, but hard coding a manual conversion like this seems like it might cause problems down the road. I'll love to hear your thoughts on this.

Please let me know how I should fix/approach this issue.

Thank you!

Ackerley answered 14/11, 2016 at 4:50 Comment(0)
S
3

You are correct hard codding is not solution, But you can convert this HTML entity back to apostrophe, by Using HTML classes provided already.

Html.fromHtml((String) "Let's see").toString()

Above code will convert any valid HTML entity. I Hope this is what you are looking for.

Suzettesuzi answered 14/11, 2016 at 5:9 Comment(2)
Thanks a lot Nakul! Everything seems to work fine. However, I don't understand why this is required. I'm parsing a JSON object, does that mean it's an HTML entity or is it an HTML entity because I'm getting the JSON object from an API? And if so, do I need to do this if I'm retrieving information from any API? Thanks again! Will like to understand the reasoning completely to avoid causing indirect errors by implementing this.Ackerley
@CharlesLi I think, I depends upon which API you use, because Google Translate API can be used by websites too, that's the reason it is HTML friendly. That's what I think.Suzettesuzi
V
14

The best solution is to add &format=text to your query.

Volkan answered 22/12, 2018 at 10:57 Comment(0)
S
3

You are correct hard codding is not solution, But you can convert this HTML entity back to apostrophe, by Using HTML classes provided already.

Html.fromHtml((String) "Let's see").toString()

Above code will convert any valid HTML entity. I Hope this is what you are looking for.

Suzettesuzi answered 14/11, 2016 at 5:9 Comment(2)
Thanks a lot Nakul! Everything seems to work fine. However, I don't understand why this is required. I'm parsing a JSON object, does that mean it's an HTML entity or is it an HTML entity because I'm getting the JSON object from an API? And if so, do I need to do this if I'm retrieving information from any API? Thanks again! Will like to understand the reasoning completely to avoid causing indirect errors by implementing this.Ackerley
@CharlesLi I think, I depends upon which API you use, because Google Translate API can be used by websites too, that's the reason it is HTML friendly. That's what I think.Suzettesuzi
B
1

Thanks Guillaume. For those using php.

   $translation = $translate->translate($stringToTranslate, ['target' => $target, 'format' => 'text']);
Beabeach answered 7/5, 2021 at 13:33 Comment(0)
H
1

Thanks Guillaume. For those using go. (api v3)

req := &translatepb.TranslateTextRequest{
    MimeType: "text/plain", // add this line to request
}
Hysteria answered 3/11, 2021 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.