[Spring]Spring Mvc實現國際化/多語言
阿新 • • 發佈:2018-09-05
sel hold encoding 內容 logo spring strong etl tle
1.添加多語言文件*.properties
F64_en_EN.properties詳情如下:
F60_G00_M100=Please select data. F60_G00_M101=Are you sure you want to delete? F60_G00_M102=The data is changed. Do you want to save it ?
2、配置springMVC配置文件
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list>
<!--後續開發中需要國際化的模塊添加到這裏即可--> <value>i18n.Example</value> </list> </property> <property name="defaultEncoding" value="UTF-8" /> <property name="useCodeAsDefaultMessage" value="true" /> </bean> <!--設置cookie解釋器分析設置的國際化多語言 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieMaxAge" value="315360000" /> <property name="defaultLocale" value="en_EN" /> <property name="cookieName" value="Language">
</property> </bean>
3.在登錄成功後修改運行環境的語言
// 根據選擇的語言種類設置配置文件中的localeResolver的內容 if ("EN".equals(logonUser.getLanguage())) { Locale localeEN = new Locale("en", "EN"); localeResolver.setLocale(request, response, localeEN); } else if ("JA".equals(logonUser.getLanguage())) { Locale localeJP = new Locale("ja", "JP"); localeResolver.setLocale(request, response, localeJP); } else { Locale localeEN = new Locale("en", "EN"); localeResolver.setLocale(request, response, localeEN); }
4.定義取得文言內容的方法
protected String getMessage(String code,Object... params){ return messageSource.getMessage(code, params, LocaleContextHolder.getLocale()); }
5.取得文言內容
String title = getMessage("RF64_L001");
[Spring]Spring Mvc實現國際化/多語言