mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-15 09:17:42 +00:00
5106: Error message when we can't connect to Google.
This commit is contained in:
parent
b16b27f23b
commit
ab242a405e
@ -28,6 +28,8 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import org.openide.util.NbBundle.Messages;
|
import org.openide.util.NbBundle.Messages;
|
||||||
@ -56,9 +58,26 @@ public final class GoogleTranslator implements TextTranslator {
|
|||||||
settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode());
|
settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode());
|
||||||
loadTranslator();
|
loadTranslator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean googleIsReachable() {
|
||||||
|
String host = "www.google.com";
|
||||||
|
InetAddress address;
|
||||||
|
try {
|
||||||
|
address = InetAddress.getByName(host);
|
||||||
|
return address.isReachable(1500);
|
||||||
|
}catch (UnknownHostException ex) {
|
||||||
|
return false;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String translate(String string) throws TranslationException {
|
public String translate(String string) throws TranslationException {
|
||||||
|
if (!googleIsReachable()) {
|
||||||
|
throw new TranslationException("Failure translating using GoogleTranslator: Cannot connect to Google");
|
||||||
|
}
|
||||||
|
|
||||||
if (googleTranslate != null) {
|
if (googleTranslate != null) {
|
||||||
try {
|
try {
|
||||||
// Translates some text into English, without specifying the source language.
|
// Translates some text into English, without specifying the source language.
|
||||||
@ -66,10 +85,10 @@ public final class GoogleTranslator implements TextTranslator {
|
|||||||
// HTML files were producing lots of white space at the end
|
// HTML files were producing lots of white space at the end
|
||||||
String substring = string.trim();
|
String substring = string.trim();
|
||||||
|
|
||||||
// WE can't currently set parameters, so we are using the default behavior of
|
// We can't currently set parameters, so we are using the default behavior of
|
||||||
// asuming the input is HTML. We need to replace newlines with <br> for Google to preserve them
|
// assuming the input is HTML. We need to replace newlines with <br> for Google to preserve them
|
||||||
substring = substring.replaceAll("(\r\n|\n)", "<br />");
|
substring = substring.replaceAll("(\r\n|\n)", "<br />");
|
||||||
|
|
||||||
// The API complains if the "Payload" is over 204800 bytes. I'm assuming that
|
// The API complains if the "Payload" is over 204800 bytes. I'm assuming that
|
||||||
// deals with the full request. At some point, we get different errors about too
|
// deals with the full request. At some point, we get different errors about too
|
||||||
// much text. Officially, Google says they will googleTranslate only 5k chars,
|
// much text. Officially, Google says they will googleTranslate only 5k chars,
|
||||||
@ -81,7 +100,7 @@ public final class GoogleTranslator implements TextTranslator {
|
|||||||
Translation translation
|
Translation translation
|
||||||
= googleTranslate.translate(substring);
|
= googleTranslate.translate(substring);
|
||||||
String translatedString = translation.getTranslatedText();
|
String translatedString = translation.getTranslatedText();
|
||||||
|
|
||||||
// put back the newlines
|
// put back the newlines
|
||||||
translatedString = translatedString.replaceAll("<br />", "\n");
|
translatedString = translatedString.replaceAll("<br />", "\n");
|
||||||
return translatedString;
|
return translatedString;
|
||||||
@ -93,7 +112,7 @@ public final class GoogleTranslator implements TextTranslator {
|
|||||||
throw new TranslationException("Google Translator has not been configured, credentials need to be specified");
|
throw new TranslationException("Google Translator has not been configured, credentials need to be specified");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Messages({"GoogleTranslator.name.text=Google Translate"})
|
@Messages({"GoogleTranslator.name.text=Google Translate"})
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user