Message Resolution
The MessageResolver loads messages from Java ResourceBundle files (.properties) and supports locale-aware message lookup. This is essential for internationalized validation error messages and user-facing text.
Properties File
Section titled “Properties File”Define your messages in a standard Java properties file (e.g., messages.properties):
error.name.required=Name is requirederror.email.invalid=Email address is invaliderror.age.minimum=Age must be at least {0}warning.deprecated=This feature is deprecatedCreating a MessageResolver
Section titled “Creating a MessageResolver”// Create a resolver from a resource bundleMessageResolver resolver = MessageResolver.builder("messages").build();
// Look up a messageString msg = resolver.resolve(Locale.getDefault(), "error.name.required");// "Name is required"
// With a default fallbackString msg2 = resolver.resolve(Locale.getDefault(), "unknown.key", "Default text");// "Default text"Multiple Bundles
Section titled “Multiple Bundles”You can register multiple resource bundles. The resolver searches them in order until it finds a match:
MessageResolver resolver = MessageResolver.builder( "messages", "validation_messages").build();// Searches both bundles in orderIntegration with RuleContext
Section titled “Integration with RuleContext”Wire your message resolver into the RuleContext so validation rules can automatically resolve messages:
RuleContext ctx = RuleContext.builder() .messageResolver("messages") .build(bindings);Locale Support
Section titled “Locale Support”Java ResourceBundles support locale-specific files using the standard naming convention. The resolver will automatically pick the correct file based on the active locale:
| File | Locale |
|---|---|
| messages.properties | Default (fallback) |
| messages_en.properties | English |
| messages_fr.properties | French |
| messages_de.properties | German |
| messages_es.properties | Spanish |
Use Case
Section titled “Use Case”Message resolution is primarily used for internationalized validation error messages. By externalizing messages into properties files, you can support multiple languages without changing your rule logic, and maintain a single source of truth for all user-facing text.