4 changed files with 247 additions and 0 deletions
@ -0,0 +1,86 @@ |
|||
# 前端============================================== |
|||
# Logs |
|||
**/logs/ |
|||
**/*.log |
|||
**/npm-debug.log* |
|||
**/yarn-debug.log* |
|||
**/yarn-error.log* |
|||
**/pnpm-debug.log* |
|||
**/lerna-debug.log* |
|||
|
|||
**/node_modules/ |
|||
**/dist/ |
|||
**/*.local |
|||
**/package-lock.json |
|||
**/pnpm-lock.yaml |
|||
**/java-src/ |
|||
|
|||
# Editor directories and files |
|||
**/.vscode/* |
|||
!**/.vscode/extensions.json |
|||
**/.idea/ |
|||
**/*.suo |
|||
**/*.ntvs* |
|||
**/*.njsproj |
|||
**/*.sln |
|||
**/*.sw? |
|||
|
|||
**/test-results/ |
|||
**/playwright-report/ |
|||
|
|||
# 后端============================================== |
|||
# mac os 隐藏文件 |
|||
**/.DS_Store |
|||
|
|||
# 缓存缩略图文件 |
|||
**/Thumbs.db |
|||
|
|||
# gradle 文件夹 |
|||
**/.gradle/ |
|||
|
|||
# svn 文件夹 |
|||
**/.svn/ |
|||
|
|||
# cvs 文件夹及文件 |
|||
**/.checkstyle |
|||
**/.cvsignore |
|||
|
|||
# eclipse project 文件夹及文件 |
|||
**/.settings/ |
|||
**/.classpath |
|||
**/.project |
|||
|
|||
# idea project 文件夹及文件 |
|||
**/*.ipr |
|||
**/*.iws |
|||
**/*.iml |
|||
**/.shelf |
|||
|
|||
# eclipse 构建文件夹 |
|||
**/bin/ |
|||
|
|||
# idea 构建文件夹 |
|||
**/out/ |
|||
**/work/ |
|||
|
|||
# gradle 构建文件夹 |
|||
**/build/ |
|||
|
|||
# spring boot 框架应用运行时自动生成的文件夹 |
|||
**/app.*/config/ |
|||
**/app.*/logs/ |
|||
**/app.*/work/ |
|||
**/app.*/tmp/ |
|||
|
|||
# 日志文件夹及文件 |
|||
**/logs/ |
|||
**/*.log |
|||
|
|||
# 所有 jar 文件 |
|||
**/*.jar |
|||
|
|||
# 所有 jrebel 文件 |
|||
**/rebel.xml |
|||
|
|||
# 保留 gradle wrapper 的 jar 文件 |
|||
!gradle/wrapper/gradle-wrapper.jar |
@ -0,0 +1,35 @@ |
|||
package io.sc.platform.core.i18n; |
|||
|
|||
import io.sc.platform.core.enums.Language; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 平台消息源接口 |
|||
*/ |
|||
public interface PlatformMessageSource { |
|||
/** |
|||
* 重新加载多语言消息 |
|||
*/ |
|||
public void reload(); |
|||
|
|||
/** |
|||
* 获取所有多语言消息 |
|||
* @return 所有多语言消息 |
|||
*/ |
|||
public Map<Language,Map<String,String>> getMessages(); |
|||
|
|||
/** |
|||
* 获取指定消息键的所有多语言消息 |
|||
* @param messageKeys 消息键 |
|||
* @return 指定消息键的所有多语言消息 |
|||
*/ |
|||
public Map<Language,Map<String,String>> getMessages(String... messageKeys); |
|||
|
|||
/** |
|||
* 获取指定语言的所有多语言消息 |
|||
* @param language 语言 |
|||
* @return 指定语言的所有多语言消息 |
|||
*/ |
|||
public Map<String,String> getMessages(Language language); |
|||
} |
@ -0,0 +1,80 @@ |
|||
package io.sc.platform.core.i18n; |
|||
|
|||
import io.sc.platform.core.Environment; |
|||
import io.sc.platform.core.enums.Language; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.core.io.DefaultResourceLoader; |
|||
import org.springframework.core.io.Resource; |
|||
import org.springframework.core.io.ResourceLoader; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.util.*; |
|||
|
|||
public class PlatformMessageSourceLoader { |
|||
private static final Logger log = LoggerFactory.getLogger(PlatformMessageSourceLoader.class); |
|||
private Map<Language,Map<String,String>> messages; |
|||
private ResourceLoader resourceLoader =new DefaultResourceLoader(); |
|||
|
|||
private static class PlatformMessageSourceLoaderHolder{ |
|||
private static PlatformMessageSourceLoader instance =new PlatformMessageSourceLoader(); |
|||
} |
|||
|
|||
public static PlatformMessageSourceLoader getInstance(){ |
|||
return PlatformMessageSourceLoader.PlatformMessageSourceLoaderHolder.instance; |
|||
} |
|||
|
|||
private PlatformMessageSourceLoader(){} |
|||
|
|||
public Map<Language,Map<String,String>> getMessages(Set<String> baseNames) { |
|||
if(this.messages==null || Environment.getInstance().isRunningInDevelopment()) { |
|||
this.messages = loadMessages(getMessageSourcePropertiesFilePaths(baseNames)); |
|||
} |
|||
return messages; |
|||
} |
|||
|
|||
private Map<Language,Map<String,String>> loadMessages(Map<String,Language> filePaths) { |
|||
if(filePaths==null || filePaths.isEmpty()){ |
|||
return Collections.emptyMap(); |
|||
} |
|||
Map<Language,Map<String,String>> result =new LinkedHashMap<>(); |
|||
for(Map.Entry<String,Language> entry : filePaths.entrySet()){ |
|||
String path =entry.getKey(); |
|||
Language language =entry.getValue(); |
|||
result.putIfAbsent(language,new LinkedHashMap<String,String>(200)); |
|||
Map<String,String> map =result.get(language); |
|||
Resource rs = resourceLoader.getResource(path); |
|||
try (InputStream ins = rs.getInputStream()) { |
|||
Properties properties = new Properties(); |
|||
properties.load(ins); |
|||
Set<String> keys =properties.stringPropertyNames(); |
|||
for(String key : keys){ |
|||
map.put(key,properties.getProperty(key)); |
|||
} |
|||
} catch (IOException e) { |
|||
log.error("",e); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
private Map<String,Language> getMessageSourcePropertiesFilePaths(Set<String> baseNames){ |
|||
if(baseNames==null || baseNames.isEmpty()){ |
|||
return Collections.emptyMap(); |
|||
} |
|||
Map<String,Language> paths = new HashMap<>(Language.values().length*50); |
|||
for(String baseName : baseNames){ |
|||
for (Language language : Language.values()) { |
|||
String path ="classpath:/"; |
|||
path +=baseName.replace('.', '/'); |
|||
path +=language.equals(Language.en)?"" : "_" + language.toString(); |
|||
path +=".properties"; |
|||
if(resourceLoader.getResource(path).exists()) { |
|||
paths.put(path, language); |
|||
} |
|||
} |
|||
} |
|||
return paths; |
|||
} |
|||
} |
@ -0,0 +1,46 @@ |
|||
package io.sc.platform.core.i18n; |
|||
|
|||
import io.sc.platform.core.enums.Language; |
|||
import org.springframework.context.support.ResourceBundleMessageSource; |
|||
|
|||
import java.util.*; |
|||
|
|||
public class PlatformResourceBundleMessageSource extends ResourceBundleMessageSource implements PlatformMessageSource { |
|||
|
|||
@Override |
|||
public void reload() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public Map<Language, Map<String, String>> getMessages() { |
|||
return PlatformMessageSourceLoader.getInstance().getMessages(this.getBasenameSet()); |
|||
} |
|||
|
|||
@Override |
|||
public Map<Language, Map<String, String>> getMessages(String... messageKeys) { |
|||
Map<Language, Map<String, String>> messageMap =PlatformMessageSourceLoader.getInstance().getMessages(this.getBasenameSet()); |
|||
if(messageMap.isEmpty()){ |
|||
return Collections.emptyMap(); |
|||
} |
|||
Map<Language, Map<String, String>> result =new LinkedHashMap<>(); |
|||
for(Map.Entry<Language, Map<String, String>> entry : messageMap.entrySet()){ |
|||
Language language =entry.getKey(); |
|||
Map<String, String> messages =entry.getValue(); |
|||
result.putIfAbsent(language,new LinkedHashMap<>()); |
|||
Map<String, String> map =result.get(language); |
|||
for(String key : messageKeys){ |
|||
map.put(key,messages.get(key)); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public Map<String, String> getMessages(Language language) { |
|||
if(language==null){ |
|||
return Collections.emptyMap(); |
|||
} |
|||
return PlatformMessageSourceLoader.getInstance().getMessages(this.getBasenameSet()).get(language); |
|||
} |
|||
} |
Loading…
Reference in new issue