From be8afec69b3421eb7ec9ed87ebaef1e7cbfa4116 Mon Sep 17 00:00:00 2001 From: anizmo Date: Tue, 15 Apr 2025 22:51:56 -0400 Subject: [PATCH] Adding strings for all other languages - Added WebConfiguration class to change language using url param - Added text from messages to html files wherever required - Added String to all other languages Closes #1854 Signed-off-by: anizmo --- .../samples/petclinic/owner/Owner.java | 2 +- .../petclinic/system/WebConfiguration.java | 60 +++++++++++++++++++ .../resources/messages/messages.properties | 25 ++++++++ .../resources/messages/messages_de.properties | 26 +++++++- .../resources/messages/messages_es.properties | 26 +++++++- .../resources/messages/messages_fa.properties | 26 +++++++- .../resources/messages/messages_ko.properties | 25 ++++++++ .../resources/messages/messages_pt.properties | 25 ++++++++ .../resources/messages/messages_ru.properties | 26 +++++++- .../resources/messages/messages_tr.properties | 26 +++++++- src/main/resources/templates/error.html | 4 +- .../resources/templates/fragments/layout.html | 16 ++--- .../owners/createOrUpdateOwnerForm.html | 14 ++--- .../templates/owners/findOwners.html | 9 ++- .../templates/owners/ownersList.html | 14 ++--- .../resources/templates/vets/vetList.html | 26 ++++---- 16 files changed, 302 insertions(+), 48 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/system/WebConfiguration.java diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index 675b2140e..c5ae067dc 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -57,7 +57,7 @@ public class Owner extends Person { @Column(name = "telephone") @NotBlank - @Pattern(regexp = "\\d{10}", message = "Telephone must be a 10-digit number") + @Pattern(regexp = "\\d{10}", message = "{telephone.invalid}") private String telephone; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) diff --git a/src/main/java/org/springframework/samples/petclinic/system/WebConfiguration.java b/src/main/java/org/springframework/samples/petclinic/system/WebConfiguration.java new file mode 100644 index 000000000..1ef32e4dc --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/system/WebConfiguration.java @@ -0,0 +1,60 @@ +package org.springframework.samples.petclinic.system; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; + +import java.util.Locale; + +/** + * Configures internationalization (i18n) support for the application. + * + *

+ * Handles loading language-specific messages, tracking the user's language, and allowing + * language changes via the URL parameter (e.g., ?lang=de). + *

+ * + * @author Anuj Ashok Potdar + */ +@Configuration +@SuppressWarnings("unused") +public class WebConfiguration implements WebMvcConfigurer { + + /** + * Uses session storage to remember the user’s language setting across requests. + * Defaults to English if nothing is specified. + * @return session-based {@link LocaleResolver} + */ + @Bean + public LocaleResolver localeResolver() { + SessionLocaleResolver resolver = new SessionLocaleResolver(); + resolver.setDefaultLocale(Locale.ENGLISH); + return resolver; + } + + /** + * Allows the app to switch languages using a URL parameter like + * ?lang=es. + * @return a {@link LocaleChangeInterceptor} that handles the change + */ + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor(); + interceptor.setParamName("lang"); + return interceptor; + } + + /** + * Registers the locale change interceptor so it can run on each request. + * @param registry where interceptors are added + */ + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(localeChangeInterceptor()); + } + +} diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 173417a10..2d779d3c3 100644 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -6,3 +6,28 @@ nonNumeric=must be all numeric duplicateFormSubmission=Duplicate form submission is not allowed typeMismatch.date=invalid date typeMismatch.birthDate=invalid date +owner=Owner +firstName=First Name +lastName=Last Name +address=Address +city=City +telephone=Telephone +owners=Owners +addOwner=Add Owner +findOwner=Find Owner +findOwners=Find Owners +updateOwner=Update Owner +vets=Veterinarians +name=Name +specialties=Specialties +none=none +pages=pages +first=First +next=Next +previous=Previous +last=Last +somethingHappened=Something happened... +pets=Pets +home=Home +error=Error +telephone.invalid=Telephone must be a 10-digit number diff --git a/src/main/resources/messages/messages_de.properties b/src/main/resources/messages/messages_de.properties index 4828d85fe..f87ab62b2 100644 --- a/src/main/resources/messages/messages_de.properties +++ b/src/main/resources/messages/messages_de.properties @@ -6,4 +6,28 @@ nonNumeric=darf nur numerisch sein duplicateFormSubmission=Wiederholtes Absenden des Formulars ist nicht erlaubt typeMismatch.date=ung�ltiges Datum typeMismatch.birthDate=ung�ltiges Datum - +owner=Besitzer +firstName=Vorname +lastName=Nachname +address=Adresse +city=Stadt +telephone=Telefon +owners=Besitzer +addOwner=Besitzer hinzufügen +findOwner=Besitzer finden +findOwners=Besitzer suchen +updateOwner=Besitzer aktualisieren +vets=Tierärzte +name=Name +specialties=Fachgebiete +none=keine +pages=Seiten +first=Erste +next=Nächste +previous=Vorherige +last=Letzte +somethingHappened=Etwas ist passiert... +pets=Haustiere +home=Startseite +error=Fehler +telephone.invalid=Telefonnummer muss aus 10 Ziffern bestehen diff --git a/src/main/resources/messages/messages_es.properties b/src/main/resources/messages/messages_es.properties index 116016fa3..5535dfb7a 100644 --- a/src/main/resources/messages/messages_es.properties +++ b/src/main/resources/messages/messages_es.properties @@ -6,4 +6,28 @@ nonNumeric=Sólo debe contener numeros duplicateFormSubmission=No se permite el envío de formularios duplicados typeMismatch.date=Fecha invalida typeMismatch.birthDate=Fecha invalida - +owner=Propietario +firstName=Nombre +lastName=Apellido +address=Dirección +city=Ciudad +telephone=Teléfono +owners=Propietarios +addOwner=Añadir propietario +findOwner=Buscar propietario +findOwners=Buscar propietarios +updateOwner=Actualizar propietario +vets=Veterinarios +name=Nombre +specialties=Especialidades +none=ninguno +pages=páginas +first=Primero +next=Siguiente +previous=Anterior +last=Último +somethingHappened=Algo pasó... +pets=Mascotas +home=Inicio +error=Error +telephone.invalid=El número de teléfono debe tener 10 dígitos diff --git a/src/main/resources/messages/messages_fa.properties b/src/main/resources/messages/messages_fa.properties index a68a21c77..b71466c14 100644 --- a/src/main/resources/messages/messages_fa.properties +++ b/src/main/resources/messages/messages_fa.properties @@ -6,4 +6,28 @@ nonNumeric=باید عددی باشد duplicateFormSubmission=ارسال تکراری فرم مجاز نیست typeMismatch.date=تاریخ نامعتبر typeMismatch.birthDate=تاریخ تولد نامعتبر - +owner=مالک +firstName=نام +lastName=نام خانوادگی +address=آدرس +city=شهر +telephone=تلفن +owners=مالکان +addOwner=افزودن مالک +findOwner=یافتن مالک +findOwners=یافتن مالکان +updateOwner=ویرایش مالک +vets=دامپزشکان +name=نام +specialties=تخصص‌ها +none=هیچ‌کدام +pages=صفحات +first=اول +next=بعدی +previous=قبلی +last=آخر +somethingHappened=مشکلی پیش آمد... +pets=حیوانات خانگی +home=خانه +error=خطا +telephone.invalid=شماره تلفن باید ۱۰ رقمی باشد diff --git a/src/main/resources/messages/messages_ko.properties b/src/main/resources/messages/messages_ko.properties index 6cd27bfff..a1a8b14cd 100644 --- a/src/main/resources/messages/messages_ko.properties +++ b/src/main/resources/messages/messages_ko.properties @@ -6,3 +6,28 @@ nonNumeric=모두 숫자로 입력해야 합니다 duplicateFormSubmission=중복 제출은 허용되지 않습니다 typeMismatch.date=잘못된 날짜입니다 typeMismatch.birthDate=잘못된 날짜입니다 +owner=소유자 +firstName=이름 +lastName=성 +address=주소 +city=도시 +telephone=전화번호 +owners=소유자 목록 +addOwner=소유자 추가 +findOwner=소유자 찾기 +findOwners=소유자들 찾기 +updateOwner=소유자 수정 +vets=수의사 +name=이름 +specialties=전문 분야 +none=없음 +pages=페이지 +first=첫 번째 +next=다음 +previous=이전 +last=마지막 +somethingHappened=문제가 발생했습니다... +pets=반려동물 +home=홈 +error=오류 +telephone.invalid=전화번호는 10자리 숫자여야 합니다 diff --git a/src/main/resources/messages/messages_pt.properties b/src/main/resources/messages/messages_pt.properties index e9bc35a39..13c442e5c 100644 --- a/src/main/resources/messages/messages_pt.properties +++ b/src/main/resources/messages/messages_pt.properties @@ -6,3 +6,28 @@ nonNumeric=Deve ser tudo numerico duplicateFormSubmission=O envio duplicado de formulario nao e permitido typeMismatch.date=Data invalida typeMismatch.birthDate=Data de nascimento invalida +owner=Proprietário +firstName=Primeiro Nome +lastName=Sobrenome +address=Endereço +city=Cidade +telephone=Telefone +owners=Proprietários +addOwner=Adicionar proprietário +findOwner=Encontrar proprietário +findOwners=Encontrar proprietários +updateOwner=Atualizar proprietário +vets=Veterinários +name=Nome +specialties=Especialidades +none=nenhum +pages=páginas +first=Primeiro +next=Próximo +previous=Anterior +last=Último +somethingHappened=Algo aconteceu... +pets=Animais de estimação +home=Início +error=Erro +telephone.invalid=O número de telefone deve conter 10 dígitos diff --git a/src/main/resources/messages/messages_ru.properties b/src/main/resources/messages/messages_ru.properties index 7e8d54d2d..835fd0430 100644 --- a/src/main/resources/messages/messages_ru.properties +++ b/src/main/resources/messages/messages_ru.properties @@ -6,4 +6,28 @@ nonNumeric=должно быть все числовое значение duplicateFormSubmission=Дублирование формы не допускается typeMismatch.date=неправильная даные typeMismatch.birthDate=неправильная дата - +owner=Владелец +firstName=Имя +lastName=Фамилия +address=Адрес +city=Город +telephone=Телефон +owners=Владельцы +addOwner=Добавить владельца +findOwner=Найти владельца +findOwners=Найти владельцев +updateOwner=Обновить владельца +vets=Ветеринары +name=Имя +specialties=Специальности +none=нет +pages=страницы +first=Первый +next=Следующий +previous=Предыдущий +last=Последний +somethingHappened=Что-то пошло не так... +pets=Питомцы +home=Главная +error=Ошибка +telephone.invalid=Телефон должен содержать 10 цифр diff --git a/src/main/resources/messages/messages_tr.properties b/src/main/resources/messages/messages_tr.properties index 1020566aa..45f86c3d5 100644 --- a/src/main/resources/messages/messages_tr.properties +++ b/src/main/resources/messages/messages_tr.properties @@ -6,4 +6,28 @@ nonNumeric=sadece sayısal olmalıdır duplicateFormSubmission=Formun tekrar gönderilmesine izin verilmez typeMismatch.date=geçersiz tarih typeMismatch.birthDate=geçersiz tarih - +owner=Sahip +firstName=Ad +lastName=Soyad +address=Adres +city=Şehir +telephone=Telefon +owners=Sahipler +addOwner=Sahip Ekle +findOwner=Sahip Bul +findOwners=Sahipleri Bul +updateOwner=Sahip Güncelle +vets=Veterinerler +name=İsim +specialties=Uzmanlıklar +none=yok +pages=sayfalar +first=İlk +next=Sonraki +previous=Önceki +last=Son +somethingHappened=Bir şey oldu... +pets=Evcil Hayvanlar +home=Ana Sayfa +error=Hata +telephone.invalid=Telefon numarası 10 basamaklı olmalıdır diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index b9026690e..070e1c770 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -4,8 +4,8 @@ -

Something happened...

+

Something happened...

Exception message

- \ No newline at end of file + diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index eb54d28ae..e0d82b003 100644 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -45,25 +45,25 @@ diff --git a/src/main/resources/templates/owners/createOrUpdateOwnerForm.html b/src/main/resources/templates/owners/createOrUpdateOwnerForm.html index 72c40fbc6..e9356f177 100644 --- a/src/main/resources/templates/owners/createOrUpdateOwnerForm.html +++ b/src/main/resources/templates/owners/createOrUpdateOwnerForm.html @@ -3,24 +3,24 @@ -

Owner

+

Owner

+ th:replace="~{fragments/inputField :: input (#{firstName}, 'firstName', 'text')}" /> + th:replace="~{fragments/inputField :: input (#{lastName}, 'lastName', 'text')}" /> + th:replace="~{fragments/inputField :: input (#{address}, 'address', 'text')}" /> + th:replace="~{fragments/inputField :: input (#{city}, 'city', 'text')}" /> + th:replace="~{fragments/inputField :: input (#{telephone}, 'telephone', 'text')}" />
diff --git a/src/main/resources/templates/owners/findOwners.html b/src/main/resources/templates/owners/findOwners.html index 0a818fc79..682bc103e 100644 --- a/src/main/resources/templates/owners/findOwners.html +++ b/src/main/resources/templates/owners/findOwners.html @@ -3,13 +3,13 @@ -

Find Owners

+

Find Owners

- +
- +
- Add Owner + Add Owner diff --git a/src/main/resources/templates/owners/ownersList.html b/src/main/resources/templates/owners/ownersList.html index 9af325289..2ff13349c 100644 --- a/src/main/resources/templates/owners/ownersList.html +++ b/src/main/resources/templates/owners/ownersList.html @@ -4,16 +4,16 @@ -

Owners

+

Owners

- - - - - + + + + + @@ -29,7 +29,7 @@
NameAddressCityTelephonePetsNameAddressCityTelephonePets
- Pages: + Pages: [ [[${i}]] diff --git a/src/main/resources/templates/vets/vetList.html b/src/main/resources/templates/vets/vetList.html index e0b8e7050..10a9efb04 100644 --- a/src/main/resources/templates/vets/vetList.html +++ b/src/main/resources/templates/vets/vetList.html @@ -5,13 +5,13 @@ -

Veterinarians

+

Veterinarians

- - + + @@ -19,13 +19,13 @@ + th:if="${vet.nrOfSpecialties == 0}" th:text="#{none}">none
NameSpecialtiesNameSpecialties
none
- Pages: + Pages: [ [[${i}]] @@ -33,24 +33,24 @@ - - + - - + - - + - - +