public class I18nResourceBundleMessageSource extends
ResourceBundleMessageSource {
private static Log log = LogFactory
.getLog(I18nResourceBundleMessageSource.class);
/**
*
* 重载父类的方法 ,支持在WEB-INF/classes下 以 basename
* 来匹配国际化资源文件,basename取值一般是message_*.properties
*
* 要求国际化资源文件 是这个格式:message_模块名_实体名_locale.properties
*
*/
@Override
public void setBasename(String basename) {
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 通过模式匹配加载多个模式匹配的Resource
Resource[] resources = resolver
.getResources("classpath*:message_*.properties");
Set<String> baseNameSet = new HashSet<String>();
for (int i = 0; i < resources.length; i++) {
Resource r = resources[i];
String fileName = r.getFilename();
log.info("Reading i18n properties: " + fileName);
Pattern p = Pattern.compile("message_[a-zA-Z]+_[a-zA-Z]+");
Matcher matcher = p.matcher(fileName);
if (matcher.find()) {
fileName = matcher.group();
baseNameSet.add(fileName);
}
}
String[] baseNames = (String[]) baseNameSet
.toArray(new String[baseNameSet.size()]);
setBasenames(baseNames);
} catch (IOException ex) {
throw new SystemException("Reading i18n properties error.", ex);
}
/*
*
* List<String> baseNameList = new ArrayList<String>(); String path =
* ""; try { path = CommUtil.getInstance().getWebInfPath(); } catch
* (IllegalAccessException e) { throw new
* SystemException("获取国际化文件路径出错",e); }
*
* //File dir = new File( path + "classes/i18n" ); File dir = new File(
* path + "classes" );
*
*
* FileFilter fileFilter = new WildcardFileFilter(basename); File[]
* files = dir.listFiles(fileFilter); String fileName = null; try { for
* (int i=0;i< files.length;i++){ fileName = files[i].getName(); int
* index = fileName.indexOf("_"); index = fileName.indexOf("_",
* index+1); index = fileName.indexOf("_", index+1);
*
* //fileName = "i18n/" +fileName.substring(0,index); fileName =
* fileName.substring(0,index);
*
* if (! baseNameList.contains(fileName)) { baseNameList.add(fileName);
* } } } catch (Exception e) { throw new SystemException("读取国际化资源文件" +
* fileName + "出错",e); }
*
* String[] baseNames = (String[]) baseNameList.toArray(new
* String[baseNameList.size()]); setBasenames(baseNames);
*/
}
}