Skip to content

Commit a262a33

Browse files
committed
Handle missing app props, created TranslatableText class, and other suggested changes
1 parent 28def20 commit a262a33

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wise.portal.presentation.web.controllers.author.project;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class TranslatableText {
7+
private String srcLangCode;
8+
private String targetLangCode;
9+
private String srcText;
10+
11+
public TranslatableText(String srcLang, String targetLang, String srcText) {
12+
this.srcLangCode = this.convertLanguageToAWSCode(srcLang);
13+
this.targetLangCode = this.convertLanguageToAWSCode(targetLang);
14+
this.srcText = srcText;
15+
}
16+
17+
private String convertLanguageToAWSCode(String language) throws IllegalArgumentException {
18+
return switch (language) {
19+
case "English" -> "en";
20+
case "Spanish" -> "es-MX";
21+
case "Italian" -> "it";
22+
case "Japanese" -> "ja";
23+
case "German" -> "de";
24+
case "Chinese (Simplified)" -> "zh";
25+
case "Chinese (Traditional)" -> "zh-TW";
26+
case "Dutch" -> "nl";
27+
case "Korean" -> "ko";
28+
case "Vietnamese" -> "vi";
29+
default -> throw new IllegalArgumentException("Invalid language provided");
30+
};
31+
}
32+
}
Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package org.wise.portal.presentation.web.controllers.author.project;
22

33
import java.io.IOException;
4+
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.http.HttpStatus;
68
import org.springframework.security.access.annotation.Secured;
79
import org.springframework.security.core.Authentication;
810
import org.springframework.web.bind.annotation.PathVariable;
911
import org.springframework.web.bind.annotation.PostMapping;
1012
import org.springframework.web.bind.annotation.RequestBody;
1113
import org.springframework.web.bind.annotation.RequestMapping;
1214
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.server.ResponseStatusException;
1316
import org.wise.portal.domain.project.impl.ProjectImpl;
1417
import org.wise.portal.domain.user.User;
1518
import org.wise.portal.service.project.ProjectService;
@@ -39,14 +42,14 @@ public class TranslateProjectAPIController {
3942
@Autowired
4043
protected TranslateProjectService translateProjectService;
4144

42-
@Value("${aws.accessKeyId}")
45+
@Value("${aws.accessKeyId:}")
4346
private String accessKey;
4447

45-
@Value("${aws.secretAccessKey}")
48+
@Value("${aws.secretAccessKey:}")
4649
private String secretKey;
4750

48-
@Value("${aws.region}")
49-
private Region region;
51+
@Value("${aws.region:}")
52+
private String region;
5053

5154
@PostMapping("{projectId}/{locale}")
5255
protected void saveTranslations(Authentication auth,
@@ -58,50 +61,34 @@ protected void saveTranslations(Authentication auth,
5861
}
5962
}
6063

61-
@PostMapping("translationSuggestions")
62-
protected String getSuggestedTranslation(Authentication auth, @RequestBody ObjectNode objectNode) throws IOException, IllegalArgumentException {
63-
String srcLang = objectNode.get("srcLang").asText();
64-
String targetLang = objectNode.get("targetLang").asText();
65-
String srcText = objectNode.get("srcText").asText();
66-
String srcLangCode = this.convertLanguageToAWSCode(srcLang);
67-
String targetLangCode = this.convertLanguageToAWSCode(targetLang);
68-
TranslateClient translateClient = buildTranslateClient();
69-
TranslateTextRequest request = buildTranslateTextRequest(srcText, srcLangCode, targetLangCode);
70-
TranslateTextResponse textResponse = translateClient.translateText(request);
71-
return textResponse.translatedText();
64+
@PostMapping("suggest")
65+
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText) throws IOException, IllegalArgumentException {
66+
if (accessKey.equals("") || secretKey.equals("") || region.equals("")) {
67+
throw new ResponseStatusException(
68+
HttpStatus.INTERNAL_SERVER_ERROR,
69+
"Missing application properties necessary for AWS Translate"
70+
);
71+
} else {
72+
TranslateClient translateClient = buildTranslateClient();
73+
TranslateTextRequest request = buildTranslateTextRequest(translatableText);
74+
TranslateTextResponse textResponse = translateClient.translateText(request);
75+
return textResponse.translatedText();
76+
}
7277
}
7378

7479
private TranslateClient buildTranslateClient() {
7580
AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
7681
return TranslateClient.builder()
77-
.region(region)
82+
.region(Region.of(region))
7883
.credentialsProvider(StaticCredentialsProvider.create(credentials))
7984
.build();
8085
}
8186

82-
private TranslateTextRequest buildTranslateTextRequest(String srcText, String srcLangCode,
83-
String targetLangCode) {
87+
private TranslateTextRequest buildTranslateTextRequest(TranslatableText translatableText) {
8488
return TranslateTextRequest.builder()
85-
.text(srcText)
86-
.sourceLanguageCode(srcLangCode)
87-
.targetLanguageCode(targetLangCode)
89+
.text(translatableText.getSrcText())
90+
.sourceLanguageCode(translatableText.getSrcLangCode())
91+
.targetLanguageCode(translatableText.getTargetLangCode())
8892
.build();
8993
}
90-
91-
private String convertLanguageToAWSCode(String language) throws IllegalArgumentException {
92-
return switch (language) {
93-
case "English" -> "en";
94-
case "Spanish" -> "es-MX";
95-
case "Italian" -> "it";
96-
case "Japanese" -> "ja";
97-
case "German" -> "de";
98-
case "Chinese (Simplified)" -> "zh";
99-
case "Chinese (Traditional)" -> "zh-TW";
100-
case "Dutch" -> "nl";
101-
case "Korean" -> "ko";
102-
case "Vietnamese" -> "vi";
103-
default -> throw new IllegalArgumentException("Invalid language provided");
104-
};
105-
}
106-
10794
}

0 commit comments

Comments
 (0)