1515 */
1616package de .openknowledge .sample .address .application ;
1717
18+ import static jakarta .ws .rs .core .HttpHeaders .ACCEPT_LANGUAGE ;
19+ import static java .util .Locale .GERMANY ;
20+ import static java .util .Optional .ofNullable ;
1821import static java .util .stream .Collectors .joining ;
1922
2023import java .net .URI ;
21- import java .net .URISyntaxException ;
2224import java .util .List ;
25+ import java .util .Locale ;
2326import java .util .logging .Logger ;
2427
2528import jakarta .enterprise .context .ApplicationScoped ;
2629import jakarta .inject .Inject ;
2730import jakarta .ws .rs .Consumes ;
2831import jakarta .ws .rs .GET ;
32+ import jakarta .ws .rs .HeaderParam ;
2933import jakarta .ws .rs .POST ;
3034import jakarta .ws .rs .Path ;
3135import jakarta .ws .rs .Produces ;
@@ -64,10 +68,9 @@ public Response healthCheck() {
6468 @ POST
6569 @ Path ("/" )
6670 @ Consumes (MediaType .APPLICATION_JSON )
67- public Response validateAddress (Address address , @ Context UriInfo uri ) throws URISyntaxException {
71+ public Response validateAddress (Address address , @ Context UriInfo uri , @ HeaderParam ( ACCEPT_LANGUAGE ) Locale locale ) {
6872 LOGGER .info ("RESTful call 'POST valid address'" );
6973 if (addressesRepository .isValid (address )) {
70- LOGGER .fine ("address is valid" );
7174 return Response .ok ().build ();
7275 } else {
7376 URI type = uri .getRequestUri ().resolve ("/errors/invalid-city" );
@@ -76,24 +79,49 @@ public Response validateAddress(Address address, @Context UriInfo uri) throws UR
7679 LOGGER .fine (suggestions .size () + " suggestions found: " + suggestions );
7780 if (suggestions .size () == 1 ) {
7881 return Response .status (Response .Status .BAD_REQUEST ).type (PROBLEM_JSON_TYPE )
79- .entity (String .format (PROBLEM_JSON , type , "invalid city" ,
82+ .entity (String .format (PROBLEM_JSON , type ,
83+ translate ("invalid city" , locale ),
8084 Response .Status .BAD_REQUEST .getStatusCode (),
81- "Did you mean " + suggestions .iterator ().next () + "?" , instance ))
85+ translate ("Did you mean %s?" , locale , suggestions .iterator ().next ()),
86+ instance ))
8287 .build ();
8388 } else if (!suggestions .isEmpty ()) {
89+ String suggestionList = suggestions .stream ().map (Object ::toString ).collect (joining (", " ));
8490 return Response .status (Response .Status .BAD_REQUEST ).type (PROBLEM_JSON_TYPE )
85- .entity (String .format (PROBLEM_JSON , type , "invalid city" ,
91+ .entity (String .format (PROBLEM_JSON , type ,
92+ translate ("invalid city" , locale ),
8693 Response .Status .BAD_REQUEST .getStatusCode (),
87- "Did you mean one of "
88- + suggestions .stream ().map (Object ::toString ).collect (joining (", " )) + "?" ,
94+ translate ("Did you mean one of %s?" , locale , suggestionList ),
8995 instance ))
9096 .build ();
9197 } else {
9298 return Response .status (Response .Status .BAD_REQUEST ).type (PROBLEM_JSON_TYPE )
93- .entity (String .format (PROBLEM_JSON , type , "invalid city" ,
94- Response .Status .BAD_REQUEST .getStatusCode (), "no matching city found" , instance ))
99+ .entity (String .format (PROBLEM_JSON , type ,
100+ translate ("invalid city" , locale ),
101+ Response .Status .BAD_REQUEST .getStatusCode (),
102+ translate ("no matching city found" ,
103+ locale ),
104+ instance ))
95105 .build ();
96106 }
97107 }
98108 }
109+
110+ private String translate (String template , Locale language , Object ... parameters ) {
111+ if (ofNullable (language ).map (Locale ::getLanguage ).filter (GERMANY .getLanguage ()::equals ).isPresent ()) {
112+ switch (template ) {
113+ case "Did you mean %s?" :
114+ return "Meinten Sie %s?" .formatted (parameters );
115+ case "invalid city" :
116+ return "ungültige Stadt" ;
117+ case "no matching city found" :
118+ return "keine passende Stadt gefunden" ;
119+ case "Did you mean one of %s?" :
120+ return "Meinten Sie eine von %s?" .formatted (parameters );
121+ default :
122+ return template .formatted (parameters );
123+ }
124+ }
125+ return template .formatted (parameters );
126+ }
99127}
0 commit comments