19 package org.sleuthkit.autopsy.texttranslation.translators;
21 import com.google.auth.Credentials;
22 import com.google.auth.oauth2.ServiceAccountCredentials;
23 import com.google.cloud.translate.Translate;
24 import com.google.cloud.translate.TranslateOptions;
25 import com.google.cloud.translate.Translation;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.InetAddress;
31 import java.net.UnknownHostException;
32 import java.util.logging.Level;
33 import javax.swing.JPanel;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openide.util.NbBundle.Messages;
37 import org.openide.util.lookup.ServiceProvider;
47 @ServiceProvider(service = TextTranslator.class)
52 private static final int MAX_PAYLOAD_SIZE = 5000;
72 String host =
"www.google.com";
75 address = InetAddress.getByName(host);
76 return address.isReachable(1500);
77 }
catch (UnknownHostException ex) {
78 logger.log(Level.WARNING,
"Unable to reach google.com due to unknown host", ex);
80 }
catch (IOException ex) {
81 logger.log(Level.WARNING,
"Unable to reach google.com due IOException", ex);
88 if (!googleIsReachable()) {
89 throw new TranslationException(
"Failure translating using GoogleTranslator: Cannot connect to Google");
92 if (googleTranslate != null) {
95 String substring =
string.trim();
99 substring = substring.replaceAll(
"(\r\n|\n)",
"<br />");
107 if (substring.length() > MAX_PAYLOAD_SIZE) {
108 substring = substring.substring(0, MAX_PAYLOAD_SIZE);
110 Translation translation
111 = googleTranslate.translate(substring);
112 String translatedString = translation.getTranslatedText();
115 translatedString = translatedString.replaceAll(
"<br />",
"\n");
120 return translatedString;
121 }
catch (Throwable ex) {
126 throw new TranslationException(
"Google Translator has not been configured, credentials need to be specified");
130 @Messages({
"GoogleTranslator.name.text=Google Translate"})
133 return Bundle.GoogleTranslator_name_text();
138 return settingsPanel;
146 InputStream credentialStream = null;
147 Credentials creds = null;
148 if (StringUtils.isBlank(settings.getCredentialPath())) {
149 logger.log(Level.INFO,
"No credentials file has been provided for Google Translator");
152 credentialStream =
new FileInputStream(settings.getCredentialPath());
153 }
catch (FileNotFoundException ex) {
154 logger.log(Level.WARNING,
"JSON file for GoogleTranslator credentials not found", ex);
157 if (credentialStream != null) {
159 creds = ServiceAccountCredentials.fromStream(credentialStream);
160 }
catch (IOException ex) {
161 logger.log(Level.WARNING,
"Error converting JSON file to Credentials object for GoogleTranslator", ex);
165 logger.log(Level.WARNING,
"Credentials were not successfully made, no translations will be available from the GoogleTranslator");
166 googleTranslate = null;
168 TranslateOptions.Builder builder = TranslateOptions.newBuilder();
169 builder.setCredentials(creds);
170 builder.setTargetLanguage(settings.getTargetLanguageCode());
171 googleTranslate = builder.build().getService();
177 settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
178 settings.setCredentialPath(settingsPanel.getCredentialsPath());
179 settings.saveSettings();
185 return MAX_PAYLOAD_SIZE;
static String unEscapeHtml(String toUnescape)
String translate(String string)
static boolean googleIsReachable()
JPanel getSettingsPanel()
Translate googleTranslate
synchronized static Logger getLogger(String name)
final GoogleTranslatorSettingsPanel settingsPanel