123
This commit is contained in:
96
bashi-admin/src/main/java/com/bashi/config/I18nConfig.java
Normal file
96
bashi-admin/src/main/java/com/bashi/config/I18nConfig.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package com.bashi.config;
|
||||
|
||||
import com.bashi.dk.service.HomeSettingService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* i18n配置,用于处理语言本地化设置。
|
||||
*
|
||||
* @author liuyanqiang
|
||||
* @version 1.0.0
|
||||
* @since 2023/06/15 09:41
|
||||
**/
|
||||
@Configuration
|
||||
public class I18nConfig implements LocaleResolver {
|
||||
|
||||
@Autowired
|
||||
private HomeSettingService homeSettingService;
|
||||
|
||||
/**
|
||||
* 根据请求中的Accept-Language头部解析出语言设置。
|
||||
* 如果没有指定语言或语言设置不完整,则默认为中文设置。
|
||||
*
|
||||
* @param request HTTP请求对象
|
||||
* @return {@link Locale } 解析后的语言设置Locale对象
|
||||
* @author liuyanqiang
|
||||
* @since 2023/06/15 09:42
|
||||
**/
|
||||
@Override
|
||||
public Locale resolveLocale(HttpServletRequest request) {
|
||||
// 默认语言设置为中文
|
||||
String defaultLocal = homeSettingService.getHomeSetting().getDefaultLocal();
|
||||
Locale locale = null;
|
||||
// 从请求的头部获取Accept-Language标头,该标头指定了客户端期望的语言
|
||||
String language = request.getHeader("Accept-Language");
|
||||
String lang = request.getParameter("lang");
|
||||
if (StringUtils.isNotBlank(lang)) {
|
||||
language = lang;
|
||||
}
|
||||
if(StringUtils.isBlank(language)){
|
||||
language = defaultLocal;
|
||||
}
|
||||
// 检查请求的语言是否非空
|
||||
if (StringUtils.isNotBlank(language)) {
|
||||
// 将语言字符串拆分为语言和国家/地区代码
|
||||
String[] splitLanguage = language.split("_");
|
||||
// 检查是否有语言和国家/地区代码
|
||||
if (splitLanguage.length > 1) {
|
||||
// 根据语言和国家/地区代码创建新的Locale对象
|
||||
locale = new Locale(splitLanguage[0], splitLanguage[1]);
|
||||
}
|
||||
}
|
||||
if(locale == null){
|
||||
locale = Locale.CHINA;
|
||||
}
|
||||
// 返回解析得到的Locale对象作为解析后的语言设置
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置语言设置时的回调方法。
|
||||
* 在此实现中,不执行任何操作,忽略对语言设置的更改。
|
||||
* @param request HTTP请求对象
|
||||
* @param response HTTP响应对象
|
||||
* @param locale 语言设置的Locale对象
|
||||
* @author liuyanqiang
|
||||
* @since 2023/06/15 09:43
|
||||
**/
|
||||
@Override
|
||||
public void setLocale(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Locale locale) {
|
||||
// 此方法不执行任何操作,忽略对语言设置的更改
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建LocaleResolver bean,用于处理语言设置。
|
||||
*
|
||||
* @return {@link LocaleResolver }
|
||||
* @author liuyanqiang
|
||||
* @since 2023/06/15 09:44
|
||||
**/
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
// 返回当前类作为LocaleResolver的实例
|
||||
return new I18nConfig();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ spring:
|
||||
# 主库数据源
|
||||
master:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:4306/dk?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true
|
||||
url: jdbc:mysql://124.222.254.188:4306/dk?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true
|
||||
username: dk
|
||||
password: dk123.com
|
||||
# 从库数据源
|
||||
@@ -66,13 +66,13 @@ spring:
|
||||
# redis 配置
|
||||
redis:
|
||||
# 地址
|
||||
host: localhost
|
||||
host: 124.222.254.188
|
||||
# 端口,默认为6379
|
||||
port: 6379
|
||||
port: 9379
|
||||
# 数据库索引
|
||||
database: 1
|
||||
# 密码
|
||||
password:
|
||||
password: 383200134
|
||||
# 连接超时时间
|
||||
timeout: 10s
|
||||
# 是否开启ssl
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
test=TestYuYan
|
||||
@@ -0,0 +1 @@
|
||||
test=测试语言
|
||||
@@ -4,22 +4,24 @@ package com.bashi.dk.controller.app;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.bashi.common.core.domain.AjaxResult;
|
||||
import com.bashi.common.utils.BeanConvertUtil;
|
||||
import com.bashi.common.utils.MessageUtils;
|
||||
import com.bashi.dk.domain.HomeSetting;
|
||||
import com.bashi.dk.domain.LoansSetting;
|
||||
import com.bashi.dk.dto.app.req.CalLoanReq;
|
||||
import com.bashi.dk.dto.app.resp.CalLoanResp;
|
||||
import com.bashi.dk.dto.app.resp.LoanUser;
|
||||
import com.bashi.dk.kit.CalLoanManager;
|
||||
import com.bashi.dk.service.HomeSettingService;
|
||||
import com.bashi.dk.service.LoansSettingService;
|
||||
import com.bashi.dk.util.Loan;
|
||||
import com.bashi.dk.util.PhoneRandomUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.aspectj.weaver.loadtime.Aj;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Random;
|
||||
|
||||
@@ -32,6 +34,20 @@ public class AppHomeController {
|
||||
private CalLoanManager calLoanManager;
|
||||
@Autowired
|
||||
private LoansSettingService loansSettingService;
|
||||
@Autowired
|
||||
private HomeSettingService homeSettingService;
|
||||
@GetMapping("/defaultLocal")
|
||||
@ApiOperation(value = "获取默认语言")
|
||||
public AjaxResult<String> defaultLocal(){
|
||||
HomeSetting homeSetting = homeSettingService.getHomeSetting();
|
||||
String defaultLocal = homeSetting.getDefaultLocal();
|
||||
if(StringUtils.isEmpty(defaultLocal)){
|
||||
defaultLocal = "zh_CN";
|
||||
}
|
||||
AjaxResult<String> success = AjaxResult.success();
|
||||
success.setData(defaultLocal);
|
||||
return success;
|
||||
}
|
||||
|
||||
@PostMapping("/calLoan")
|
||||
@ApiOperation(value = "计算每月还款")
|
||||
|
||||
@@ -35,6 +35,7 @@ public class HomeSetting implements Serializable {
|
||||
*/
|
||||
private String commonSeal;
|
||||
|
||||
private String defaultLocal;
|
||||
|
||||
private String chatUrl;
|
||||
@TableField(exist = false)
|
||||
|
||||
Reference in New Issue
Block a user