0

I use Deepl for translating HTML text. If I have a .gif file in my HTML it is okay but if I use .jpg I get the following error. I know Deepl uses gson in his lib but don't know how to solve it, I am wondering if any Deepl guys know about this issue? I use Deepl java lib.

Any solution welcome in advance?

Error location in the code:

        boolean failed = false;
        String translationResult = "";
        try
        {
            TextResult result = translator.translateText( text, source, target, translationOptions );
            translationResult = result.getText( );
        }
        catch (IllegalStateException | JsonSyntaxException exception)
        {
          failed = true;
          //...
          exception.printStackTrace( );
          Show.error( "failded"+ exception.getMessage( ) +"\n"+ exception.getCause( ) );
        }
        
        if(failed) {
            Show.error( "failded" );
        }

Error

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
13/10/2022 10:22:45,483 [AWT-EventQueue-0] ERROR [JBroker] User Message: faildedjava.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:225)
    at com.google.gson.Gson.fromJson(Gson.java:991)
    at com.google.gson.Gson.fromJson(Gson.java:956)
    at com.google.gson.Gson.fromJson(Gson.java:905)
    at com.google.gson.Gson.fromJson(Gson.java:876)
    at com.deepl.api.parsing.Parser.parseErrorMessage(Parser.java:47)
    at com.deepl.api.Translator.checkResponse(Translator.java:769)
    at com.deepl.api.Translator.translateText(Translator.java:174)
    at com.deepl.api.Translator.translateText(Translator.java:110)
    at com.upsilon.screens.translator.DeeplTranslator.translate(DeeplTranslator.java:75)
lsa
  • 63
  • 6

2 Answers2

1

I'm a maintainer of the DeepL Java client library.

This problem seems to be caused by a bug in the Java client: the HTTP response is assumed to be valid JSON and this exception is thrown because the response was not valid. I'll work on a fix to the client, and update when it's released.

Edit 19.10.2022: A bug causing JsonSyntaxException to be thrown is fixed in v0.2.1, however the underlying issue is still unknown.

Daniel Jones
  • 116
  • 1
  • 4
  • Thanks for the rapid reply. Is it also possible to add a feature such as `Translator.isAuthKeyValid("key");` to the class Translator? – lsa Oct 13 '22 at 18:19
  • Sorry, I should have been clearer before: the Java client throws a JsonSyntaxException due to a bug; this bug is fixed in version 0.2.1. With that bug fixed the error response should be more helpful. However I am not sure why your input HTML was rejected in the first place, and I wasn't able to reproduce it yet. Can you share a minimal example input that triggers the error? – Daniel Jones Oct 19 '22 at 12:50
1

As @Daniel Jones mentioned it is a bug that Deepl going to fix it.

To work around this until the definitive Deepl solution, I did the following.

  1. Extracted all <img src ="" /> tags from HTML text and put them into a hash map with a dynamically created <ignoreImage> image1</ignoreImage> tags as key and the original <img src ="" /> tags as value Map<String,String> imageTagMap = new HashMap();

  2. Replaced all <img /> tags with <ignoreImage>image1</ignoreImage> tags in my HTML text

  3. Translated the text and after getting translation result replaced the <ignoreImage>image1</ignoreImage> tags with the original tags according to my imageTagMap .

I think this solution can also be used in Deepl java lib to avoid unnecessary image transport over the network to their server.

lsa
  • 63
  • 6