The process of integrating spring with freemarker for internationalization is very simple. Spring under ORG/springframework/web/servlet/View/freemarker in spring-webmvc is used. FTL include all the FTL files to be internationalized, such as the spring. copy the FTL file to the common directory of the FTL file. You can include the FTL file directly.
<#import "/common/spring.ftl" as spring/>
Of course, you can also automatically import freemarkerconfig configuration in the spring configuration file.
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> ... <property name="freemarkerSettings"> <props> <prop key="defaultEncoding">UTF-8</prop> ... <prop key="auto_import">common/spring.ftl as spring</prop> </props> </property>
Then declare it in the spring configuration file
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>MessageResources</value> </list> </property> </bean>
The corresponding messageresources. zh_cn.properties and messageresources. en_us.propertes attribute files are available in the source file directory resource.
Use this in the FTL File
<@spring.message "hello"/>
In this way, the system will internationalize according to the locale in the current environment. But what if you want to manually specify the locale of a user?
Refer to Org. springframework. web. servlet. i18n. localechangeinterceptor: To set locale, first declare a localeresolver (for example, cookielocaleresolver) in the spring configuration file. Otherwise, exceptions will occur in subsequent operations ), set locale in the session range.
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="zh" /> </bean>
Then add the following code to the locale control method in spring controller.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?"); } LocaleEditor localeEditor = new LocaleEditor(); localeEditor.setAsText(lang); localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
Finally, I will summarize my methods.
1. required in spring Configuration
<Bean id = "freemarkerconfig" class = "org. springframework. web. servlet. view. freemarker. freemarkerconfigurer "> <property name =" templateloaderpath "value ="/WEB-INF/FTL/"/> <property name =" freemarkersettings "> <props> <prop key =" defaultencoding "> UTF-8 </prop> <! -- Set the template update interval in the production environment according to the actual situation. Here we set the development convenience to 5 seconds --> <prop key = "template_update_delay"> 5 </prop> <! -- I set spring. the content in FTL is combined with the public macro used in the project --> <prop key = "auto_import"> common/website. FTL as website </prop> </props> </property> </bean> <bean id = "viewresolver" class = "org. springframework. web. servlet. view. freemarker. freemarkerviewresolver "> <property name =" cache "value =" true "/> <property name =" prefix "value =" "/> <property name =" suffix "value =". FTL "/> <property name =" contenttype "value =" text/html; charset = UTF-8 "> </property> </bean> <bean id =" localeresolver "class =" org. springframework. web. servlet. i18n. sessionlocaleresolver "> <property name =" defaultlocale "value =" ZH "/> </bean> <bean id =" messagesource "class =" org. springframework. context. support. resourcebundlemessagesource "> <property name =" basenames "> <list> <value> messageresources </value> </List> </property> </bean>
2. Set locale in the Controller that sets the User Locale
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?"); } LocaleEditor localeEditor = new LocaleEditor(); localeEditor.setAsText(lang); localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
3. Use it in FTL
<@website.message "hello"/>
Source: http://yvonxiao.iteye.com/blog/1005183