Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息

Posted

技术标签:

【中文标题】Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息【英文标题】:Thymeleaf + Spring (not Boot) - how to show messages from messageSource 【发布时间】:2016-08-13 15:43:19 【问题描述】:

我在使用 Thymeleaf 设置 Spring MVC(不使用 Boot,因为我在发现 Spring Initializr 之前启动它)以显示来自我的资源包的消息时遇到问题。

应用的主要配置类是:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.m.h")
public class HrmConfiguration extends WebMvcConfigurerAdapter 

    @Bean
    public ViewResolver viewResolver() 
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setTemplateMode("html5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setPrefix("/WEB-INF/html/");
        templateResolver.setSuffix(".html");

        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.addDialect(new SpringSecurityDialect());
        engine.addDialect(new LayoutDialect(new GroupingStrategy()));
        engine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        viewResolver.setCache(false);

        return viewResolver;
    

    @Bean(name = "messageSource")
    public MessageSource messageSource() 

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/i18n/messages");
        messageSource.setFallbackToSystemLocale(false);
        messageSource.setCacheSeconds(0);
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    

    @Bean(name="localeResolver")
    public LocaleResolver localeResolver()
        SessionLocaleResolver  resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(new Locale("pt", "BR"));
        return resolver;
     

    @Bean
    public UrlTemplateResolver urlTemplateResolver() 
        return new UrlTemplateResolver();
    

    // other beans and configs

应用程序的总体视图是:

messages_pt_BR.properties 文件是:

#messages
spring.messages.basename=i18n/messages

app.name=APPLICATION NAME

但是,当我尝试在页面上的某处显示 APPLICATION NAME 时,使用以下代码:

<p th:text="#app.name">app.name</p>

我只得到

??app.name_pt_BR??

我浏览了许多可能的解决方案和教程,包括:

I18n in Spring boot + Thymeleaf http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example How to display messages in Thymeleaf and Spring Boot?

我还是不能让它工作。

浏览器和操作系统设置为语言环境 en_US。

如何解决?

【问题讨论】:

检查src/main/resources/messages_pt_br.properties中的文件是否有相同的内容。 Spring取这个特定文件夹中的一个 Hassam,感谢您的意见,但我仍然没有运气。 还在坚持吗? 是的!仍然没有运气...... 什么版本的 Spring 和 Thymeleaf? 【参考方案1】:

我在这个问题上找到了我的问题的答案:

Spring 4 with thymeleaf Internationalization not identify message from resource properties file

基本上,解决问题的方法是将以下@Bean...

@Bean
public ViewResolver viewResolver() 
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setPrefix("/WEB-INF/html/");
    templateResolver.setSuffix(".html");

    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.addDialect(new SpringSecurityDialect());
    engine.addDialect(new LayoutDialect(new GroupingStrategy()));
    engine.setTemplateResolver(templateResolver);

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(engine);
    viewResolver.setCache(false);

    return viewResolver;

... 分成三个不同的 bean,例如:

@Bean
public ServletContextTemplateResolver templateResolver() 
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setCacheable(false);
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setPrefix(HTML_VIEWS);
    templateResolver.setSuffix(".html");

    return templateResolver;


@Bean
public SpringTemplateEngine templateEngine() 
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addDialect(new SpringSecurityDialect());
    templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
    templateEngine.setTemplateResolver(templateResolver());

    return templateEngine;


@Bean
public ViewResolver viewResolver() 

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setCache(false);
    viewResolver.setOrder(1);

    return viewResolver;

从那以后,我得到了它按预期工作。

感谢您的见解和帮助!

【讨论】:

【参考方案2】:

在这里使用 Spring Boot,但我能够重现您的问题,并且一旦我将其更改为 ResourceBundleMessageSource,它就与 ReloadableResourceBundleMessageSource 相关联,它就可以正常工作。

@Bean(name = "messageSource")
public MessageSource messageSource() 

    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("/i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);
    messageSource.setDefaultEncoding("UTF-8");

    return messageSource;

更新:

发现您需要为 ReloadableReourceBundleMessageSource 提供完整路径,因此如果您的 /il8n 目录在您的类路径中,您需要将您的基本名称设置为 classpath:/il8n/messages

在这里查看我的帖子:Why does Thymeleaf International only works with ResourceBundleMessageSource

【讨论】:

@gtludwig 修改了发现可行的答案。【参考方案3】:
 @Bean(name="localeResolver")
    public LocaleResolver localeResolver()
        SessionLocaleResolver  resolver = new SessionLocaleResolver();
        resolver.resolver.setDefaultLocale(new Locale("en"));
        return resolver;
      

更改了您的 localresolver bean,这可能会对您有所帮助。如果您将浏览器设置为en locale,应用程序如何读取pt_BR,将其更改为味精属性文件中的_en

或者您可以将浏览器区域设置更改为Portuguese - BRAZIL (BR) (pt_BR),而无需在任何地方更改代码

【讨论】:

您是否尝试过将浏览器区域设置更改为葡萄牙语-巴西?不改变任何东西

以上是关于Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot使用thymeleaf模板时报异常:template might not exist or might not be accessible by any of the confi

怎么解决might not be accessible by any of the configured template resolvers

thymeleaf在开发环境正常,但用jar运行时报错 Error resolving template template might not exist or might not be access

Spring Thymeleaf 下载生成的报告

Spring boot 找不到我的模板视图出现意外错误(type=Not Found,status=404)

spring整合thymeleaf