Commit 6f3433da authored by Thomas's avatar Thomas
Browse files

- Fix Lingva truncated translations

- Improve deobfuscation process
parent 07b73474
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

@@ -149,7 +151,7 @@ public class TransAsync {
            } else if (te == MyTransL.translatorEngine.LINGVA) {
                String key = MyTransL.getInstance(te).getLibreTranslateAPIKey();
                //String contentToSendEncoded = URLEncoder.encode(contentToSend, "UTF-8");
                String lingvaURL = MyTransL.getLingvaUrl() + this.params.getSource_lang() + "/" + toLanguage + "/" + contentToSend;
                String lingvaURL = MyTransL.getLingvaUrl() + this.params.getSource_lang() + "/" + toLanguage + "/" + URLEncoder.encode(contentToSend, "utf-8");
                str_response = new Client().get(lingvaURL, this.timeout);
            }
        } catch (IOException | NoSuchAlgorithmException | KeyManagementException err) {
+6 −35
Original line number Diff line number Diff line
@@ -64,18 +64,11 @@ public class Client {
        httpsURLConnection.setConnectTimeout(timeout * 1000);
        httpsURLConnection.setRequestProperty("http.keepAlive", "false");
        httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
            httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
        httpsURLConnection.setRequestMethod("GET");
        //Read the reply
        if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
            Reader in;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), StandardCharsets.UTF_8));
            } else {
                //noinspection CharsetObjectCanBeUsed
                in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
            }
            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0; )
                sb.append((char) c);
@@ -84,13 +77,8 @@ public class Client {
            return sb.toString();
        } else {
            Reader in;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), StandardCharsets.UTF_8));
            } else {
                //noinspection CharsetObjectCanBeUsed
                in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), "UTF-8"));
            }
            StringBuilder sb = new StringBuilder();// TODO Auto-generated catch block
            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0; )
                sb.append((char) c);
            httpsURLConnection.disconnect();
@@ -115,19 +103,12 @@ public class Client {
        URL url = new URL(urlConnection);
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        byte[] postDataBytes;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        postDataBytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
        } else {
            //noinspection CharsetObjectCanBeUsed
            postDataBytes = jsonObject.toString().getBytes("utf-8");
        }
        httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
        httpsURLConnection.setConnectTimeout(timeout * 1000);
        httpsURLConnection.setDoInput(true);
        httpsURLConnection.setDoOutput(true);
        httpsURLConnection.setUseCaches(false);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
        httpsURLConnection.setRequestMethod("POST");
        httpsURLConnection.setRequestProperty("Content-Type", "application/json");
        httpsURLConnection.setRequestProperty("Accept", "application/json");
@@ -140,12 +121,7 @@ public class Client {
        //Read the reply
        if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
            Reader in;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), StandardCharsets.UTF_8));
            } else {
                //noinspection CharsetObjectCanBeUsed
                in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
            }
            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0; )
                sb.append((char) c);
@@ -154,12 +130,7 @@ public class Client {
            return sb.toString();
        } else {
            Reader in;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), StandardCharsets.UTF_8));
            } else {
                //noinspection CharsetObjectCanBeUsed
                in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), "UTF-8"));
            }
            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0; )
                sb.append((char) c);
+17 −15
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -156,11 +157,11 @@ public class Translate {
        String text = spannableString.toString();
        Matcher matcher;

        //Mentions with instances (@name@domain) will be replaced by __o0__, __o1__, etc.
        //Mentions with instances (@name@domain) will be replaced
        int i = 0;
        matcher = mentionOtherInstancePattern.matcher(text);
        while (matcher.find()) {
            String key = "$o" + i;
            String key = "§" + i;
            String value = matcher.group(0);
            if (value != null) {
                this.mentionConversion.put(key, value);
@@ -170,10 +171,9 @@ public class Translate {
        }
        //Extracts Emails
        matcher = Patterns.EMAIL_ADDRESS.matcher(text);
        i = 0;
        //replaces them by a kind of variable which shouldn't be translated ie: __e0__, __e1__, etc.
        //replaces them by a kind of variable which shouldn't be translated
        while (matcher.find()) {
            String key = "$e" + i;
            String key = "§" + i;
            String value = matcher.group(0);
            if (value != null) {
                this.mailConversion.put(key, value);
@@ -182,11 +182,10 @@ public class Translate {
            i++;
        }

        //Same for mentions with __m0__, __m1__, etc.
        i = 0;
        //Same for mentions w
        matcher = mentionPattern.matcher(text);
        while (matcher.find()) {
            String key = "$m" + i;
            String key = "§" + i;
            String value = matcher.group(0);
            if (value != null) {
                this.mentionConversion.put(key, value);
@@ -197,10 +196,9 @@ public class Translate {

        //Extracts urls
        matcher = Patterns.WEB_URL.matcher(text);
        i = 0;
        //replaces them by a kind of variable which shouldn't be translated ie: __u0__, __u1__, etc.
        //replaces them by a kind of variable which shouldn't be translated ie: _
        while (matcher.find()) {
            String key = "$u" + i;
            String key = "§" + i;
            String value = matcher.group(0);
            int end = matcher.end();
            if (spannableString.length() > end && spannableString.charAt(end) == '/') {
@@ -213,11 +211,9 @@ public class Translate {
            }
            i++;
        }
        i = 0;
        //Same for tags with __t0__, __t1__, etc.
        matcher = hashtagPattern.matcher(text);
        while (matcher.find()) {
            String key = "$t" + i;
            String key = "§" + i;
            String value = matcher.group(0);
            if (value != null) {
                this.tagConversion.put(key, value);
@@ -348,7 +344,13 @@ public class Translate {
        try {
            JSONObject translationJson = new JSONObject(response);
            //Retrieves the translated content
            translate.setTranslatedContent(translationJson.getString("translation"));
            String content;
            try {
                content = URLDecoder.decode(translationJson.getString("translation"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                content = translationJson.getString("translation");
            }
            translate.setTranslatedContent(content);
            //Retrieves the initial language
            translate.setInitialLanguage(initialLanguage);
        } catch (JSONException e1) {