Home > Enterprise >  Why Deepl can't exclude jpg images while translating html text?
Why Deepl can't exclude jpg images while translating html text?

Time:10-14

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)

CodePudding user response:

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.

CodePudding user response:

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.

  • Related