Get exact cost of a single translation API call

1,882 views
Skip to first unread message

Ariel Walden

unread,
Oct 13, 2017, 9:04:26 AM10/13/17
to Google Cloud Translation API
My app will send some text to the Translate API to be translated. Google will charge me for this based on the number of characters (code points) in the query.
Is there a way to get a response that includes the cost of that particular request, or at least the character count of that request? Google may count the characters differently from me (what does it do, for instance, with invalid HTML tags -- does it charge for them or not?) So I would like to be able to know exactly how many characters Google counted for each api call.
Is there are way to get that information?

George (Cloud Platform Support)

unread,
Oct 13, 2017, 11:44:17 AM10/13/17
to Google Cloud Translation API
Hello Ariel, 

There is no risk of sending HTML tags in a call to the Translation API if one is aware that only actual useful text is expected, without formatting. Of course, it may happen that one sends HTML tags by mistake, and gets charged for it, but these should be in practice extreme cases, as it is easy to verify your text payload programmatically prior to the API call, and exclude formatting tags as useless. You may check for more detail on the "Translating Text" documentation page

The "Pricing" page explains how charges are calculated and applied, the relevant paragraph being: "Price is per character sent to the API for processing, including whitespace characters. Empty queries are charged for one character. Google charges on per character basis, even if the character is multiple bytes, where a character corresponds to a (code-point). For example, translating "こんにちは" to English counts as 5 characters for the purposes of billing."

In short, you get indeed charged per character, as you stated initially. 

Ariel Walden

unread,
Oct 14, 2017, 3:59:11 PM10/14/17
to Google Cloud Translation API
Hi George,

Thanks for your answer.

You write: "as it is easy to verify your text payload programmatically prior to the API call"

Could you give an example of the lines of code to do this, in Javascript or PHP? In other words, since it is easy, what code should I use to count characters prior to sending them to the Google Translate API (especially since they have all been URI-encoded first, e.g. a space will be sent to Google as %20).

For instance, if I'm sending a string called myString, it's not quite as simple as (in Javascript)

myString.length;

since, if this string includes a lot of HTML, this is not accurate.

What function what Google recommend I use so I can get an accurate character count, identical to the character count I will be charged for?

Thanks,
Ariel

Ariel Walden

unread,
Oct 14, 2017, 3:59:11 PM10/14/17
to Google Cloud Translation API
Hi George,

Just one more comment. You write: "There is no risk of sending HTML tags in a call to the Translation API if one is aware that only actual useful text is expected, without formatting."

But the Google Translate API specifically accepts HTML as well as plain text (see the "Format" parameter listed here: https://cloud.google.com/translate/docs/reference/translate).

So we certainly are sending text with a lot of HTML tags. And so I'm not sure how easy it is to verify programmatically (before sending to Google) how many chargeable characters there are. But as per my previous post here, if you could supply such a Google-approved function, that would be great.

Thanks,
Ariel

George (Cloud Platform Support)

unread,
Oct 16, 2017, 3:05:33 PM10/16/17
to Google Cloud Translation API
Hi Ariel, 

The link you provided points to a page that refers to a page dealing with the mapping of an RPC method to one or more HTTP REST APIs. This type of formatting tokens are not billed, just the submitted text. JSON formatted data looks like:  myObj = { "name":"John", "age":31, "city":"New York" }; In particula, for the Translation API, you can see a JSON formatted object such as: 
{
  'q': 'Hello world',
  'q': 'My name is Jeff',
  'target': 'de'
}

The q parameter is used to specify each text string to translate.

George (Cloud Platform Support)

unread,
Oct 16, 2017, 3:09:25 PM10/16/17
to Google Cloud Translation API
Regarding counting characters in a string, Java provides a special method for it: public int length(). Other languages have similar facilities, so it would be a matter of convenience to choose the right solution. 

Ariel Walden

unread,
Oct 26, 2017, 9:36:38 AM10/26/17
to Google Cloud Translation API
Hi George,
Yes, but I would still want to be able to verify that my function for counting characters is the same as the one Google has charged me for!
Is there no way to get the Google Translate API to return the number of characters it has charged for -- perhaps as an extra JSON property when it returns the translated string?

Ariel Walden

unread,
Nov 9, 2017, 12:24:22 PM11/9/17
to Google Cloud Translation API
I would still expect that Google's API provides a way for developer's to know exactly how many characters Google has just charged them for.
Any progress on this at all?

Kartik Godawat

unread,
Apr 19, 2018, 11:54:22 AM4/19/18
to Google Cloud Translation API
Hi George,
I have a case where I need to convert a word document with a lot of original data kept intact(40-50%), but these are just words in middle of a sentence, if I make 2 API calls separating them then results aren't good because context is lost. If I use notranslate will those tags also be charged? If yes, then will defining it like 2nd will cost me more?
  • <span translate="no"> </span>
  • <span class="notranslate"> </span>

Ariel Walden

unread,
May 1, 2018, 9:51:01 AM5/1/18
to Google Cloud Translation API
It would be great to get an official response to these questions -- Kartik's and my own. Anyone?

Harshad Patel

unread,
May 29, 2018, 7:50:03 AM5/29/18
to Google Cloud Translation API
Hi Team,

I am also having same question. If I am using Python to translate content and sending no-translate tags along with content then will those tags also be charged?

George (Cloud Platform Support)

unread,
Jul 4, 2018, 11:53:02 AM7/4/18
to Google Cloud Translation API
Hello Ariel, 

Work is in progress on exactly the feature you would like to see implemented. There is no implementation date as yet, and priority does not appear high at first sight, as characters may be counted pragmatically. There is also no reason to believe that your own program's implementation of character counting were less reliable than the future feature from the API side. 

Conal Loughrey

unread,
Aug 24, 2018, 9:19:32 AM8/24/18
to Google Cloud Translation API
Hi George,
Can we get a response to Kartik's question., 
If I use notranslate will those tags also be charged? If yes, then will defining it like 2nd will cost me more?
  • <span translate="no"> </span>
  • <span class="notranslate"> </span>
And a followup question, will we be charged for any text within the span even though it is not going to be translated?

Thanks,
Conal

George (Cloud Platform Support)

unread,
Aug 24, 2018, 5:11:08 PM8/24/18
to Google Cloud Translation API
Hello Conal, 

If the tags are considered part of the text, so simple characters, they will be charged as part of the useful text to be translated. This consideration depends on various factors. The markup described by Kartik has to be recognized as markup in an HTML page, to be effective. This happens if the marked-up text is a part of an HTML website submitted for translation through Google Translate, as HTML page, not as simple text. 

anthony T

unread,
Dec 26, 2018, 11:42:15 PM12/26/18
to Google Cloud Translation API
Hello, 

Any news on this please ? 
Even if "characters may be counted pragmatically", which I tend to disagree with, it seems logical in terms of visibility to make it accessible

thanks 

George (Cloud Platform Support)

unread,
Jan 1, 2019, 2:53:48 PM1/1/19
to Google Cloud Translation API
Hello Anthony, 

You should not tend to disagree about characters being easily counted in code, Java script offers:

Return the number of characters in a string:

var str = "Hello World!";
var n = str.length;

All other programming languages offer similar facilities in a form or other. 

Glad to report that great progress has been made on the above requested feature's implementation, but final stage is still not reached yet. It may be difficult to estimate a release date, but on this first day of the year hopes are at a maximum. Have a great and fulfilling year 2019. 

anthony T

unread,
Jan 1, 2019, 4:04:59 PM1/1/19
to Google Cloud Translation API
Hi George, 

With python 2.7.10 which supports google translate api 
len("こんにちは") is 15 which is the example given on https://cloud.google.com/translate/pricing and which is supposed to be billed for 5 characters

Your response implies that google translate is billing me exactly this way (15 instead of 5 characters) 
So I'm happy that all hopes are at a maximum but I have a very basic issue here for my other requests in thai or hebrew, for example : 
len('ราค') is 9
len('גאון') is 8

What am I missing please ?

Thanks, happy new year 

George (Cloud Platform Support)

unread,
Jan 2, 2019, 9:17:55 AM1/2/19
to Google Cloud Translation API
This discussion group is oriented more towards general opinions, trends, and issues of general nature touching the app engine. For coding and programming architecture, such as str.length;, you may be better served in dedicated forums such as stackoverflow, where experienced programmers are within reach and ready to help. 

Id-Extras InDesign Scripts

unread,
Apr 1, 2019, 3:09:23 PM4/1/19
to Google Cloud Translation API
Hi George,
Almost a year now has passed. Any movement on this issue?
Ariel

Ali T (Cloud Platform Support)

unread,
Apr 29, 2019, 1:24:16 PM4/29/19
to Google Cloud Translation API
Hi,

There are currently no new updates on this matter. I created a public feature request where you can keep track of any future updates related to this matter.

I would suggest starring it as it subscribed you to future updates and also counts as an upvote towards this feature. 
Reply all
Reply to author
Forward
0 new messages