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
- Right-click your
messages.propertiesfile in the Project view. - Hover over
Resource Bundlein the context menu, then selectAdd to Resource Bundle. - 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.
- Right-click your
Option 2: Project Structure configuration
- Open the Project Structure window (use the shortcut
Ctrl+Alt+Shift+Son Windows/Linux,Cmd+;on Mac). - On the left sidebar, select
Modules, then pick your Spring Boot module. - Switch to the
Resourcestab. - Click the
+button at the top, then chooseResource Bundle. - Navigate to
src/main/resources, selectmessages.properties, and clickOK. - Apply the changes and close the window.
- Open the Project Structure window (use the shortcut
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-loadsmessages.propertiesby default, but if you’ve customized settings, double-check yourapplication.properties(orapplication.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-8Check file encoding
Non-ASCII characters (like Chinese) can break if the file encoding is wrong:- Right-click
messages.properties→File Encodings. - Set the file encoding to
UTF-8. - Check the box for Transparent native-to-ascii conversion—this ensures special characters are stored correctly in the .properties file.
- Right-click
Confirm you’re using
MessageSourcecorrectly
Make sure you’re fetching messages using Spring’sMessageSourcebean, 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 toFile→Invalidate Caches..., selectInvalidate and Restart, and let the IDE reload.Double-check your file content
Ensure yourmessages.propertieshas valid key-value pairs, e.g.:welcome.message=Welcome to Spring Boot! error.not-found=Resource not foundNo 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

