Comments (6)
For someone who also get the same suggestion to use const inputText = encodeURIComponent(translateText)
, I suggest you to use the code below.
const inputText = translateText.split("\n").join("%0A");
encodeURIComponent()
will let you have a super expensive bill
from deepl-node.
Hi @taishi55 Taishi, thanks for creating this issue.
Firstly, our API does understand text in multiple lines. For example if you translated the following:
const multilineText = "こんにちは。\n" +
"これは複数行のテキストです。\n" +
"\n" +
"これは別の段落です。";
then responseDeepL.data.translations[0].text
would give something like "Hello.\nThis is a multi-line text.\n\nThis is another paragraph."
.
Secondly, you shouldn't be billed for more characters due to using encodeURIComponent()
. I have just tested your example and confirmed my account is billed 5 characters in your example using こんにちは
.
You can check your usage using the /usage
endpoint; the character_count
field in the response will increment as you make requests. However there may be a delay in updates to the value, up to 10 seconds or so. Admittedly this is not very convenient, so we are working on adding a billed_characters
field to the /translate
response, so that you can immediately see the number of billed characters with every translate request.
Some further explanation about character encoding:
DeepL API accounts are billed per Unicode character, so a
, こ
, and 訳
are each billed as a single character. Punctuation and whitespace are also billed per character, so 。
, space, and newline characters count as single characters. FYI the multi-line example above is billed as 33 characters.
encodeURIComponent()
is needed to properly encode the text for transmission as an HTTP request. Using this encoding, some characters become longer, for example こ
is encoded as %E3%81%93
. However, this encoding is only necessary for transmission over HTTP: DeepL counts characters after removing this encoding, so although こんにちは
is encoded as 45 characters, DeepL bills it as 5 characters.
You should use encodeURIComponent()
to encode your requests. Some inputs might work as expected (as in your example), but others will not, depending on what characters are contained within the input text.
I hope that this explanation clarifies the situation.
from deepl-node.
The \n
didn't work with the cURL query. The only way of formatting multi-line sentences is to replace the \n
with %0A
.
Thanks for the clear explanation. The price description says $25.00 per 1 million characters
, so I thought encodeURIComponent ()
let to the high usage.
In the future, is it possible for us to reduce the price per usage? We might need to switch another translation API due to the constant price-plan regardless of a large-volume of usage.
from deepl-node.
HI @taishi55,
I have to assume you are not encoding your input text using encodeURIComponent()
as I suggested. The newline character \n
needs to be encoded correctly; after URL-encoding it is %0A
-- this is why your replacement-strategy works. I still recommend using encodeURIComponent()
to URL-encode your text in case your text contains any other character that requires encoding.
Here is a full example translating multiline-text, you will need to replace your auth key, and deeplURL
if you are using a DeepL API Free account.
This example translation will be billed 33 characters, that is how many characters are in the unencoded input (including punctuation).
Note that this example does use encodeURIComponent()
; without this function the request will fail.
const DEEPL_KEY = "DEEPL_AUTH_KEY";
const deeplURL = "https://api.deepl.com/v2/translate";
const targetLang = "en-US";
const multilineText = "こんにちは。\n" +
"これは複数行のテキストです。\n" +
"\n" +
"これは別の段落です。";
const encodedText = encodeURIComponent(multilineText);
const responseDeepL = await axios.post(
deeplURL,
`text=${encodedText}&target_lang=${targetLang}`,
{
headers: {
Authorization: `DeepL-Auth-Key ${DEEPL_KEY}`,
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "axios 1.2.0-alpha.1",
},
}
);
const translatedText = responseDeepL.data.translations[0].text;
console.log(translatedText);
// Prints: "Hello.\nThis is a multi-line text.\n\nThis is another paragraph."
Regarding pricing: unfortunately this is outside of my knowledge. Discounts might be available for high volume customers; you can contact our support team about that.
from deepl-node.
Thank you for the detail. I restarted using encodeURIComponent()
. I'll close this since it was my misunderstand.
I will need to change my app's price plan. The usage was more expensive than I expected (My mistake).
from deepl-node.
@daniel-jones-dev are there any news about the billed_characters
property on the translate reponse?
from deepl-node.
Related Issues (20)
- Feature Request: Markdown Handling HOT 1
- The translation API returns an empty body in Cloudflare Workers. HOT 3
- downloadDocument() response is empty for PDFs HOT 1
- TypeError HOT 5
- XML tags with attributes change the order of the translation when tagHandling is XML but are OK when tagHandling is off HOT 5
- Uncaught TypeError: os.type is not a function HOT 5
- [tests] Jest may incorrectly detect open handles when running the tests
- Deepl Limitations - Timeout Errors during concurrent calls HOT 8
- SSL error using node 17+ HOT 4
- feature: translate a text to multiple Languages HOT 2
- Docs: `createGlossaryFromCsv` should be `createGlossaryWithCsv` HOT 1
- Axios proxy as URL HOT 1
- Update axios to latest version HOT 2
- 'ar' missing from CommonLanguageCode type HOT 4
- Translating HTML with English text to French adds HTML Tags and full sentence that is not in the input text HOT 2
- Circular dependency index.js ➜ parsing.js ➜ index.js HOT 4
- Translator Option 'serverUrl' is misleading HOT 1
- Increasing "socket hang up" errors when updated to 1.13
- Spaces before XML tags are randomly removed in the translation and quotes around the tags might end up within the tag
- The API doesn't translate English to Spanish when there's a small typo; it just returns the English text. However, it works for English to French. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepl-node.