如何解决国际化报错:No message found under code 'good.morning.message' for locale 'us'?
Hey there, let's tackle this internationalization issue you're facing! The error No message found under code 'good.morning.message' for locale 'us' usually points to a few common misconfigurations in your Spring Boot setup. Let's go through the fixes step by step:
Spring Boot looks for internationalization files in src/main/resources by default, and they need to follow a strict naming pattern:
- For the 'us' locale, your file should be named
messages_us.properties(note: the standard locale code for American English isen_US—if you meant that, usemessages_en_US.propertiesinstead, as this avoids ambiguity). - Always keep a fallback
messages.propertiesfile with default values, so Spring has something to use if the requested locale isn't found.
Open your messages_us.properties (or messages_en_US.properties) and make sure the exact key is present with a value, like this:
good.morning.message=Good morning!
Double-check for typos—even a missing underscore or capitalization mismatch will cause this error.
You mentioned using SessionLocaleResolver—let's make sure it's set up correctly. Here's a complete, working configuration:
@SpringBootApplication public class RestfulWebServicesApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(RestfulWebServicesApplication.class, args); } @Bean public LocaleResolver localeResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); // Set a default locale to fall back on localeResolver.setDefaultLocale(Locale.US); // Use Locale.forLanguageTag("us") if you need the exact 'us' code return localeResolver; } // Optional: Add an interceptor to let users switch locale via a request param (e.g., ?lang=us) @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor(); interceptor.setParamName("lang"); return interceptor; } // Register the interceptor with Spring MVC @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
When retrieving the message in your controller, make sure you're passing the correct locale. For example:
@Autowired private MessageSource messageSource; @GetMapping("/greet") public String getGreeting(@RequestHeader(name = "Accept-Language", required = false) Locale locale) { return messageSource.getMessage("good.morning.message", null, locale); }
If you're using the LocaleChangeInterceptor, the locale will be picked up automatically when you add ?lang=us to your GET request.
- Ensure
src/main/resourcesis marked as a source directory: Right-click your project > Properties > Build > Compiling, and confirm the folder is listed under Source Packages. - Clean and rebuild your project (Build > Clean and Build Project)—Netbeans sometimes doesn't refresh resource files automatically, so this ensures your latest message files are included in the build.
内容的提问来源于stack exchange,提问作者brownwood

