Created
April 12, 2022 12:52
-
-
Save susimsek/42c4e29000fe336a03d638cfb883970a to your computer and use it in GitHub Desktop.
Spring Boot i18n Customer LocaleResolver
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
public class AcceptHeaderResolver extends AcceptHeaderLocaleResolver { | |
private static final String LOCALE_PARAM = "locale"; | |
@Override | |
public Locale resolveLocale(HttpServletRequest request) { | |
String localeParam = request.getParameter(LOCALE_PARAM); | |
if (StringUtils.hasText(localeParam)) { | |
Locale locale = StringUtils.parseLocaleString(localeParam); | |
this.setDefaultLocale(locale); | |
return locale; | |
} | |
return super.resolveLocale(request); | |
} | |
} |
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
@Configuration | |
public class LocaleConfig { | |
@Bean | |
public LocaleResolver localeResolver() { | |
return new AcceptHeaderResolver(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment