Running a Spring Boot Application with fixed locale
Asked Answered
B

3

15

I am working on a server application without a user frontend. To ensure that all parts of the application will generate only English message I would like to ensure that the application will use only a specific locale.

After reading the docs and some googling I came to the conclusion that I have to do the following:

  1. Setting the locale programmatically in the main method via Locale.setDefault(Locale.ENGLISH);

  2. Provide a bean instance of FixedLocaleResolver as locale resolver for the application.

Is this correct or this there a better way to achive my goal?

Billiebilling answered 8/6, 2018 at 14:47 Comment(0)
L
9

you can put

spring.mvc.locale=en_EN
spring.mvc.localeResolver=fixed

in your application.properties or application.yml(of course in yaml format).

Spring will automatically read those properties.

I am not aware of any other better way.

Ludovika answered 8/6, 2018 at 15:9 Comment(1)
Can this also be done for session locale or only for fixed locale?Marduk
C
5

Since SpringBoot 2.4.0 use:

spring.web.locale=en_EN
spring.mvc.localeResolver=fixed
Chud answered 27/12, 2020 at 13:40 Comment(1)
with spring boot 2.5.3 i had to use spring.web.locale-resolver=fixedTheadora
B
5

In application.yml:

spring:
  web:
    locale: en_EN
    locale-resolver: fixed
Burlingame answered 6/9, 2021 at 9:23 Comment(1)
This combination works well for me in Spring Boot 3.0 . Thanks Ashley!Culinarian

© 2022 - 2024 — McMap. All rights reserved.