Spring Boot 1.5.9无法加载国际化消息文件问题咨询
Hey there, let's dig into why your app isn't picking up the messages_en.properties file when switching locales. I've run into similar quirks with Spring Boot 1.5.x before, so here are the key areas to check:
1. Double-Check Core Configuration
Spring Boot 1.5.x has sensible defaults, but small misconfigurations can break internationalization. Verify these settings in application.properties:
- Ensure the message basename is correctly set (even though
messagesis default, typos happen easily):spring.messages.basename=messages spring.messages.cache-seconds=-1 # Disable caching during development to avoid stale resources - If you're using URL parameters (like
?lang=en) to switch locales, you'll need to add aLocaleChangeInterceptorand customLocaleResolver—the defaultAcceptHeaderLocaleResolveronly reads theAccept-Languageheader from requests. Here's a quick setup:@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } @Bean public LocaleResolver localeResolver() { CookieLocaleResolver resolver = new CookieLocaleResolver(); resolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE); // Set your default locale resolver.setCookieName("app_locale"); resolver.setCookieMaxAge(86400); // 1-day cookie expiry return resolver; } }
2. Validate File Naming & Packaging
- Filename precision: The file must be named exactly
messages_en.properties—no typos, no incorrect capitalization (e.g.,Messages_en.propertiesormessage_en.propertieswon't be recognized). - Classpath presence: After building your app, check the
target/classesdirectory (Maven) orbuild/resources/main(Gradle) to confirm bothmessages.propertiesandmessages_en.propertiesare present. If they're missing, your build tool might be excluding the resources directory (rare for default setups, but worth checking). For Maven, ensure yourpom.xmlincludes this standard resource config:<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> </resource> </resources> </build>
3. Check Thymeleaf Template Syntax
- Make sure you're using Thymeleaf's internationalization syntax correctly: use
#{message.key}instead of${message.key}to reference messages. Example:<h2 th:text="#{greeting.welcome}"></h2> - Confirm your template includes the Thymeleaf namespace in the root HTML tag—without this, Thymeleaf's directives won't work:
<html xmlns:th="http://www.thymeleaf.org">
4. Rule Out Caching Issues
Even if you've updated the properties files, Spring Boot might be serving cached versions. Try these fixes:
- Set
spring.messages.cache-seconds=-1inapplication.propertiesto disable caching during development. - Restart your app (or use Spring DevTools for automatic restarts when resources change).
5. Test Locale Resolution Directly
To isolate the issue, add a simple controller method to print the current locale and a test message:
@GetMapping("/test-locale") @ResponseBody public String testLocale(Locale locale, MessageSource messageSource) { String message = messageSource.getMessage("greeting.welcome", null, locale); return String.format("Current Locale: %s, Message: %s", locale, message); }
Call this endpoint with the appropriate Accept-Language header or lang parameter (if you set up the interceptor) to see if it picks up the correct message from messages_en.properties.
Start with the simplest checks first—filename spelling and basic configuration are almost always the root cause here!
内容的提问来源于stack exchange,提问作者user2466251

