-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from SanzharAkparaliev/translation
translation
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/axelor/script/service/DictionaryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.axelor.script.service; | ||
|
||
public interface DictionaryService { | ||
String getTranslation(String from, String to, String request); | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/axelor/script/service/impl/DictionaryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.axelor.script.service.impl; | ||
|
||
import com.axelor.script.service.DictionaryService; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
|
||
public class DictionaryServiceImpl implements DictionaryService { | ||
|
||
private static final String USER_AGENT = "Mozilla/5.0"; | ||
private static final String TRANSLATE_API_URL = "https://translate.googleapis.com/translate_a/single"; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public String getTranslation(String from, String to, String request) { | ||
try { | ||
String url = buildUrl(from, to, request); | ||
HttpURLConnection connection = setupConnection(url); | ||
|
||
String response = getResponse(connection); | ||
String translatedText = parseResponse(response); | ||
|
||
return translatedText; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
private String buildUrl(String from, String to, String request) throws Exception { | ||
return TRANSLATE_API_URL + "?" + | ||
"client=gtx&" + | ||
"sl=" + from + "&" + | ||
"tl=" + to + "&" + | ||
"dt=t&q=" + URLEncoder.encode(request, "UTF-8"); | ||
} | ||
|
||
private HttpURLConnection setupConnection(String url) throws Exception { | ||
URL obj = new URL(url); | ||
HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); | ||
connection.setRequestProperty("User-Agent", USER_AGENT); | ||
return connection; | ||
} | ||
|
||
private String getResponse(HttpURLConnection connection) throws Exception { | ||
StringBuilder response = new StringBuilder(); | ||
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { | ||
String inputLine; | ||
while ((inputLine = in.readLine()) != null) { | ||
response.append(inputLine); | ||
} | ||
} | ||
return response.toString(); | ||
} | ||
|
||
private String parseResponse(String response) throws Exception { | ||
JsonNode jsonNode = objectMapper.readTree(response); | ||
if (jsonNode.isArray()) { | ||
return jsonNode.get(0).get(0).get(0).asText(); | ||
} else { | ||
return jsonNode.asText(); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/axelor/script/web/TranslationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.axelor.script.web; | ||
|
||
import com.axelor.meta.db.MetaTranslation; | ||
import com.axelor.meta.db.repo.MetaTranslationRepository; | ||
import com.axelor.rpc.ActionRequest; | ||
import com.axelor.rpc.ActionResponse; | ||
import com.axelor.script.service.DictionaryService; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import com.google.inject.persist.Transactional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
@Singleton | ||
public class TranslationController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(TranslationController.class); | ||
|
||
private final DictionaryService dictionaryService; | ||
private final MetaTranslationRepository metaTranslationRepository; | ||
|
||
@Inject | ||
public TranslationController(DictionaryService dictionaryService, MetaTranslationRepository metaTranslationRepository) { | ||
this.dictionaryService = dictionaryService; | ||
this.metaTranslationRepository = metaTranslationRepository; | ||
} | ||
|
||
@Transactional | ||
public void makeTranslation(ActionRequest request, ActionResponse response) { | ||
List<MetaTranslation> metaTranslations = fetchUntranslatedMetaTranslations(); | ||
if (metaTranslations == null || metaTranslations.isEmpty()) { | ||
logger.info("No untranslated MetaTranslations found."); | ||
return; | ||
} | ||
for (MetaTranslation metaTranslation : metaTranslations) { | ||
translateAndSaveMetaTranslation(metaTranslation); | ||
} | ||
} | ||
|
||
private List<MetaTranslation> fetchUntranslatedMetaTranslations() { | ||
return metaTranslationRepository.all() | ||
.filter("self.message IS NULL OR self.message = ''") | ||
.fetch(); | ||
} | ||
|
||
private void translateAndSaveMetaTranslation(MetaTranslation metaTranslation) { | ||
try { | ||
String translatedText = dictionaryService.getTranslation("en", metaTranslation.getLanguage(), metaTranslation.getKey()); | ||
metaTranslation.setMessage(translatedText); | ||
metaTranslationRepository.save(metaTranslation); | ||
logger.info("Translated and saved MetaTranslation: {}", metaTranslation); | ||
} catch (Exception e) { | ||
logger.error("Error translating MetaTranslation with key: " + metaTranslation.getKey(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters