刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
        }
    }
}