Code Monkey home page Code Monkey logo

Comments (6)

taishi55 avatar taishi55 commented on August 20, 2024

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.

daniel-jones-deepl avatar daniel-jones-deepl commented on August 20, 2024

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.

taishi55 avatar taishi55 commented on August 20, 2024

@daniel-jones-deepl

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.

daniel-jones-deepl avatar daniel-jones-deepl commented on August 20, 2024

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.

taishi55 avatar taishi55 commented on August 20, 2024

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.

Wizzel1 avatar Wizzel1 commented on August 20, 2024

@daniel-jones-dev are there any news about the billed_characters property on the translate reponse?

from deepl-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.