简体   繁体   English

Spring Boot、Thymeleaf 电子邮件模板国际化

[英]Spring Boot, Thymeleaf email template internationalization

I'm trying to change the language of the email template based on the users preferred language (ID of the preferred language is stored in the user table).我正在尝试根据用户的首选语言(首选语言的 ID 存储在用户表中)更改电子邮件模板的语言。 When preparing the message I pass the Locale to the Context as shown below:在准备消息时,我将 Locale 传递给 Context,如下所示:

Locale locale = new Locale(user.getLanguage().getLocaleCode());
final Context ctx = new Context(locale);
ctx.setVariable("username", user.getUsername());
ctx.setVariable("address", user.getAddress());

When I log the result of the getLocaleCode() it returns the expected value, for example 'sr'.当我记录getLocaleCode()的结果时,它返回预期值,例如“sr”。 Inside my /resources/lang folder I have created messages_sr.properties file.在我的 /resources/lang 文件夹中,我创建了 messages_sr.properties 文件。 But for some reason when I send the email it will always use the language in the default messages.properties.但是出于某种原因,当我发送电子邮件时,它会始终使用默认的 messages.properties 中的语言。 Here is my configuration file:这是我的配置文件:

@Configuration
public class LocaleConfig {

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }
    
    @Bean(name = "messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:lang/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

Inside the resources/lang folder I have:在 resources/lang 文件夹中,我有:

  • messages_en_US.properties messages_en_US.properties
  • messages_sr.properties messages_sr.properties

Based on the documentation and examples I was able to find I couldn't find the solution.根据我找到的文档和示例,我找不到解决方案。 Can anyone please help?有人可以帮忙吗?

Thanks.谢谢。

https://stackoverflow.com/a/11152674/3849555启发,请改用classpath*:lang/messages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM