Spring Boot + Thymeleaf 中的 I18n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36531131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I18n in Spring boot + Thymeleaf
提问by No One
I'm trying to make a multilanguage application using Spring boot and Thymeleaf.
我正在尝试使用 Spring boot 和 Thymeleaf 制作一个多语言应用程序。
I made few properties files to save the different messages but I'm only able to display it in my browser language (I tried extensions to change browser locale but they seem to not be working), anyway I wanted to put a button in my website to do this duty (changing the language), but I don't know how or where to find how to manage this.
我制作了几个属性文件来保存不同的消息,但我只能以我的浏览器语言显示它(我尝试使用扩展程序来更改浏览器区域设置,但它们似乎不起作用),无论如何我想在我的网站上放置一个按钮履行这项职责(更改语言),但我不知道如何或在哪里可以找到如何管理它。
Gonna show you my config:
给你看我的配置:
Structure of the project
项目结构
I18n configuration class
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 page
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 (Properties files)
消息(属性文件)
messages_en_US.properties
messages_en_US.properties
hello.world = Hello people
messages_es.properties
messages_es.properties
hello.world = Hola gente
Actually the message is displaying in Spanish, not sure how would I change this, so if you could help me thank you very much.
实际上消息是用西班牙语显示的,不知道我会如何改变它,所以如果你能帮助我非常感谢你。
There's another question that comes to my mind... How would I get the messages from the Database instead from the properties file?
我想到了另一个问题......我如何从数据库而不是从属性文件中获取消息?
回答by Lay Leangsros
Your application should extends WebMvcConfigurerAdapter
您的应用程序应该扩展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());
}
}
Then on browser you can switch language with param langExample: http://localhost:1111/?lang=khwhich messages_kh.properites will store the content of Khmer language.
然后在浏览器上,您可以使用参数lang切换语言 示例:http://localhost:1111/?lang=kh其中 messages_kh.properites 将存储高棉语的内容。