Created
March 24, 2024 23:04
-
-
Save humbertodias/5b3ab5addc8c0b8ba5e00b40de17ad62 to your computer and use it in GitHub Desktop.
SpringBoot set default locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import jakarta.annotation.PostConstruct; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.servlet.LocaleResolver; | |
import org.springframework.web.servlet.i18n.FixedLocaleResolver; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
@Configuration | |
public class LocaleConfiguration { | |
@Value("${default.language:pt}") | |
private String language; | |
@Value("${default.country:BR}") | |
private String country; | |
@Value("${default.timezone:America/Sao_Paulo}") | |
private String timezone; | |
public Locale getDefaultLocale() { | |
return new Locale(language, country); | |
} | |
public TimeZone getDefaultTimeZone() { | |
return TimeZone.getTimeZone(timezone); | |
} | |
@Bean | |
public LocaleResolver localeResolver() { | |
FixedLocaleResolver resolver = new FixedLocaleResolver(); | |
resolver.setDefaultLocale(getDefaultLocale()); | |
return resolver; | |
} | |
@PostConstruct | |
public void init() { | |
TimeZone.setDefault(getDefaultTimeZone()); | |
Locale.setDefault(getDefaultLocale()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment