Repository: urlaubsverwaltung/urlaubsverwaltung
Pull request #4590 (2024-03-27 12:04:21)

unify locale in locale modal interceptor
refs #4581 


Patch:
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/sicknote/sicknote/SickNoteViewController.java b/src/main/java/org/synyx/urlaubsverwaltung/sicknote/sicknote/SickNoteViewController.java
index c858e9413..551ae88c6 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/sicknote/sicknote/SickNoteViewController.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/sicknote/sicknote/SickNoteViewController.java
@@ -40,7 +40,6 @@ import java.time.Clock;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.util.List;
-import java.util.Locale;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -290,7 +289,7 @@ class SickNoteViewController implements HasLaunchpad {
 
     @PreAuthorize(IS_OFFICE)
     @GetMapping("/sicknote/{id}/convert")
-    public String convertSickNoteToVacation(@PathVariable("id") Long id, Model model, Locale locale)
+    public String convertSickNoteToVacation(@PathVariable("id") Long id, Model model)
         throws UnknownSickNoteException, SickNoteAlreadyInactiveException {
 
         final SickNote sickNote = getSickNote(id);
@@ -301,7 +300,6 @@ class SickNoteViewController implements HasLaunchpad {
         model.addAttribute("sickNote", sickNote);
         model.addAttribute("sickNoteConvertForm", new SickNoteConvertForm(sickNote));
         model.addAttribute("vacationTypes", getActiveVacationTypes());
-        model.addAttribute("currentLocale", locale);
 
         return "sicknote/sick_note_convert";
     }
@@ -310,7 +308,7 @@ class SickNoteViewController implements HasLaunchpad {
     @PostMapping("/sicknote/{id}/convert")
     public String convertSickNoteToVacation(@PathVariable("id") Long id,
                                             @ModelAttribute("sickNoteConvertForm") SickNoteConvertForm sickNoteConvertForm,
-                                            Errors errors, Model model, Locale locale)
+                                            Errors errors, Model model)
         throws UnknownSickNoteException {
 
         final SickNote sickNote = getSickNote(id);
@@ -321,7 +319,6 @@ class SickNoteViewController implements HasLaunchpad {
             model.addAttribute("sickNote", sickNote);
             model.addAttribute("sickNoteConvertForm", sickNoteConvertForm);
             model.addAttribute("vacationTypes", getActiveVacationTypes());
-            model.addAttribute("currentLocale", locale);
 
             return "sicknote/sick_note_convert";
         }
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptor.java b/src/main/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptor.java
index 82126b568..661cb2b73 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptor.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptor.java
@@ -8,13 +8,17 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 
+import java.util.Locale;
+
 @Component
 class LocaleModelInterceptor implements HandlerInterceptor {
 
     @Override
     public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, ModelAndView modelAndView) {
         if (attributeRequired(modelAndView)) {
-            modelAndView.addObject("language", LocaleContextHolder.getLocale().toLanguageTag());
+            final Locale currentLocale = LocaleContextHolder.getLocale();
+            modelAndView.addObject("locale", currentLocale);
+            modelAndView.addObject("language", currentLocale.toLanguageTag());
         }
     }
 
diff --git a/src/main/resources/templates/sicknote/sick_note_convert.html b/src/main/resources/templates/sicknote/sick_note_convert.html
index 45fcc390f..8f392f8b3 100644
--- a/src/main/resources/templates/sicknote/sick_note_convert.html
+++ b/src/main/resources/templates/sicknote/sick_note_convert.html
@@ -46,9 +46,9 @@
                     <th:block th:fragment="sicknote-vacationtype-select-options">
                       <option
                         th:each="vacationType: ${vacationTypes}"
-                        th:text="${vacationType.getLabel(currentLocale)}"
+                        th:text="${vacationType.getLabel(locale)}"
                         th:value="${vacationType.id}"
-                        th:selected="${sickNoteConvertForm.vacationType != null && sickNoteConvertForm.vacationType.id == vacationType.id}"
+                        th:selected="${sickNoteConvertForm.vacationType != null && sickNoteConvertForm.vacationType == vacationType.id}"
                       ></option>
                     </th:block>
                   </select>


Test patch:
diff --git a/src/test/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptorTest.java b/src/test/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptorTest.java
index c9fc8c338..f76696ad2 100644
--- a/src/test/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptorTest.java
+++ b/src/test/java/org/synyx/urlaubsverwaltung/user/LocaleModelInterceptorTest.java
@@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 class LocaleModelInterceptorTest {
 
     @Test
-    void ensureLanguageModelAttribute() throws Exception {
+    void ensureLanguageModelAttribute() {
         LocaleContextHolder.setLocale(GERMAN);
 
         final LocaleModelInterceptor interceptor = new LocaleModelInterceptor();
@@ -25,6 +25,7 @@ class LocaleModelInterceptorTest {
         modelAndView.setViewName("view-name");
         interceptor.postHandle(new MockHttpServletRequest(), new MockHttpServletResponse(), new Object(), modelAndView);
 
+        assertThat(modelAndView.getModelMap().getAttribute("locale")).isEqualTo(GERMAN);
         assertThat(modelAndView.getModelMap().getAttribute("language")).isEqualTo("de");
     }
 
@@ -39,7 +40,7 @@ class LocaleModelInterceptorTest {
     @ParameterizedTest
     @NullSource
     @ValueSource(strings = {"forward::view-name", "redirect::view-name"})
-    void ensureLanguageModelAttributeIsNotSet(String viewName) throws Exception {
+    void ensureLanguageModelAttributeIsNotSet(String viewName) {
         LocaleContextHolder.setLocale(GERMAN);
 
         final LocaleModelInterceptor interceptor = new LocaleModelInterceptor();