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;
48 @ServiceProvider(service = TextTranslator.class)
53 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 public JPanel getSettingsPanel() {
139 if(settingsPanel == null) {
142 return settingsPanel;
150 InputStream credentialStream = null;
151 Credentials creds = null;
152 if (StringUtils.isBlank(settings.getCredentialPath())) {
153 logger.log(Level.INFO,
"No credentials file has been provided for Google Translator");
156 credentialStream =
new FileInputStream(settings.getCredentialPath());
157 }
catch (FileNotFoundException ex) {
158 logger.log(Level.WARNING,
"JSON file for GoogleTranslator credentials not found", ex);
161 if (credentialStream != null) {
163 creds = ServiceAccountCredentials.fromStream(credentialStream);
164 }
catch (IOException ex) {
165 logger.log(Level.WARNING,
"Error converting JSON file to Credentials object for GoogleTranslator", ex);
169 logger.log(Level.WARNING,
"Credentials were not successfully made, no translations will be available from the GoogleTranslator");
170 googleTranslate = null;
172 TranslateOptions.Builder builder = TranslateOptions.newBuilder();
173 builder.setCredentials(creds);
174 builder.setTargetLanguage(settings.getTargetLanguageCode());
175 googleTranslate = builder.build().getService();
181 settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
182 settings.setCredentialPath(settingsPanel.getCredentialsPath());
183 settings.saveSettings();
189 return MAX_PAYLOAD_SIZE;
static String unEscapeHtml(String toUnescape)
String translate(String string)
static boolean googleIsReachable()
Translate googleTranslate
GoogleTranslatorSettingsPanel settingsPanel
synchronized static Logger getLogger(String name)