|
| 1 | +package com.baeldung.google.cloud.translator; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import com.google.auth.oauth2.GoogleCredentials; |
| 12 | +import com.google.cloud.translate.Language; |
| 13 | +import com.google.cloud.translate.Translate; |
| 14 | +import com.google.cloud.translate.TranslateOptions; |
| 15 | +import com.google.cloud.translate.Translation; |
| 16 | +import com.google.cloud.translate.v3.GlossaryName; |
| 17 | +import com.google.cloud.translate.v3.LocationName; |
| 18 | +import com.google.cloud.translate.v3.TranslateTextGlossaryConfig; |
| 19 | +import com.google.cloud.translate.v3.TranslateTextRequest; |
| 20 | +import com.google.cloud.translate.v3.TranslateTextResponse; |
| 21 | +import com.google.cloud.translate.v3.TranslationServiceClient; |
| 22 | + |
| 23 | +public class Translator { |
| 24 | + |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(Translator.class); |
| 26 | + |
| 27 | + private static Translate translate; |
| 28 | + |
| 29 | + static { |
| 30 | + initializeTranslateClient(); |
| 31 | + } |
| 32 | + |
| 33 | + public static void initializeTranslateClient() { |
| 34 | + if (translate == null) { |
| 35 | + try { |
| 36 | + GoogleCredentials credentials = GoogleCredentials.fromStream( |
| 37 | + new FileInputStream("src/main/resources/translator_permission.json") |
| 38 | + ); |
| 39 | + translate = TranslateOptions.newBuilder() |
| 40 | + .setCredentials(credentials) |
| 41 | + .build() |
| 42 | + .getService(); |
| 43 | + logger.info("Google Translate client initialized."); |
| 44 | + } catch (Exception e) { |
| 45 | + logger.error("Failed to initialize Google Translate client.", e); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void listSupportedLanguages() { |
| 51 | + try { |
| 52 | + List<Language> languages = translate.listSupportedLanguages(); |
| 53 | + for (Language language : languages) { |
| 54 | + logger.info(String.format("Name: %s, Code: %s", language.getName(), language.getCode())); |
| 55 | + } |
| 56 | + } catch (Exception e) { |
| 57 | + // handle exception |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static void listSupportedLanguagesWithSpecificLanguage() { |
| 62 | + try { |
| 63 | + List<Language> languages = translate.listSupportedLanguages(Translate.LanguageListOption.targetLanguage("es")); |
| 64 | + for (Language language : languages) { |
| 65 | + logger.info(String.format("Name: %s, Code: %s", language.getName(), language.getCode())); |
| 66 | + } |
| 67 | + } catch (Exception e) { |
| 68 | + // handle exception |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static String translateText(String text, String targetLanguage) { |
| 73 | + String s = ""; |
| 74 | + try { |
| 75 | + Translation translation = translate.translate(text, Translate.TranslateOption.targetLanguage(targetLanguage)); |
| 76 | + s = translation.getTranslatedText(); |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + // handle exception |
| 80 | + } |
| 81 | + return s; |
| 82 | + } |
| 83 | + |
| 84 | + public static String detectLanguage(String text) { |
| 85 | + return translate.detect(text) |
| 86 | + .getLanguage(); |
| 87 | + } |
| 88 | + |
| 89 | + public static List<String> translateBatch(List<String> texts, String targetLanguage) { |
| 90 | + List<String> translationList = null; |
| 91 | + try { |
| 92 | + List<Translation> translations = translate.translate(texts, Translate.TranslateOption.targetLanguage(targetLanguage)); |
| 93 | + translationList = translations.stream() |
| 94 | + .map(Translation::getTranslatedText) |
| 95 | + .collect(Collectors.toList()); |
| 96 | + } catch (Exception e) { |
| 97 | + // handle exception |
| 98 | + } |
| 99 | + return translationList; |
| 100 | + } |
| 101 | + |
| 102 | + public static String translateWithGlossary(String projectId, String location, String text, String targetLanguage, String glossaryId) { |
| 103 | + String translatedText = ""; |
| 104 | + |
| 105 | + try (TranslationServiceClient client = TranslationServiceClient.create()) { |
| 106 | + LocationName parent = LocationName.of(projectId, location); |
| 107 | + GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId); |
| 108 | + |
| 109 | + TranslateTextRequest request = TranslateTextRequest.newBuilder() |
| 110 | + .setParent(parent.toString()) |
| 111 | + .setTargetLanguageCode(targetLanguage) |
| 112 | + .addContents(text) |
| 113 | + .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder() |
| 114 | + .setGlossary(glossaryName.toString()).build()) // Attach glossary |
| 115 | + .build(); |
| 116 | + |
| 117 | + TranslateTextResponse response = client.translateText(request); |
| 118 | + translatedText = response.getTranslations(0).getTranslatedText(); |
| 119 | + } catch (IOException e) { |
| 120 | + // handle exception |
| 121 | + } |
| 122 | + return translatedText; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments