5106: Error message when we can't connect to Google.

This commit is contained in:
Brian Kjersten 2019-05-31 17:40:53 -04:00
parent b16b27f23b
commit ab242a405e

View File

@ -28,6 +28,8 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.util.NbBundle.Messages;
@ -56,9 +58,26 @@ public final class GoogleTranslator implements TextTranslator {
settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode());
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
public String translate(String string) throws TranslationException {
if (!googleIsReachable()) {
throw new TranslationException("Failure translating using GoogleTranslator: Cannot connect to Google");
}
if (googleTranslate != null) {
try {
// 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
String substring = string.trim();
// 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
// We can't currently set parameters, so we are using the default behavior of
// assuming the input is HTML. We need to replace newlines with <br> for Google to preserve them
substring = substring.replaceAll("(\r\n|\n)", "<br />");
// 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
// much text. Officially, Google says they will googleTranslate only 5k chars,
@ -81,7 +100,7 @@ public final class GoogleTranslator implements TextTranslator {
Translation translation
= googleTranslate.translate(substring);
String translatedString = translation.getTranslatedText();
// put back the newlines
translatedString = translatedString.replaceAll("<br />", "\n");
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");
}
}
@Messages({"GoogleTranslator.name.text=Google Translate"})
@Override
public String getName() {