* * Original article, please do not reprint * *
When the spring boot 1.5.6 + thymeleaf 3 was internationalized, a pit was stepped (in fact more than one). Phenomenon:
See, is the value of the key, followed by the addition of _en_us or _ZH_CN, and before and after the question mark.
First look at the code, first two resource files:
Messages_en_us.properties
Page.content= This is a test string.
Message_zh_cn.properties, found in Eclipse, the content is: This is a test string
page.content=\u8fd9\u662f\u4e00\u4e2a\u6d4b\u5b57\u7b26\u4e32\u3002
I18n.html:
<!DOCTYPE html SYSTEM "HTTP://WWW.THYMELEAF.ORG/DTD/XHTML1-STRICT-THYMELEAF-4.DTD"><HTMLxmlns= "http://www.w3.org/1999/xhtml"xmlns:th= "http://www.thymeleaf.org"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /></Head><Body> <H1>Spring Boot, thymeleaf 3 internationalization</H1> <HR> <ahref= "/i18n?lang=en_us">中文版</a>|<ahref= "/I18N?LANG=ZH_CN">Chinese</a> <BR> <BR> <BR> <BR>From thymeleaf Engine:<spanTh:text= "#{page.content}"></span> <BR> <BR>From controller model:<spanTh:text= "${content}"></span> </Body></HTML>
Where #{page.content} from Thymeleaf 3, ${content} is returned by the controller's model, the code is HardCode, the return is Chinese
Controller
@Controller Public class I18ncontroller { @Autowired private messagesource Messagesource; @GetMapping ("/i18n") public String i18n (model model) { null , Locale.simplified_chinese); System.out.println ("message=" + message); Model.addattribute ("content", message); return "/i18n"; }}
From the phenomenon, ${content} This is no problem, that is, Controller messagesource.getmessage () is normal, again that is, the resource file is successfully loaded, visible, the problem is thymeleaf 3.
Then look at the configuration of Thymeleaf 3:
@Configuration Public classThymeleafconfigImplementsApplicationcontextaware {Private Static FinalString UTF8 = "UTF-8"; PrivateApplicationContext ApplicationContext; @Override Public voidSetapplicationcontext (ApplicationContext ApplicationContext)throwsbeansexception { This. ApplicationContext =ApplicationContext; } Privatespringresourcetemplateresolver Htmltemplateresolver () {springresourcetemplateresolver templateResolver=NewSpringresourcetemplateresolver (); Templateresolver.setapplicationcontext ( This. ApplicationContext); Templateresolver.setprefix ("Classpath:/templates/"); Templateresolver.setsuffix (". html"); Templateresolver.settemplatemode (templatemode.html); Templateresolver.setcacheable (false); Templateresolver.setcharacterencoding ("UTF-8"); returnTemplateresolver; } @AutowiredPrivateMessagesource Messagesource; //If it shows?? X_ZH_CN??, missing Spring-context-support Privatespringtemplateengine templateengine (springresourcetemplateresolver templateresolver) {SpringTemplateEngine te Mplateengine=NewSpringtemplateengine (); Templateengine.settemplateresolver (Templateresolver); Templateengine.setenablespringelcompiler (false); returnTemplateengine; } @Bean Publicviewresolver htmlviewresolver (springtemplateengine templateengine) {thymeleafviewresolver resolver=NewThymeleafviewresolver (); Resolver.setapplicationcontext (ApplicationContext); Resolver.settemplateengine (Templateengine (Htmltemplateresolver ())); Resolver.setcharacterencoding ("UTF-8"); returnResolver; }}
* * Temporary supplement, internationalization to Spring-context-support package support.
Configuration of the Web:
@Configuration Public classMvcconfigextendsWebmvcconfigureradapter {@Bean Publiclocaleresolver Localeresolver () {sessionlocaleresolver SLR=NewSessionlocaleresolver (); //Default LanguageSlr.setdefaultlocale (Locale.simplified_chinese); returnSLR; } @Bean Publiclocalechangeinterceptor Localechangeinterceptor () {Localechangeinterceptor LCI=NewLocalechangeinterceptor (); //Name of parameterLci.setparamname ("Lang"); returnLCI; } @Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (Localechangeinterceptor ()); } //This messagesource is dispensable, spring boot has one by default.
If you do not have a custom messagesource, you have a messages.properties file.
If you have this definition, you don't need messages.properites.//@Bean//Public Resourcebundlemessagesource Messagesource () {//Resourcebundlemessagesource messagesource = new Resourcebundlemessagesource ();//messagesource.setbasename ("messages"); //return messagesource;// }}
Find the reason, see this paragraph on thymeleaf.org: (online document 3.1:using Th:text and externalizing text)
Thymeleaf is called external text, either in an external file, or in a database, and of course internationalized using an external file.
From this information, Thymeleaf uses the Imessageresolver interface to load external text, and there is a standard implementation:
Org.thymeleaf.messageresolver.StandardMessageResolver, so loading the. properties file should be related to this information.
But how to use it in Thymeleaf-spring4, look at the code:
Org.thymeleaf.spring4.messageresolver.SpringMessageResolver:
Private Final standardmessageresolver standardmessageresolver; Private messagesource Messagesource; Public Springmessageresolver () { Super(); This New standardmessageresolver (); }
It's using org.thymeleaf.messageresolver.StandardMessageResolver, so I'll just add the imessageresolver implementation to the template engine's settings, and the new code will look like this:
Private springtemplateengine templateengine (springresourcetemplateresolver templateresolver) { Springtemplateengine templateengine new springtemplateengine (); New springmessageresolver (); Messageresolver.setmessagesource (messagesource); Join these three lines as solution Templateengine.setmessageresolver (messageresolver); Templateengine.settemplateresolver (templateresolver); Templateengine.setenablespringelcompiler (false); return Templateengine;}
Final presentation:
Spring Boot + thymeleaf 3 internationalization