opt:1.新增国际化准备工作。

This commit is contained in:
2026-02-04 09:37:08 +08:00
parent 67653cc200
commit 6c9c1c95e4
24 changed files with 474 additions and 108 deletions

View File

@@ -0,0 +1,20 @@
package org.nl.config;
/**
* @author dsh
* 2025/8/25
*/
public interface CallBack {
/**
* 回调执行方法
*/
void executor();
/**
* 本回调任务名称
* @return /
*/
default String getCallBackName() {
return Thread.currentThread().getId() + ":" + this.getClass().getName();
}
}

View File

@@ -29,8 +29,10 @@ public class CorsConfig implements WebMvcConfigurer {
FileProperties.ElPath path = properties.getPath();
String qrcodeUtl = "file:" + path.getQrcode().replace("\\","/");
String pathUtl = "file:" + path.getPath().replace("\\","/");
String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
registry.addResourceHandler("/qrcode/**").addResourceLocations(qrcodeUtl).setCachePeriod(0);
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
}

View File

@@ -0,0 +1,20 @@
package org.nl.config;
import java.io.Serializable;
import java.util.HashMap;
/*
* @author ZZQ
* @Date 2022/11/29 2:55 下午
*/
public class MapOf implements Serializable {
public static <K> HashMap of(K... key){
HashMap map = new HashMap<>();
for (int i = 0; i < (key.length & ~1); i=i+2) {
map.put(key[i],key[i+1]);
}
return map;
}
}

View File

@@ -0,0 +1,141 @@
package org.nl.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import java.util.ArrayList;
import java.util.List;
/**
* @author dsh
* 2025/8/25
*/
@Slf4j
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext = null;
//数据库连接的bean名字
public static String dataSourceBeanName="dataSource";
private static final List<CallBack> CALL_BACKS = new ArrayList<>();
private static boolean addCallback = true;
/**
* 针对 某些初始化方法在SpringContextHolder 未初始化时 提交回调方法。
* 在SpringContextHolder 初始化后,进行回调使用
*
* @param callBack 回调函数
*/
public synchronized static void addCallBacks(CallBack callBack) {
if (addCallback) {
SpringContextHolder.CALL_BACKS.add(callBack);
} else {
log.warn("CallBack{} 已无法添加!立即执行", callBack.getCallBackName());
callBack.executor();
}
}
public static ApplicationContext getApplicationContext() {
try {
} catch (Exception e) {
e.printStackTrace();
}
return applicationContext;
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}
/**
* 获取SpringBoot 配置信息
*
* @param property 属性key
* @param defaultValue 默认值
* @param requiredType 返回类型
* @return /
*/
public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) {
T result = defaultValue;
try {
result = getBean(Environment.class).getProperty(property, requiredType);
} catch (Exception ignored) {}
return result;
}
/**
* 获取SpringBoot 配置信息
*
* @param property 属性key
* @return /
*/
public static String getProperties(String property) {
return getProperties(property, null, String.class);
}
/**
* 获取SpringBoot 配置信息
*
* @param property 属性key
* @param requiredType 返回类型
* @return /
*/
public static <T> T getProperties(String property, Class<T> requiredType) {
return getProperties(property, null, requiredType);
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" +
".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder.");
}
}
/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
private static void clearHolder() {
log.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext = null;
}
@Override
public void destroy() {
SpringContextHolder.clearHolder();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringContextHolder.applicationContext != null) {
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
}
SpringContextHolder.applicationContext = applicationContext;
if (addCallback) {
for (CallBack callBack : SpringContextHolder.CALL_BACKS) {
callBack.executor();
}
CALL_BACKS.clear();
}
SpringContextHolder.addCallback = false;
}
}

View File

@@ -0,0 +1,44 @@
package org.nl.config.language;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.nl.config.MapOf;
import org.springframework.web.servlet.LocaleResolver;
import java.util.Locale;
import java.util.Map;
/**
* @author dsh
* 2025/8/25
*/
public class InitLocaleResolver implements LocaleResolver {
public static Map<String,String> Language_Country = MapOf.of("vi","vi-VN","id","in-ID","in","in-ID","en-US,en;q=0.9","en-US","en","en-US","zh","zh-CN","ko","ko-KR");
public static String language = "";
@Override
public Locale resolveLocale(HttpServletRequest request) {
String header = request.getHeader("Accept-Language");
if (StrUtil.isNotEmpty(header)){
String lang = Language_Country.get(header);
language = lang;
if (StrUtil.isNotEmpty(lang)){
String[] l = lang.split("-");
//印尼的ISO标准国家代码为id-ID
return new Locale(l[0], l[1]);
}
}
return Locale.getDefault();
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
public String getLanguage(){
return language;
}
}

View File

@@ -0,0 +1,23 @@
package org.nl.config.language;
import cn.hutool.core.util.StrUtil;
import org.nl.config.SpringContextHolder;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* @author dsh
* 2025/8/25
*/
public class LangProcess {
public static String msg(String code,String...args){
MessageSource bean = SpringContextHolder.getBean(MessageSource.class);
if (StrUtil.isEmpty(code)){
return " ";
}
String message = bean.getMessage(code, args, LocaleContextHolder.getLocale());
return message;
}
}

View File

@@ -0,0 +1,21 @@
package org.nl.config.language;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.nl.config.MapOf;
import java.util.Map;
/**
* @author dsh
* 2025/8/27
*/
@Getter
@AllArgsConstructor
public class RcsLang {
public static Map<String,String> Language_Country = MapOf.of("en","EN","zh","ZH");
public static String getRcsLanguage(String language) {
return Language_Country.get(language);
}
}

View File

@@ -2,6 +2,7 @@ package org.nl.response;
import lombok.Builder;
import lombok.Data;
import org.nl.config.language.LangProcess;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
@@ -35,7 +36,7 @@ public class WebResponse<T> {
*/
public static WebResponse requestOk() {
return WebResponse.builder()
.message("成功")
.message(LangProcess.msg("successful"))
.code(OK.value())
.build();
}
@@ -57,7 +58,7 @@ public class WebResponse<T> {
*/
public static <T> WebResponse requestParamOk(T data) {
return WebResponse.builder()
.message("成功")
.message(LangProcess.msg("successful"))
.data(data)
.code(OK.value())
.build();