DeepLcom / deepl-node

Official Node.js library for the DeepL language translation API.
MIT License
359 stars 23 forks source link

400 Bad request errors with Japanese texts #55

Open ttreder-explorance opened 3 hours ago

ttreder-explorance commented 3 hours ago

Describe the bug We are getting 400 errors when calling POST /v2/translate with the DeepL-node library translateText() method when using Japanese comments from the following file.

To Reproduce Steps to reproduce the behavior:

Expected behavior The /v2/translate endpoint should have a consistent behaviour when translating Japanese texts.

Environment:

Additional context We did try the translation API with a lot of different languages, so far Japanese seems to be the only one causing this issue. Also worth noting that when we use the /document DeepL API translate the document correctly.

daniel-jones-dev commented 3 hours ago

Hi @ttreder-explorance, can you please share the code you used, or the array indices in each batch? I was not able to reproduce the issue so far, using the first 60 or 100 array items.

ttreder-explorance commented 3 hours ago
const translator = new Translator(YOUR_API_KEY, {
  maxRetries: 5
  minTimeout: 20000,
});
log.getLogger('deepl').setLevel('debug');
let searchParams = new URLSearchParams();
let commentsToTranslate: AnalysisMappedComment[] = [];
comments.forEach((comment) => {
        searchParams.append("text", comment);
        const commentsByteLength = searchParams.toString().length;

        if (commentsByteLength > 122880) {
          const result = await translator.translateText(
               commentsToTranslate,
               null,
               "en-US",
          );
          // Do what you want with the result

          searchParams = new URLSearchParams({ text: comment });
          commentsToTranslate = [comment];
        } else {
          commentsToTranslate.push(comment);
        }
      });
}

if (commentsToTranslate.length > 0) {
      const result = await translator.translateText(
            commentsToTranslate,
            null,
            "en-US",
       );
       // Do what you want with the result
}