Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -2,14 +2,14 @@

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.wise.portal.domain.project.impl.ProjectImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.service.project.ProjectService;
Expand All @@ -18,7 +18,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 +39,16 @@ public class TranslateProjectAPIController {
@Autowired
protected TranslateProjectService translateProjectService;

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

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

@Value("${aws.region}")
private Region 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 +57,51 @@ protected void saveTranslations(Authentication auth,
translateProjectService.saveTranslations(project, locale, translations.toString());
}
}

@PostMapping("translationSuggestions")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename endpoint to /api/author/project/translate/suggest

Suggested change
@PostMapping("translationSuggestions")
@PostMapping("suggest")

protected String getSuggestedTranslation(Authentication auth, @RequestBody ObjectNode objectNode) throws IOException, IllegalArgumentException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of ObjectNode, create a class TranslatableText(?)

See how we did CRaterPingRequest here: https://github.com/WISE-Community/WISE-API/blob/translate-with-ai/src/main/java/org/wise/vle/web/CRaterController.java#L60

String srcLang = objectNode.get("srcLang").asText();
String targetLang = objectNode.get("targetLang").asText();
String srcText = objectNode.get("srcText").asText();
String srcLangCode = this.convertLanguageToAWSCode(srcLang);
String targetLangCode = this.convertLanguageToAWSCode(targetLang);
TranslateClient translateClient = buildTranslateClient();
TranslateTextRequest request = buildTranslateTextRequest(srcText, srcLangCode, targetLangCode);
TranslateTextResponse textResponse = translateClient.translateText(request);
return textResponse.translatedText();
}

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

private TranslateTextRequest buildTranslateTextRequest(String srcText, String srcLangCode,
String targetLangCode) {
return TranslateTextRequest.builder()
.text(srcText)
.sourceLanguageCode(srcLangCode)
.targetLanguageCode(targetLangCode)
.build();
}

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");
};
}

}
Loading