多语言-配置文件外置独立的命名空间
This commit is contained in:
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "spring.messages")
|
||||
public class I18nMessageConfig {
|
||||
private String nameSpace;
|
||||
/**
|
||||
* 国际化文件目录
|
||||
*/
|
||||
|
@@ -27,14 +27,52 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class I18nMessageNacosAutoConfig {
|
||||
|
||||
/**
|
||||
* 应用名称
|
||||
*/
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
/**
|
||||
* 命名空间
|
||||
*/
|
||||
private String nameSpace;
|
||||
/**
|
||||
* 服务器地址
|
||||
*/
|
||||
private String serverAddr;
|
||||
|
||||
@Autowired
|
||||
private I18nMessageConfig messageConfig;
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
||||
|
||||
@Primary
|
||||
@Bean(name = "messageSource")
|
||||
@DependsOn(value = "i18nMessageConfig")
|
||||
public ReloadableResourceBundleMessageSource messageSource() {
|
||||
String dirPath = ResourceUtils.FILE_URL_PREFIX + System.getProperty("user.dir");
|
||||
if (dirPath.endsWith(File.separator)) {
|
||||
dirPath = dirPath.substring(0, dirPath.length() - File.separator.length());
|
||||
}
|
||||
String path = dirPath + File.separator + messageConfig.getBaseFolder() + File.separator + messageConfig.getBasename();
|
||||
log.info("国际化配置内容:{}", JSONObject.toJSONString(messageConfig));
|
||||
log.info("国际化配置路径:{}", path);
|
||||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
messageSource.setBasename(path);
|
||||
messageSource.setDefaultEncoding(messageConfig.getEncoding());
|
||||
messageSource.setCacheMillis(messageConfig.getCacheMillis());
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void init() {
|
||||
serverAddr = applicationContext.getEnvironment().getProperty("spring.cloud.nacos.config.server-addr");
|
||||
dNamespace = applicationContext.getEnvironment().getProperty("spring.cloud.nacos.config.namespace");
|
||||
if (StringUtils.isEmpty(dNamespace)) {
|
||||
dNamespace = DEFAULT_NAMESPACE;
|
||||
}
|
||||
log.info("开始初始化国际化配置!应用名称:{},Nacos地址:{},提示语命名空间:{}", applicationName, serverAddr, dNamespace);
|
||||
nameSpace = messageConfig.getNameSpace();
|
||||
log.info("开始初始化国际化配置!应用名称:{},Nacos地址:{},提示语命名空间:{}", applicationName, serverAddr, nameSpace);
|
||||
WebLocaleConfig.locales.forEach(item -> {
|
||||
initTip(item, true);
|
||||
});
|
||||
@@ -63,7 +101,7 @@ public class I18nMessageNacosAutoConfig {
|
||||
log.info("开始获取{}的配置", dataId);
|
||||
Properties properties = new Properties();
|
||||
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
||||
properties.put(PropertyKeyConst.NAMESPACE, dNamespace);
|
||||
properties.put(PropertyKeyConst.NAMESPACE, nameSpace);
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
content = configService.getConfig(dataId, DEFAULT_GROUP, 5000);
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
@@ -111,47 +149,4 @@ public class I18nMessageNacosAutoConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用名称
|
||||
*/
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
/**
|
||||
* 命名空间
|
||||
*/
|
||||
private String dNamespace;
|
||||
/**
|
||||
* 服务器地址
|
||||
*/
|
||||
private String serverAddr;
|
||||
|
||||
@Autowired
|
||||
private I18nMessageConfig messageConfig;
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
||||
|
||||
private static final String DEFAULT_NAMESPACE = "f591e849-daae-4d44-9704-163e1d638635";
|
||||
|
||||
@Primary
|
||||
@Bean(name = "messageSource")
|
||||
@DependsOn(value = "i18nMessageConfig")
|
||||
public ReloadableResourceBundleMessageSource messageSource() {
|
||||
String dirPath = ResourceUtils.FILE_URL_PREFIX + System.getProperty("user.dir");
|
||||
if (dirPath.endsWith(File.separator)) {
|
||||
dirPath = dirPath.substring(0, dirPath.length() - File.separator.length());
|
||||
}
|
||||
String path = dirPath + File.separator + messageConfig.getBaseFolder() + File.separator + messageConfig.getBasename();
|
||||
log.info("国际化配置内容:{}", JSONObject.toJSONString(messageConfig));
|
||||
log.info("国际化配置路径:{}", path);
|
||||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
messageSource.setBasename(path);
|
||||
messageSource.setDefaultEncoding(messageConfig.getEncoding());
|
||||
messageSource.setCacheMillis(messageConfig.getCacheMillis());
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user