11package org .wise .portal .presentation .web .controllers .author .project ;
22
33import java .io .IOException ;
4+
45import org .springframework .beans .factory .annotation .Autowired ;
56import org .springframework .beans .factory .annotation .Value ;
7+ import org .springframework .http .HttpStatus ;
68import org .springframework .security .access .annotation .Secured ;
79import org .springframework .security .core .Authentication ;
810import org .springframework .web .bind .annotation .PathVariable ;
911import org .springframework .web .bind .annotation .PostMapping ;
1012import org .springframework .web .bind .annotation .RequestBody ;
1113import org .springframework .web .bind .annotation .RequestMapping ;
1214import org .springframework .web .bind .annotation .RestController ;
15+ import org .springframework .web .server .ResponseStatusException ;
1316import org .wise .portal .domain .project .impl .ProjectImpl ;
1417import org .wise .portal .domain .user .User ;
1518import 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