Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Spring Boot Application with fixed locale

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?

like image 355
Oliver Avatar asked Jun 08 '18 14:06

Oliver


People also ask

How does i18n work in spring boot?

Internationalization or I18N is a process that makes your application adaptable to different languages and regions without engineering changes on the source code. You can display messages, currencies, date, time etc.

What is the default locale resolver used in Spring framework?

Interface LocaleResolver The default implementation is AcceptHeaderLocaleResolver , simply using the request's locale provided by the respective HTTP header.


2 Answers

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.

like image 105
harsh Avatar answered Oct 10 '22 04:10

harsh


Since SpringBoot 2.4.0 use:

spring.web.locale=en_EN
spring.mvc.localeResolver=fixed
like image 20
kyakya Avatar answered Oct 10 '22 05:10

kyakya