相关疑难解决方法(0)

如何在Thymeleaf和Spring Boot中显示消息?

我创建了一个使用Thymeleaf作为模板引擎的Spring Boot Web应用程序.我配置了MessageSource在子文件夹中查找消息:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);

    return messageSource;
}
Run Code Online (Sandbox Code Playgroud)

在这个文件夹中,我创建了messages_de.properties包含内容的文件ticket.type.BUG=Fehler.在我的模板中,我尝试显示如下文本:

<p th:text="#{ticket.type.BUG}">BUG</p>
Run Code Online (Sandbox Code Playgroud)

但是当呈现页面时,我得到以下内容:

<p>??ticket.type.BUG_de_DE??</p>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我是否必须配置任何其他bean?

PS:

在"服务器端"我可以使用获取消息MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY).

spring spring-mvc thymeleaf spring-boot

18
推荐指数
1
解决办法
3万
查看次数

I18n在Spring boot + Thymeleaf

我正在尝试使用Spring启动和Thymeleaf创建一个多语言应用程序.

我制作了几个属性文件来保存不同的消息,但我只能用我的浏览器语言显示它(我试过更改浏览器区域设置的扩展但它们似乎不起作用),无论如何我想在我的网站上放一个按钮做这个职责(改变语言),但我不知道如何或在哪里找到如何管理这个.

要告诉你我的配置:

项目结构

项目结构


I18n配置类

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class I18nConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

}
Run Code Online (Sandbox Code Playgroud)

Thymleaf HTML页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    th:with="lang=${#locale.language}" th:lang="${lang}">

<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h3>Spring Boot and Thymeleaf</h3>
    <p>Hello World!</p>
    <p th:text="${nombre}"></p>
    <h1 th:text="#{hello.world}">FooBar</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

消息(属性文件)

messages_en_US.properties

hello.world = Hello people …
Run Code Online (Sandbox Code Playgroud)

spring internationalization thymeleaf spring-boot

12
推荐指数
1
解决办法
2万
查看次数

具有百万美元国际化的Spring 4无法识别来自资源属性文件的消息

使用Spring 4 + thymeleaf在我的webapp上工作以支持国际化.我尝试了很多方法来更改位置文件和basename的配置并仍然接收?? label.greeting _**?? 在模板结果.以下是我的代码

@Configuration
@EnableSpringConfigured
@EnableWebMvc
@EnableScheduling
@ComponentScan(basePackages = {"com.categorybags.web.controller", "com.categorybags.persistence.services"})
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/res/**").addResourceLocations("/res/");
    }

    @Bean
    public ViewResolver viewResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("/WEB-INF/jsp/");
        templateResolver.setSuffix(".jsp");
        templateResolver.setTemplateMode("HTML5");

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

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

        return viewResolver;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(new Locale("en"));
        return localeResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc thymeleaf

3
推荐指数
1
解决办法
4917
查看次数