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

IntelliJ社区版编辑Resource Bundle默认locale文件失效及配置messages.properties

Hey there! Let’s tackle your Resource Bundle headaches with Spring Boot and IntelliJ Community Edition step by step. I’ll cover both adding messages.properties to the bundle and fixing the default locale file not working issue.


1. Adding messages.properties to Resource Bundle in IntelliJ Community

First, make sure your messages.properties is placed in the standard Spring Boot location: src/main/resources (this is where Spring looks for it by default). Then follow these steps to get IntelliJ to recognize it as a Resource Bundle:

  • Option 1: Quick right-click method

    1. Right-click your messages.properties file in the Project view.
    2. Hover over Resource Bundle in the context menu, then select Add to Resource Bundle.
    3. IntelliJ will immediately mark it as a Resource Bundle—you’ll see a small bundle icon next to the file, and you can easily add other locale variants (like messages_en.properties) later.
  • Option 2: Project Structure configuration

    1. Open the Project Structure window (use the shortcut Ctrl+Alt+Shift+S on Windows/Linux, Cmd+; on Mac).
    2. On the left sidebar, select Modules, then pick your Spring Boot module.
    3. Switch to the Resources tab.
    4. Click the + button at the top, then choose Resource Bundle.
    5. Navigate to src/main/resources, select messages.properties, and click OK.
    6. Apply the changes and close the window.

2. Fixing the Default Locale File (messages.properties) Not Working

If your default messages.properties isn’t being picked up, here are the most common fixes:

  • Verify your Spring Boot configuration
    Spring Boot auto-loads messages.properties by default, but if you’ve customized settings, double-check your application.properties (or application.yml) has these entries:

    # Point to your message bundle (no .properties suffix needed)
    spring.messages.basename=messages
    # Set your default locale (adjust to your needs, e.g., en_US)
    spring.messages.default-locale=zh_CN
    # Ensure proper encoding for non-ASCII characters
    spring.messages.encoding=UTF-8
    
  • Check file encoding
    Non-ASCII characters (like Chinese) can break if the file encoding is wrong:

    1. Right-click messages.properties → File Encodings.
    2. Set the file encoding to UTF-8.
    3. Check the box for Transparent native-to-ascii conversion—this ensures special characters are stored correctly in the .properties file.
  • Confirm you’re using MessageSource correctly
    Make sure you’re fetching messages using Spring’s MessageSource bean, not just regular property injection:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.MessageSource;
    import org.springframework.stereotype.Component;
    import java.util.Locale;
    
    @Component
    public class MessageService {
        private final MessageSource messageSource;
    
        @Autowired
        public MessageService(MessageSource messageSource) {
            this.messageSource = messageSource;
        }
    
        public String getWelcomeMessage() {
            // Uses the default locale configured in Spring
            return messageSource.getMessage("welcome.message", null, Locale.getDefault());
        }
    }
    

    Note: Using @Value("${welcome.message}") loads values from application.properties, not your Resource Bundle—don’t mix these up!

  • Invalidate IntelliJ cache
    Sometimes IntelliJ’s cached data causes weird issues. Go to File → Invalidate Caches..., select Invalidate and Restart, and let the IDE reload.

  • Double-check your file content
    Ensure your messages.properties has valid key-value pairs, e.g.:

    welcome.message=Welcome to Spring Boot!
    error.not-found=Resource not found
    

    No typos, no missing equals signs, no extra spaces around keys.


Hope these steps get your Resource Bundle working smoothly! If you hit any other roadblocks, feel free to dig into the details of your setup.

内容的提问来源于stack exchange,提问作者Kamil Nękanowicz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:27:30