Spring国际化

由公司平台及相关产品要向印度,俄罗斯等国家发展,原有项目必须做国际化;

原理很简单,spring引用ReloadableResourceBundleMessageSource并指定配置文件的路径,其bean的id指定是id="messageSource",MessageSource会自动找到配置文件message_en.properties、message.properties;在Action层的基类里定义方法获取session中的Language,浏览器在切换语言时,即session的Language的zh_cn  , en变化(现在只支持中英);获取spring容器applicationContext调用getMessage传入key和要动态替换的参数,来获取配置文件的信息返回到前端。

1.app-resources.xml

复制代码
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="cacheSeconds" value="10800" />
        <property name="fallbackToSystemLocale" value="false" />
        <property name="basenames">
            <list>
                <value>classpath:/com/foton/m2m/iov/gate/message</value>
                。。。
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="true" />
    </bean>
复制代码

2.BasicAction.java 类似action.info.opt.success的key和配置文件key一致

复制代码

protected static final String OPT_SUCCESS = "action.info.opt.success";
protected static final String OPT_ERROR = "action.info.opt.error";
protected static final String SAVE_SUCCESS = "action.info.save.success";
protected static final String SAVE_ERROR = "action.info.save.error";
protected static final String EDIT_SUCCESS = "action.info.edit.success";
protected static final String EDIT_ERROR = "action.info.edit.error";
protected static final String DELETE_SUCCESS = "action.info.delete.success";
protected static final String DELETE_ERROR = "action.info.delete.error";
protected static final String REFERENCE_DELETE_ERROR = "action.info.reference.delete.error";


protected
String getI18nInfo(String key, Object[] args, Language lang) { if (lang == Language.en) { return SpringUtils.getProperty(Locale.ENGLISH, key, args, null); } else { return SpringUtils.getProperty(Locale.SIMPLIFIED_CHINESE, key, args, null); } } protected String getI18nInfo(String key) { return getI18nInfo(key, ContextUtil.getCurrentLanguage()); }
复制代码

3.Action.java

复制代码
  @SysLog(name = "删除", type = 2)
    @JSON
    public String delete() {
        HttpServletRequest request = getRequest();
        try {
            Set<Long> ids = ServletUtils.getLongSetValue(request, "ids");
            getService().batchDelete(ids, ContextUtil.getCurrentCompany());
            setInfo(getI18nInfo(DELETE_SUCCESS));
            setSuccess(true);
        } catch (ServiceException e) {
            logger.error(e.getMessage(), e);
            setInfo(getI18nInfo(SAVE_ERROR));
            setSuccess(false);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            setInfo(getI18nInfo(SAVE_ERROR));
            setSuccess(false);
        }
        return ResultType.JSON;
    }
复制代码

SpringUtils.java

复制代码
  /**
     * 获取配置属性(指定语言环境)
     * 
     * @param key
     * @param args
     * @param defaultValue
     * @param locale
     * @return
     * @throws BeansException
     */
    public static String getProperty(Locale locale, String key, Object[] args, String defaultValue) throws BeansException {
        Assert.hasText(key, "Argument:[key] must not be null or empty.");
        Assert.notNull(locale, "locale must not be null or empty.");
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getMessage(key, args, defaultValue, locale);
    }
复制代码

message_en.properties 或 message.properties

复制代码
########################   TruckServiceImpl  ##################
fms.info.Truck.loadCarDataByVin1 = VIN:{0} has registered in the company
fms.info.Truck.loadCarDataByVin2 = VIN:{0} is no on-line vehicles
fms.info.Truck.loadCarDataByVin3 = VIN:{0} has no primary device
fms.info.Truck.batchDelete = There is no vehicle in company
fms.info.Truck.batchAdd1 = A serial, B vin, C lpn
fms.info.Truck.batchAdd2 = {0} row
fms.info.Truck.batchAdd3 = [{0}]Column is required
fms.info.Truck.batchAdd4 = verify that the data is listed incorrectly.
fms.info.Truck.batchAdd5 = VIN format is illegal
fms.info.Truck.batchAdd6 = register success
fms.info.Truck.batchAdd7 = register fail
fms.info.Truck.batchAdd8 = successful upload of {0} records
复制代码

 

posted @   蔡徐坤1987  阅读(284)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示