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 @ 2019-08-08 14:48  蔡徐坤1987  阅读(282)  评论(0编辑  收藏  举报