22
33import java .io .IOException ;
44import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .beans .factory .annotation .Value ;
56import org .springframework .security .access .annotation .Secured ;
67import org .springframework .security .core .Authentication ;
7- import org .springframework .stereotype .Controller ;
88import org .springframework .web .bind .annotation .PathVariable ;
99import org .springframework .web .bind .annotation .PostMapping ;
1010import org .springframework .web .bind .annotation .RequestBody ;
1111import org .springframework .web .bind .annotation .RequestMapping ;
12- import org .springframework .web .bind .annotation .ResponseBody ;
12+ import org .springframework .web .bind .annotation .RestController ;
1313import org .wise .portal .domain .project .impl .ProjectImpl ;
1414import org .wise .portal .domain .user .User ;
1515import org .wise .portal .service .project .ProjectService ;
1818
1919import com .fasterxml .jackson .databind .node .ObjectNode ;
2020
21- @ Controller
21+ import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
22+ import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
23+ import software .amazon .awssdk .regions .Region ;
24+ import software .amazon .awssdk .services .translate .TranslateClient ;
25+ import software .amazon .awssdk .services .translate .model .TranslateTextRequest ;
26+ import software .amazon .awssdk .services .translate .model .TranslateTextResponse ;
27+
28+ @ RestController
2229@ RequestMapping ("/api/author/project/translate" )
2330@ Secured ({ "ROLE_AUTHOR" })
2431public class TranslateProjectAPIController {
@@ -32,8 +39,16 @@ public class TranslateProjectAPIController {
3239 @ Autowired
3340 protected TranslateProjectService translateProjectService ;
3441
42+ @ Value ("${aws.accessKeyId}" )
43+ private String accessKey ;
44+
45+ @ Value ("${aws.secretAccessKey}" )
46+ private String secretKey ;
47+
48+ @ Value ("${aws.region}" )
49+ private Region region ;
50+
3551 @ PostMapping ("{projectId}/{locale}" )
36- @ ResponseBody
3752 protected void saveTranslations (Authentication auth ,
3853 @ PathVariable ("projectId" ) ProjectImpl project , @ PathVariable ("locale" ) String locale ,
3954 @ RequestBody ObjectNode translations ) throws IOException {
@@ -42,4 +57,51 @@ protected void saveTranslations(Authentication auth,
4257 translateProjectService .saveTranslations (project , locale , translations .toString ());
4358 }
4459 }
60+
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 ();
72+ }
73+
74+ private TranslateClient buildTranslateClient () {
75+ AwsBasicCredentials credentials = AwsBasicCredentials .create (accessKey , secretKey );
76+ return TranslateClient .builder ()
77+ .region (region )
78+ .credentialsProvider (StaticCredentialsProvider .create (credentials ))
79+ .build ();
80+ }
81+
82+ private TranslateTextRequest buildTranslateTextRequest (String srcText , String srcLangCode ,
83+ String targetLangCode ) {
84+ return TranslateTextRequest .builder ()
85+ .text (srcText )
86+ .sourceLanguageCode (srcLangCode )
87+ .targetLanguageCode (targetLangCode )
88+ .build ();
89+ }
90+
91+ private String convertLanguageToAWSCode (String language ) throws IllegalArgumentException {
92+ return switch (language ) {
93+ case "English" -> "en" ;
94+ case "Spanish" -> "es" ;
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+
45107}
0 commit comments