Reputation: 2324
Im using thymeleaf and spring, my messages.properties files need to have the same name as the template in order for them to work. I have tried to create a custom path using webConfigurer.Java but it's not working.
WebConfigurer.java source:
...
...
@EnableWebMvc
@EnableTransactionManagement
@Configuration
@ComponentScan({"en.irp.project.*"})
@PropertySource("classpath:/application.properties")
@Import({SecurityConfigurer.class})
public class WebConfigurer extends WebMvcConfigurerAdapter {
...
...
...
@Bean(name="messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
resource.setBasename("WEB-INF/languages/messages");
resource.setDefaultEncoding("UTF-8");
return resource;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean(name = "localeResolver")
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
return localeResolver;
}
...
...
}
Upvotes: 4
Views: 11487
Reputation: 1568
I know it's an old question but well referenced in Google.
I finally found the solution from this issue in Github https://github.com/thymeleaf/thymeleaf-spring/issues/9
You have to use setTemplateEngineMessageSource(MessageSource)
What I've done
@Service
public class TemplateEngineService {
private Logger log = LoggerFactory.getLogger(getClass());
private final SpringTemplateEngine thymeleafTemplateEngine;
public TemplateEngineService(SpringTemplateEngine thymeleafTemplateEngine) {
this.thymeleafTemplateEngine = thymeleafTemplateEngine;
}
public String processTemplate(String templatePath, String messagePath, String locale, Map<String, Object> variables) {
thymeleafTemplateEngine.setTemplateEngineMessageSource(getMessageSource(messagePath));
// ...
}
public MessageSource getMessageSource(String messagePath) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename( messagePath);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
Upvotes: 1
Reputation: 682
Just for future reference.
If Spring Boot is used, you can also set its custom location in application.properties file:
spring.messages.basename=i18n/messages
Javadoc:
Comma-separated list of basenames, each following the ResourceBundle convention. Essentially a fully-qualified classpath location. If it doesn't contain a package qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
Upvotes: 10
Reputation: 311
In your application.properties file, just mention the value for spring.messages.baseDir.
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
List<String> baseNames = new ArrayList<>();
String baseName = applicationProperties.getString("spring.messages.basename", DEFAULT_MESSAGE_BASENAME);
String messageResourcePath = applicationProperties.getString("spring.messages.baseDir");
if(isNotEmpty(messageResourcePath)){
baseNames.add(String.format("file:%s", Paths.get(messageResourcePath, baseName)));
}
baseNames.add(String.format("classpath:%s", baseName));
resource.setBasenames(baseNames.toArray(new String[baseNames.size()]));
String encoding = applicationProperties.getString("spring.messages.encoding", CHARACTER_ENCODING);
LOGGER.debug("Encoding is set to :: {}", encoding);
resource.setDefaultEncoding(encoding);
resource.setUseCodeAsDefaultMessage(true);
return resource;
}
Upvotes: 0
Reputation: 498
I had the same issue and I got it work as follows.
@Bean
public MessageSource messageSource()
{
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames( "file:" + System.getProperty( "web.server.message" ), "/WEB-INF/languages/messages" );
messageSource.setDefaultEncoding( CHARACTER_ENCODING );
return messageSource;
}
Here I wanted to use different set of messages files in DEV mode, so I used a system property in pom.xml for message file path. My messages files are in this format : messages_en.properties under the languages folder.
<systemProperties>
<web.server.message>${basedir}/src/main/resources/conf/languages/messages</web.server.message>
</systemProperties>
Upvotes: 1
Reputation: 7309
The idea that messages.properties files need to have the same name as the template is not very common. However you can solve this by listen all your files in the configuration.
messageSource.setBasenames("classpath:i18n/template1","classpath:i18n/template2"...);
if you put them into the classpath or in replace classpath:i18n
by WEB-INF/languages
.
Perhaps you can create the list of basenames via program logic.
Have in mind, that even with this approach a message-key could only defined once in your application.
Upvotes: 3