Does Google Translate API support placeholders?

Viewed 1390

Let's say I have a tweet I'll like to translate through the google api.

Text: #sf #fiesta #verano #baile #vamos Fiesta aquí @my_friend

I'm sending the following to the API endpoint https://www.googleapis.com/language/translate/v2:

{1 /} {2 /} {3 /} {4 /} {5 /} Fiesta aquí  {0 /}

This is the response:

{1} {2} {3}} {4} {5}} party over here {0}

This is not what I expected. However, I would like to put in placeholders otherwise the actual hashtags get translated and that doesn't point to the same results as the original tweet.

There's no mention of placeholders on the API docs, so I guess I'm not too surprised that it failed. Maybe they aren't supported. The only thing I've found is this and I'm not sure that it's related.

Is there a documented way to get this working ?

2 Answers

Depending on the language pair, the results will be inconsistent. Google has not been built to handle placeholders and if it works, it won't be by design but by luck.

For example the following simple sentences in English:

I read 0 books.
I read 1 book.
I read 2 books.

Should be translated to this in Vietnamese:

Tôi đọc 0 cuốn sách.
Tôi đã đọc 1 cuốn sách.
Tôi đọc 2 cuốn sách.

But when using markup to try to identify placeholders, results are unexpected:

Tôi đọc <span class = "notranslate"> 0 </span> sách.
Tôi đã đọc <span class = "notranslate"> 1 </span> cuốn sách.
Tôi đã đọc cuốn sách <span class = "notranslate"> 2 </span>.

If you try another strategy, for example, this sentence with a placeholder identified by $count:

I read $count book.

Shoud translate to (in French):

J'ai lu $count livre.

But will be translated to:

J'ai lu le livre $count.

Which Google interprets $count as the name of the book and not a placeholder.

You can try other syntaxes but in some cases, even the placeholder itself is translated...

⚠️ Use Google Translate and placeholders at your own risk but expect surprises

Related