项目中有时需要做国际化,且如果国际化文件可能会被分成多个,比如一个全局多个局部文件,
但是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; }