1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.impl.action.application.translation;
20
21 import java.io.IOException;
22 import java.security.GeneralSecurityException;
23 import java.util.Arrays;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import org.springframework.transaction.annotation.Propagation;
31 import org.springframework.transaction.annotation.Transactional;
32
33 import com.google.api.client.json.jackson2.JacksonFactory;
34 import com.google.api.services.translate.Translate;
35 import com.google.api.services.translate.model.TranslationsListResponse;
36 import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
37 import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
38 import com.hack23.cia.service.data.api.ApplicationConfigurationService;
39
40
41
42
43 @Service
44 @Transactional(propagation = Propagation.REQUIRED)
45 final class TranslationServiceImpl implements TranslationService {
46
47
48 private static final Logger LOGGER = LoggerFactory.getLogger(TranslationServiceImpl.class);
49
50
51 @Autowired
52 private ApplicationConfigurationService applicationConfigurationService;
53
54
55
56
57 public TranslationServiceImpl() {
58 super();
59 }
60
61 @Override
62 public String translate(final String translateText, final String targetLanguage) throws TranslationException {
63
64 final ApplicationConfiguration googleTranslateApiKey = applicationConfigurationService.checkValueOrLoadDefault(
65 "Google translate api accesskey", "Required for use of the translation service",
66 ConfigurationGroup.AUTHENTICATION, TranslationServiceImpl.class.getSimpleName(), "Translation Service",
67 "Responsible configure access credentials for google translate api",
68 "external.services.google.translate.api.accesskey", "");
69 final ApplicationConfiguration googleTranslateApiApplicationName = applicationConfigurationService
70 .checkValueOrLoadDefault("Google translate api application name",
71 "Required for use of the translation service", ConfigurationGroup.AUTHENTICATION,
72 TranslationServiceImpl.class.getSimpleName(), "Translation Service",
73 "Responsible configure application name for google translate api",
74 "external.services.google.translate.api.application.name", "");
75
76 if (!StringUtils.isBlank(googleTranslateApiKey.getPropertyValue())
77 && !StringUtils.isBlank(googleTranslateApiApplicationName.getPropertyValue())) {
78
79 try {
80 final Translate t = new Translate.Builder(
81 com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
82 JacksonFactory.getDefaultInstance(), null)
83 .setApplicationName(googleTranslateApiApplicationName.getPropertyValue()).build();
84
85 final Translate.Translations.List list = t.new Translations().list(Arrays.asList(translateText),
86 targetLanguage);
87 list.setKey(googleTranslateApiKey.getPropertyValue());
88 list.setSource("EN");
89 final TranslationsListResponse response = list.execute();
90
91 return response.getTranslations().listIterator().next().getTranslatedText();
92
93 } catch (GeneralSecurityException | IOException e) {
94 final StringBuilder stringBuilder = new StringBuilder().append("Problem translation text:")
95 .append(translateText).append("to language:").append(targetLanguage);
96 LOGGER.warn(stringBuilder.toString(), e);
97 throw new TranslationException(stringBuilder.toString(), e);
98 }
99 }
100 throw new TranslationException("Missing google api key or application name", null);
101 }
102 }