<fmt:bundle标签,使其支持同时加载多个国际化文件.

项目中有时需要做国际化,且如果国际化文件可能会被分成多个,比如一个全局多个局部文件,

但是JSTL的<fmt:bundle国际化标签,只支持加载单个国际化文件

所以我对其进行了小量的修改,使其能支持同时加载多个国际化文件 

<fmt:bundle basename="config/messages/public,config/messages/news"></fmt:bundle>

 

首先需要找到org.apache.taglibs.standard.tag.common.fmt.BundleSupport.java文件

修改283行(最后一个方法)代码的方法如下.

 

 private static ResourceBundle findMatch(String basename, Locale pref) {
	ResourceBundle match = null;

	try {
		ResourceBundle bundle=null;
		if(basename!=null && basename.indexOf(",")!=-1){
//		----自建代码---wuxiaogang 2015-10-29 新增 如果有问题请屏蔽下面新增代码---
			String[] basenameArray=basename.split(",");
			SequenceInputStream ins=null;
			Vector<InputStream> v = new Vector<InputStream>();
			if(basenameArray!=null){
				for(String name:basenameArray){
					if(Validator.isNullEmpty(name)){
						continue;
					}
					InputStream in_a=Thread.currentThread().getContextClassLoader().getResourceAsStream(name.trim()+"_"+pref.getLanguage()+"_"+pref.getCountry()+".properties");
					if(in_a!=null){
						v.add(in_a);
					}else{
						System.err.println("================不存在的国际化文件=="+name.trim()+"_"+pref.getLanguage()+"_"+pref.getCountry()+".properties");
					}
				}
			}
			Enumeration<InputStream> en = v.elements();
			ins=new SequenceInputStream(en);
			bundle = new PropertyResourceBundle(ins);
			try {
				ReflectHelper.setValueByFieldName(bundle,"locale", pref);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
//		----原有代码---wuxiaogang 2015-10-29 屏蔽 如果有问题请屏蔽上面新增代码-打开此处代码--
			bundle = ResourceBundle.getBundle(basename, pref, Thread.currentThread().getContextClassLoader());
		}
		Locale avail = bundle.getLocale();
	    if (avail==null || pref.equals(avail)) {
		// Exact match
		match = bundle;
	    } else {
                /*
                 * We have to make sure that the match we got is for
                 * the specified locale. The way ResourceBundle.getBundle()
                 * works, if a match is not found with (1) the specified locale,
                 * it tries to match with (2) the current default locale as 
                 * returned by Locale.getDefault() or (3) the root resource 
                 * bundle (basename).
                 * We must ignore any match that could have worked with (2) or (3).
                 * So if an exact match is not found, we make the following extra
                 * tests:
                 *     - avail locale must be equal to preferred locale
                 *     - avail country must be empty or equal to preferred country
                 *       (the equality match might have failed on the variant)
		 */
                if (pref.getLanguage().equals(avail.getLanguage())
		    && ("".equals(avail.getCountry()) || pref.getCountry().equals(avail.getCountry()))) {
		    /*
		     * Language match.
		     * By making sure the available locale does not have a 
		     * country and matches the preferred locale's language, we
		     * rule out "matches" based on the container's default
		     * locale. For example, if the preferred locale is 
		     * "en-US", the container's default locale is "en-UK", and
		     * there is a resource bundle (with the requested base
		     * name) available for "en-UK", ResourceBundle.getBundle()
		     * will return it, but even though its language matches
		     * that of the preferred locale, we must ignore it,
		     * because matches based on the container's default locale
		     * are not portable across different containers with
		     * different default locales.
		     */
		    match = bundle;
		}
	    }
	} catch (MissingResourceException | IOException mre) {
	}

	return match;
    }

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值