spring国际化: 从数据库中读取国际化资源


public class MessageResource extends AbstractMessageSource implements ResourceLoaderAware, InitializingBean {
	@SuppressWarnings("unused")
	private ResourceLoader resourceLoader = null;
	
	@Autowired
	private Sys_resourcesService sys_resourcesService = null;
	
	private final String MAP_SPLIT_CODE = "|";
	
    private final Map<String, String> properties = new HashMap<String, String>();

    public MessageResource(){
    	
    }

    @Override
	public void afterPropertiesSet() throws Exception {
        System.out.println("中国大陆:语言:" + Locale.SIMPLIFIED_CHINESE.getLanguage() +", 国家");
        System.out.println("台湾:语言:" + Locale.TAIWAN.getLanguage());
    	properties.clear();
        properties.putAll(loadTexts());
	}

    /**
     * 读取资源信息
     * @return
     */
    private List<Resource> getResource(){
        return sys_resourcesService.findAll();
    }

    /**
     * 加载数据
     * @return
     */
    protected Map<String, String> loadTexts(){
        Map<String, String> mapResource = new HashMap<String, String>();
        List<Resource> resources = this.getResource();
        for (Resource item : resources) {
            String code = item.getRskey() + MAP_SPLIT_CODE + item.getLanguage();
            mapResource.put(code, item.getText());
        }
        System.out.println("国际化资源加载完毕.共"+resources.size()+"条.");
        return mapResource;
    }

    /**
     * 获取文本
     * @param code   
     * @param locale 本地化语言
     * @return
     */
    private String getText(String code, Locale locale){
        String localeCode = locale.getLanguage();
        if(localeCode != null && localeCode.toUpperCase().equals(R.lan.ZH)){
        	if(locale.getCountry().toUpperCase().equals(R.lan.CN)){
        		localeCode = R.lan.CN.toLowerCase();
        	}else{
        		localeCode = R.lan.TW.toLowerCase();
        	}
        }
        String key = code + MAP_SPLIT_CODE + localeCode;
        String localeText = properties.get(key);
        String resourceText = code;

        if(localeText != null && !"".equals(localeText)) {
            resourceText = localeText;
        }else{
            localeCode = Locale.ENGLISH.getLanguage();
            key = code + MAP_SPLIT_CODE + localeCode;
            localeText = properties.get(key);
            if(localeText != null && !"".equals(localeText)) {
                resourceText = localeText;
            }else{
                try{
                    if(getParentMessageSource() != null) {
                        resourceText = getParentMessageSource().getMessage(code, null, locale);
                    }
                }catch(Exception e) {
                    logger.error("找不到对应的消息, code: " + code);
                }
            }
        }
        return resourceText;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader){
        this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale){
        String msg = getText(code, locale);
        MessageFormat result = createMessageFormat(msg, locale);
        return result;
    }

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale){
        String result = getText(code, locale);
        return result;
    }
}

  

转载于:https://www.cnblogs.com/CR1me/p/9875012.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值