Skip to content

Commit

Permalink
Merge pull request #1 from SanzharAkparaliev/translation
Browse files Browse the repository at this point in the history
translation
  • Loading branch information
SanzharAkparaliev authored Jun 21, 2024
2 parents 2e8569b + b1287f3 commit 83fd24e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/axelor/script/module/AxelorScript.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.axelor.script.module;

import com.axelor.app.AxelorModule;
import com.axelor.script.service.DictionaryService;
import com.axelor.script.service.FieldService;
import com.axelor.script.service.PermissionService;
import com.axelor.script.service.impl.DictionaryServiceImpl;
import com.axelor.script.service.impl.FieldServiceImpl;
import com.axelor.script.service.impl.PermissionServiceImpl;

Expand All @@ -11,5 +13,6 @@ public class AxelorScript extends AxelorModule {
protected void configure() {
bind(PermissionService.class).to(PermissionServiceImpl.class);
bind(FieldService.class).to(FieldServiceImpl.class);
bind(DictionaryService.class).to(DictionaryServiceImpl.class);
}
}
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);
}
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 src/main/java/com/axelor/script/web/TranslationController.java
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);
}
}
}
5 changes: 5 additions & 0 deletions src/main/resources/views/AppScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<button name="selectAll" title="Select All" onClick="axelor.script.select.all,save"/>
<button name="importPermissions" onClick="axelor-script-generate-permission,save" title="Generate Permissions"/>
<button name="importFields" onClick="axelor-script-field-permission" title="Generate Fields"/>
<button name="translation" onClick="axelor-script-field-translation" title="Translate"/>
</panel>

</form>
Expand All @@ -32,6 +33,10 @@
<call class="com.axelor.script.web.FieldController" method="generateField"/>
</action-method>

<action-method name="axelor-script-field-translation">
<call class="com.axelor.script.web.TranslationController" method="makeTranslation"/>
</action-method>

<grid name="axelor.script.model.grid" width="large" edit-icon="false" title="Models" editable="true"
model="com.axelor.meta.db.MetaModel">
<field name="name" readonly="true"/>
Expand Down

0 comments on commit 83fd24e

Please sign in to comment.