com.dahua.eco.base.spring.i18n.I18nResourceProcessor Maven / Gradle / Ivy
package com.dahua.eco.base.spring.i18n;
import com.dahua.eco.base.spring.context.BusinessContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class I18nResourceProcessor {
private Logger logger = LoggerFactory.getLogger(I18nResourceProcessor.class);
private static String RESOURCE_SUFFIX_NAME = "properties";
private static String RESOURCE_CLASSPATH_PREFIX = "classpath:";
// 以 jar 包打包并且部署运行的时候,需要对资源文件添加一层资源目录
private static String RESOURCE_DIR_WITH_JAR = "resources";
// 国际化资源基础名称
private List baseNames = new ArrayList();
/**
* 获取文件夹下所有的国际化文件名
*
* @param baseFolderName 基础文件目录,基于classpath
* @return
* @throws IOException
*/
public String[] getAllBaseNames(String baseFolderName) throws IOException {
File file = null;
String i18nPath = "";
// 加载国家化资源文件,默认从两个地方去读取
try {
// 1. 先从 classpath 下读取
i18nPath = baseFolderName;
Resource resource = new ClassPathResource(i18nPath);
file = resource.getFile();
} catch (FileNotFoundException e) {
logger.warn("i18n resources not exist at classpath: {}", i18nPath);
i18nPath = RESOURCE_DIR_WITH_JAR + File.separator + baseFolderName;
// 2. classpath 下没找到,就从 jar 运行目录下读
String i18nAbsolutePath = "";
try {
i18nAbsolutePath = BusinessContext.getInstance().getMainJarPath() + File.separator + i18nPath;
logger.warn("retry find i18n resources at main-jar root path: {}", i18nAbsolutePath);
file = new File(i18nAbsolutePath);
} catch (Exception e2) {
logger.error("can not found i18n resources at classpath or main-jar root path: {} / {}", i18nPath, i18nAbsolutePath);
}
}
if (file.exists() && file.isDirectory()) {
this.getAllFile(file, "");
} else {
logger.error("指定的 baseFolderName 不存在或者不是文件夹: {}", file.getPath());
}
return baseNames.toArray(new String[baseNames.size()]);
}
/**
* 遍历指定目录下的所有文件
*
* @param folder
* @param path
*/
private void getAllFile(File folder, String path) {
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
getAllFile(file, path + folder.getName() + File.separator);
} else {
String fileName = file.getName();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!RESOURCE_SUFFIX_NAME.equals(suffix)) {
continue;
}
this.getAllFile(file, path + folder.getName() + File.separator);
}
}
} else {
String i18Name = this.getI18FileName(path + folder.getName());
if (!baseNames.contains(i18Name)) {
baseNames.add(i18Name);
}
}
}
/**
* 把普通文件名转换成国际化文件名
*
* @param filename
* @return
*/
private String getI18FileName(String filename) {
filename = filename.replace("." + RESOURCE_SUFFIX_NAME, "");
for (int i = 0; i < 2; i++) {
int index = filename.lastIndexOf("_");
if (index != -1) {
filename = filename.substring(0, index);
}
}
return RESOURCE_CLASSPATH_PREFIX + filename;
}
}