I'm using MessageSource
of Spring to load errors messages from a .properties
file in classpath. My properties respect a certain "template" such as {Object}.{field}.{unrespectedConstraint}
Example :
userRegistrationDto.password.Size= Le mot de passe doit avoir au minimum 6 caractères.
userRegistrationDto.email.ValidEmail= Merci de saisir une addresse mail valide.
In case of refactoring (Changing the name of the class for example), I have to change my properties file in several places.
Is there any way to use a yaml file (messages.yml) as a ResourceBundle to obtain something like :
userRegistrationDto:
password:
Size: Le mot de passe doit avoir au minimum 6 caractères.
email:
ValidEmail: Merci de saisir une addresse mail valide.
Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.
yml file in a Spring Boot application. In a Spring Boot application, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments.
If your key is loginUrl (inside your yaml file), you can inject its value with the @Value annotation, inside a Spring component. @Value("${loginUrl}") private String loginUrl; If it's a second level property, the path is @Value("${yourFirstKey. loginUrl}") .
In a default structure Spring Boot web application, you can locate the application.yml file under the resources folder. To understand how Spring Boot Logging works, let’s consider an application with an empty application.yml file. model.put("message", "Hello World!");
2. @PropertySource and YAML Format Spring Boot has great support for externalized configuration. Also, it's possible to use different ways and formats to read the properties in the Spring Boot application out-of-the-box. However, by default, @PropertySource doesn't load YAML files. This fact is explicitly mentioned in the official documentation.
In Spring Boot, YAML files can be overridden by other YAML properties files. Prior to version 2.4.0, YAML properties were overridden by properties files in the following locations, in order of highest precedence first: Profiles' properties placed outside the packaged jar Profiles' properties packaged inside the packaged jar
Java ResourceBundle for YAML format. Accesses YAML-formatted resources via ResourceBundle. Supports locale-specific resources according to the ResourceBundle specification. Supports YAML values nested in a map or list. Supports YAML anchors and aliases indicated by & and *. Supports multiple YAML documents separated by ---.
I think this should suffice for your requirements, if you need the MessageSource to be reloadable during VM operation, you might have to do a bit more digging.
@Configuration
public class TestConfig {
@Bean(name = "testProperties")
public Properties yamlProperties() throws IOException {
YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
bean.setResources(new ClassPathResource("test.yml"));
return bean.getObject();
}
@Bean
public MessageSource messageSource() throws IOException {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setCommonMessages(yamlProperties());
return messageSource;
}
}
The best solution I managed to find was found before me by @vtosh: to use this library. The only problem (but still) is that it is not popular enough.
The other option could be to extend Java localization support manually extending the ResourceBundle.Control
class (you can find an official example here). But I don't see much sense in it since the library @vtosh found uses this approach.
Why there is no a solution for Spring? Well, the answer you can find in this jira. It is still in Open state so I don't expect to be any solution from their side at least for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With