【问题标题】:I18n in Spring boot + ThymeleafSpring Boot 中的 I18n + Thymeleaf
【发布时间】:2016-07-31 13:52:56
【问题描述】:

我正在尝试使用 Spring boot 和 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;
    }

}

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>

消息(属性文件)

messages_en_US.properties

hello.world = Hello people

messages_es.properties

hello.world = Hola gente

实际上消息是用西班牙语显示的,不知道我该如何更改,所以如果你能帮助我,非常感谢。

我想到了另一个问题...如何从数据库而不是属性文件中获取消息?

【问题讨论】:

  • 愚蠢的问题:你是如何设置Locale的?您是否配置了LocaleResolver?你如何将它传递给你想要的语言环境?
  • 我不是,我猜 Spring 是从浏览器配置中获取它的。我应该根据按钮设置来设置语言环境吗?不确定它是否会以这种方式工作。
  • the top 开始并继续前进。您需要 3 件事 1) MessageSource, 2) LocaleResolver 来确定特定请求,使用什么语言环境,以及(可选)3) LocaleChangeInterceptor 以便您可以从请求中,在LocaleResolver 中设置语言环境。
  • 你也可以考虑AcceptHeaderLocaleResolver,它不存储Locale,而是从Request中读取。由你决定......
  • 模板引擎无关紧要 - MessageSource 可以。它使用LocaleResolver 来决定使用哪个语言环境。您确定您已将 ThymeLeaf 设置为使用 Spring MessageSource 而不是它自己的吗?

标签: spring spring-boot internationalization thymeleaf


【解决方案1】:

您的应用程序应该扩展 WebMvcConfigurerAdapter

@SpringBootApplication
public class NerveNetApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(NerveNetApplication.class, args);
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new CookieLocaleResolver();
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

然后在浏览器上,您可以使用参数 lang 切换语言 示例:http://localhost:1111/?lang=kh which messages_kh.properites 将存储高棉语的内容。

【讨论】:

  • 作为一个较旧的答案,WebMvcConfigurerAdapter 现在已被弃用。相反,您应该使用implements WebMvcConfigurer。此答案中的代码仍然可以使用替换抽象类的接口。
猜你喜欢
  • 2018-01-26
  • 2018-05-11
  • 2021-02-08
  • 2016-06-25
  • 2017-05-26
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多