Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@
<artifactId>lzstring4java</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>translate</artifactId>
<version>2.40.15</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.wise.portal.presentation.web.controllers.author.project;

import lombok.Getter;

@Getter
public class TranslatableText {
private String srcLangCode;
private String targetLangCode;
private String srcText;

public TranslatableText(String srcLang, String targetLang, String srcText) {
this.srcLangCode = this.convertLanguageToAWSCode(srcLang);
this.targetLangCode = this.convertLanguageToAWSCode(targetLang);
this.srcText = srcText;
}

private String convertLanguageToAWSCode(String language) throws IllegalArgumentException {
return switch (language) {
case "English" -> "en";
case "Spanish" -> "es-MX";
case "Italian" -> "it";
case "Japanese" -> "ja";
case "German" -> "de";
case "Chinese (Simplified)" -> "zh";
case "Chinese (Traditional)" -> "zh-TW";
case "Dutch" -> "nl";
case "Korean" -> "ko";
case "Vietnamese" -> "vi";
default -> throw new IllegalArgumentException("Invalid language provided");
};
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.wise.portal.presentation.web.controllers.author.project;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.wise.portal.domain.project.impl.ProjectImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.service.project.ProjectService;
Expand All @@ -18,7 +21,14 @@

import com.fasterxml.jackson.databind.node.ObjectNode;

@Controller
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.translate.TranslateClient;
import software.amazon.awssdk.services.translate.model.TranslateTextRequest;
import software.amazon.awssdk.services.translate.model.TranslateTextResponse;

@RestController
@RequestMapping("/api/author/project/translate")
@Secured({ "ROLE_AUTHOR" })
public class TranslateProjectAPIController {
Expand All @@ -32,8 +42,16 @@ public class TranslateProjectAPIController {
@Autowired
protected TranslateProjectService translateProjectService;

@Value("${aws.accessKeyId:}")
private String accessKey;

@Value("${aws.secretAccessKey:}")
private String secretKey;

@Value("${aws.region:}")
private String region;

@PostMapping("{projectId}/{locale}")
@ResponseBody
protected void saveTranslations(Authentication auth,
@PathVariable("projectId") ProjectImpl project, @PathVariable("locale") String locale,
@RequestBody ObjectNode translations) throws IOException {
Expand All @@ -42,4 +60,35 @@ protected void saveTranslations(Authentication auth,
translateProjectService.saveTranslations(project, locale, translations.toString());
}
}

@PostMapping("suggest")
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText) throws IOException, IllegalArgumentException {
if (accessKey.equals("") || secretKey.equals("") || region.equals("")) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
"Missing application properties necessary for AWS Translate"
);
} else {
TranslateClient translateClient = buildTranslateClient();
TranslateTextRequest request = buildTranslateTextRequest(translatableText);
TranslateTextResponse textResponse = translateClient.translateText(request);
return textResponse.translatedText();
}
}

private TranslateClient buildTranslateClient() {
AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
return TranslateClient.builder()
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.build();
}

private TranslateTextRequest buildTranslateTextRequest(TranslatableText translatableText) {
return TranslateTextRequest.builder()
.text(translatableText.getSrcText())
.sourceLanguageCode(translatableText.getSrcLangCode())
.targetLanguageCode(translatableText.getTargetLangCode())
.build();
}
}
7 changes: 7 additions & 0 deletions src/main/resources/application-dockerdev-sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ spring.servlet.multipart.max-request-size=100MB
# henry_verification_url - [url or leave empty] henry verification url
# henry_scoring_url - [url or leave empty] henry scoring url
# henry_client_id - [id or leave empty] henry client token id
# aws.accessKeyId - [key or leave empty] AWS Translate public key
# aws.secretAccessKey - [key or leave empty] AWS Translate secret key
# aws.region - [region or leave empty] AWS Translate server region

wise.name=My Local WISE Development Instance
wise.hostname=http://localhost:81
Expand Down Expand Up @@ -73,6 +76,10 @@ berkeley_cRater_scoring_url=
berkeley_cRater_client_id=
berkeley_cRater_password=

aws.accessKeyId=
aws.secretAccessKey=
aws.region=

######### database properties #########

# Modify below as needed.
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/application_sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ spring.servlet.multipart.max-request-size=100MB
# henry_verification_url - [url or leave empty] henry verification url
# henry_scoring_url - [url or leave empty] henry scoring url
# henry_client_id - [id or leave empty] henry client token id
# aws.accessKeyId - [key or leave empty] AWS Translate public key
# aws.secretAccessKey - [key or leave empty] AWS Translate secret key
# aws.region - [region or leave empty] AWS Translate server region

wise.name=My Local WISE Development Instance
wise.hostname=http://localhost:8080
Expand Down Expand Up @@ -73,6 +76,10 @@ berkeley_cRater_scoring_url=
berkeley_cRater_client_id=
berkeley_cRater_password=

aws.accessKeyId=
aws.secretAccessKey=
aws.region=

######### database properties #########

# Modify below as needed.
Expand Down
Loading