You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决国际化报错: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:

1. Double-check your message file naming & location

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 is en_US—if you meant that, use messages_en_US.properties instead, as this avoids ambiguity).
  • Always keep a fallback messages.properties file with default values, so Spring has something to use if the requested locale isn't found.
2. Verify the message key exists in the right file

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.

3. Fix your LocaleResolver configuration

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());
    }
}
4. Confirm how you're fetching the message

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.

5. Netbeans-specific checks
  • Ensure src/main/resources is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:57:04