package i18n;
|
|
import jnpf.i18n.core.MyReloadableResourceBundleMessageSource;
|
import jnpf.i18n.provider.MessageSourceProvider;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.core.io.ClassPathResource;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.nio.charset.StandardCharsets;
|
|
/**
|
* 国际化配置 classpath 读取(补丁 #8:替代 NacosMessageSouceProvider)。
|
* 行为对齐原 Nacos 版:按 basename 取 <名字>.properties 内容返回;
|
* 差异仅在来源(classpath,message*.properties 已打进 cloud-base)与热更新(不支持,改文案需重建)。
|
*/
|
@Slf4j
|
public class ClasspathMessageSourceProvider implements MessageSourceProvider {
|
|
@Override
|
public String loadMessageResource(String name, MyReloadableResourceBundleMessageSource messageSource) throws IOException {
|
File file = new File(name);
|
String fileName = file.getName() + MessageSourceProvider.PROPERTIES_SUFFIX;
|
ClassPathResource resource = new ClassPathResource(fileName);
|
if (!resource.exists()) {
|
// 与 Nacos 版 data-id 不存在时返回 null 的行为一致
|
return null;
|
}
|
try (InputStream in = resource.getInputStream()) {
|
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
|
}
|
}
|
}
|