<!-- 國際化 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="cacheSeconds" value="5" /> <!-- 資源刷新間隔時間 --> <property name="basenames"> <list> <!-- 文件示例 --> <!-- /src/config/i18n/messages_en_US.properties --> <!-- /src/config/i18n/messages_zh_CN.properties --> <value>config.i18n.messages</value> </list> </property> </bean> <!-- 基於Session的國際化配置 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> <!-- 國際化操做 攔截器 必需配置,能夠和其它國際化方式通用 --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
// 國際化配置語言 if (langType.equals("zh")) { Locale locale = new Locale("zh", "CN"); request.getSession() .setAttribute( SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); } else if (langType.equals("en")) { Locale locale = new Locale("en", "US"); request.getSession() .setAttribute( SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); } else request.getSession().setAttribute( SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleContextHolder.getLocale());
到org.springframework.web.servlet.view.freemarker目錄下複製spring.ftl到相關目錄(/WebRoot/WEB-INF/template/spring.ftl)java
在springMVC整合freemarker視圖的配置中添加配置:自動引入spring的宏命令庫。jquery
<prop key="auto_import">/template/spring.ftl as spring</prop>
完整的示例:web
<!-- freemarker視圖 --> <!-- 必定要放在viewResolver的前面,這樣就先去找freemarker的 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/" /> <property name="freemarkerSettings"> <props> <!-- 編碼 --> <prop key="defaultEncoding">UTF-8</prop> <!-- 國際化 --> <prop key="auto_import">/template/spring.ftl as spring</prop> <!-- 緩存刷新 --> <prop key="template_update_delay">0</prop> <!-- 數字逗號分隔 --> <prop key="number_format">0.##</prop> <!-- 日期格式化 --> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <!-- 變量null更換爲空字符 --> <prop key="classic_compatible">true</prop> </props> </property> </bean>
提示:也能夠本身手工引用 <#import "/common/spring.ftl" as spring/>spring
<@spring.message "home"/> 其中home在國際化配置文件中配置。緩存
作國際化有些麻煩的,除了頁面上,還有我我的些一下js也要進行國際化,查了些資料,打算使用jquery-i18next,暫時先擱置着,有時間了在進行下一步的記錄。jsp