1.在spring配置文件中增加如下配置
<span style="white-space:pre"> </span><bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:/i18n/messages</value> //资源文件的位置
</list>
</property>
<property name="useCodeAsDefaultMessage" value="true" />
<property name="defaultEncoding" value="UTF-8" />
<property name="cacheSeconds" value="3" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
2.创建不同的资源文件
messages_en.properties,messages_zh_CN.properties
示例
messages_en.properties
#labels##############################
view.label.search=查询
view.label.reset=重置
view.label.add=新增
view.label.operate=操作
view.label.detail=详情
view.label.update=修改
view.label.delete=删除
view.label.input=请输入
controller.update.success=更新成功
<span style="color:#ff0000;">messages_zh_CN.properties</span>
#labels##############################
view.label.search=Search
view.label.reset=Reset
view.label.add=Add
view.label.operate=Operate
view.label.detail=Detail
view.label.update=Update
view.label.delete=Delete
view.label.input=Please input
controller.update.success=Update Success
3.在velocity的页面中使用标签
#springMessage('view.label.search') 来表示对应的语言的文字
4.后台josn返回的数据的数据的国际化
@RequestMapping(value = "update")
@ResponseBody
public Wrapper<?> update(Model model, Orders orders) {
try {
orders.setUpdateUser(getLoginUserCnName());
if (ordersService.update(orders)) {
return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "controller.update.success");
} else {
return WrapMapper.wrap(Wrapper.ERROR_CODE, "controller.update.fail");
}
} catch (Exception e) {
LOG.error("orders update has error.", e);
return WrapMapper.error();
}
}
定义多个返回值的的key 如上面代码中的 controller.update.success
定义一个包含key value的方法
<script language="javascript">
jQuery.i18nMessage ={
"controller.update.success":"#springMessage('controller.update.success')",
"controller.add.fail.exist":"#springMessage('controller.add.fail.exist')",
"controller.add.fail":"#springMessage('controller.add.fail')",
};
</script>
调用的时候使用 jQuery.i18nMessage[controller.update.success] 就会显示对应的国际化信息
5.对于下拉框中多种类型的 如果是定义的eunm
public enum EmployeeEnumsStatus {
a("0","禁用","uos.department.disable"),
b("1","启用","uos.department.enable");
private String status;
private String name;
private String key; //表示国家化文件中的key值
<span style="white-space:pre"> public static Object MapEnum(){
Map<String, String> map = new HashMap<String, String>();
for(EmployeeEnumsStatus printStatus : EmployeeEnumsStatus.values()){
<span style="color:#ff0000;"> map.put( printStatus.getStatus(),printStatus.getKey()); // 转化得到对应的key</span>
}
return map;
} </span>
}
在页面遍历的时候
<select class="col-xs-10 col-sm-8" name="balanceType" id="balanceType" style="width:180px;" order_data="$!orders.balanceType">
<option value="" selected>-#springMessage('uos.query.select')#springMessage('uos.orders.balanceType')-</option>
#if(!$null.isNull($!balanceType))
#foreach($key in $balanceType.keySet())
<option value="$key"
#if("$!query.balanceType" == "$!key")
selected
#end
><span style="color:#ff0000;">#springMessage(${balanceType.get($key)})</span></option>
#end
#end
</select>
参考文章 文章链接