Skip to content

Commit bbca928

Browse files
committed
Support internationalization
1 parent e67d3da commit bbca928

59 files changed

Lines changed: 561 additions & 601 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package ai.chat2db.server.domain.core.impl;
22

3+
import java.sql.SQLException;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
import com.alibaba.druid.DbType;
10+
import com.alibaba.druid.sql.PagerUtils;
11+
import com.alibaba.druid.sql.SQLUtils;
12+
import com.alibaba.druid.sql.ast.SQLStatement;
13+
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
14+
import com.alibaba.druid.sql.parser.SQLParserUtils;
15+
316
import ai.chat2db.server.domain.api.param.DlCountParam;
417
import ai.chat2db.server.domain.api.param.DlExecuteParam;
518
import ai.chat2db.server.domain.api.param.SqlAnalyseParam;
@@ -11,28 +24,15 @@
1124
import ai.chat2db.server.domain.support.util.JdbcUtils;
1225
import ai.chat2db.server.tools.base.constant.EasyToolsConstant;
1326
import ai.chat2db.server.tools.base.excption.BusinessException;
14-
import ai.chat2db.server.tools.base.excption.DatasourceErrorEnum;
1527
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
1628
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
1729
import ai.chat2db.server.tools.common.util.EasyCollectionUtils;
18-
import com.alibaba.druid.DbType;
19-
import com.alibaba.druid.sql.PagerUtils;
20-
import com.alibaba.druid.sql.SQLUtils;
21-
import com.alibaba.druid.sql.ast.SQLStatement;
22-
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
23-
import com.alibaba.druid.sql.parser.SQLParserUtils;
2430
import lombok.extern.slf4j.Slf4j;
2531
import org.apache.commons.collections4.CollectionUtils;
2632
import org.apache.commons.lang3.BooleanUtils;
2733
import org.apache.commons.lang3.StringUtils;
2834
import org.springframework.stereotype.Service;
2935

30-
import java.sql.SQLException;
31-
import java.util.ArrayList;
32-
import java.util.Collections;
33-
import java.util.List;
34-
import java.util.Optional;
35-
3636
/**
3737
* @author moji
3838
* @version DataSourceCoreServiceImpl.java, v 0.1 2022年09月23日 15:51 moji Exp $
@@ -55,7 +55,7 @@ public ListResult<ExecuteResult> execute(DlExecuteParam param) {
5555
JdbcUtils.parse2DruidDbType(DbhubContext.getConnectInfo().getDbType());
5656
List<String> sqlList = SQLParserUtils.splitAndRemoveComment(param.getSql(), dbType);
5757
if (CollectionUtils.isEmpty(sqlList)) {
58-
throw new BusinessException(DatasourceErrorEnum.SQL_ANALYSIS_ERROR);
58+
throw new BusinessException("dataSource.sqlAnalysisError");
5959
}
6060

6161
List<ExecuteResult> result = new ArrayList<>();

chat2db-server/chat2db-server-start/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
<groupId>com.dtflys.forest</groupId>
101101
<artifactId>forest-spring-boot-starter</artifactId>
102102
</dependency>
103+
104+
<dependency>
105+
<groupId>org.zalando</groupId>
106+
<artifactId>logbook-spring-boot-starter</artifactId>
107+
</dependency>
103108
</dependencies>
104109
<build>
105110
<finalName>chat2db-server-start</finalName>

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/config/config/DbhubWebMvcConfigurer.java renamed to chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/config/config/Chat2dbWebMvcConfigurer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ai.chat2db.server.tools.common.model.Context;
1414
import ai.chat2db.server.tools.common.model.LoginUser;
1515
import ai.chat2db.server.tools.common.util.ContextUtils;
16+
import ai.chat2db.server.tools.common.util.I18nUtils;
1617
import cn.dev33.satoken.context.SaHolder;
1718
import cn.dev33.satoken.stp.StpUtil;
1819
import cn.dev33.satoken.util.SaFoxUtil;
@@ -27,16 +28,14 @@
2728
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
2829
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2930

30-
import static ai.chat2db.server.tools.common.enums.ErrorEnum.NEED_LOGGED_IN;
31-
3231
/**
3332
* web项目配置
3433
*
3534
* @author 是仪
3635
*/
3736
@Configuration
3837
@Slf4j
39-
public class DbhubWebMvcConfigurer implements WebMvcConfigurer {
38+
public class Chat2dbWebMvcConfigurer implements WebMvcConfigurer {
4039

4140
/**
4241
* api前缀
@@ -46,7 +45,8 @@ public class DbhubWebMvcConfigurer implements WebMvcConfigurer {
4645
/**
4746
* 全局放行的url
4847
*/
49-
private static final String[] FRONT_PERMIT_ALL = new String[] {"/favicon.ico", "/error", "/static/**", "/api/system"};
48+
private static final String[] FRONT_PERMIT_ALL = new String[] {"/favicon.ico", "/error", "/static/**",
49+
"/api/system"};
5050

5151
@Resource
5252
private UserService userService;
@@ -106,12 +106,12 @@ public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServl
106106
String path = SaHolder.getRequest().getRequestPath();
107107
if (path.startsWith(API_PREFIX)) {
108108
response.getWriter().println(JSON.toJSONString(
109-
ActionResult.fail(NEED_LOGGED_IN.getCode(), NEED_LOGGED_IN.getDescription())));
109+
ActionResult.fail("common.needLoggedIn", I18nUtils.getMessage("common.needLoggedIn"))));
110110
return false;
111-
//throw new NeedLoggedInBusinessException();
112111
} else {
113112
throw new RedirectBusinessException(
114-
"/login-a#/login?callback=" + SaFoxUtil.joinParam(request.getRequestURI(), request.getQueryString()));
113+
"/login-a#/login?callback=" + SaFoxUtil.joinParam(request.getRequestURI(),
114+
request.getQueryString()));
115115
}
116116
}
117117
return true;
@@ -125,6 +125,7 @@ public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServl
125125
.excludePathPatterns("/**/*-a")
126126
// _a结尾的统一放行
127127
.excludePathPatterns("/**/*_a");
128+
128129
}
129130

130131
private String buildHeaderString(HttpServletRequest request) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ai.chat2db.server.start.config.i18n;
2+
3+
import java.util.Locale;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
8+
9+
/**
10+
* Internationalized configuration
11+
*
12+
* @author Jiaju Zhuang
13+
*/
14+
@Configuration
15+
public class I18nConfig {
16+
17+
@Bean
18+
public AcceptHeaderLocaleResolver localeResolver() {
19+
final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
20+
resolver.setDefaultLocale(Locale.US);
21+
return resolver;
22+
}
23+
24+
}

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/config/i18n/InternationalizationConfig.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/exception/convertor/BindExceptionConvertor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package ai.chat2db.server.start.exception.convertor;
22

3-
import ai.chat2db.server.tools.base.excption.CommonErrorEnum;
43
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
5-
64
import org.springframework.validation.BindException;
75

86
/**
@@ -15,6 +13,6 @@ public class BindExceptionConvertor implements ExceptionConvertor<BindException>
1513
@Override
1614
public ActionResult convert(BindException exception) {
1715
String message = ExceptionConvertorUtils.buildMessage(exception.getBindingResult());
18-
return ActionResult.fail(CommonErrorEnum.PARAM_ERROR, message);
16+
return ActionResult.fail("common.paramError", message);
1917
}
2018
}

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/exception/convertor/BusinessExceptionConvertor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ai.chat2db.server.tools.base.excption.BusinessException;
44
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
5+
import ai.chat2db.server.tools.common.util.I18nUtils;
56

67
/**
78
* BusinessException
@@ -12,6 +13,6 @@ public class BusinessExceptionConvertor implements ExceptionConvertor<BusinessEx
1213

1314
@Override
1415
public ActionResult convert(BusinessException exception) {
15-
return ActionResult.fail(exception.getCode(), exception.getMessage());
16+
return ActionResult.fail(exception.getCode(), I18nUtils.getMessage(exception.getCode()));
1617
}
1718
}

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/exception/convertor/DefaultExceptionConvertor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ai.chat2db.server.start.exception.convertor;
22

3-
import ai.chat2db.server.tools.base.excption.CommonErrorEnum;
43
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
4+
import ai.chat2db.server.tools.common.util.I18nUtils;
55

66
/**
77
* 默认的异常处理
@@ -13,6 +13,6 @@ public class DefaultExceptionConvertor implements ExceptionConvertor<Throwable>
1313

1414
@Override
1515
public ActionResult convert(Throwable exception) {
16-
return ActionResult.fail(CommonErrorEnum.COMMON_SYSTEM_ERROR);
16+
return ActionResult.fail("common.systemError", I18nUtils.getMessage("common.systemError"));
1717
}
1818
}

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/exception/convertor/ExceptionConvertorUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import ai.chat2db.server.tools.base.constant.SymbolConstant;
66

7+
import ai.chat2db.server.tools.common.util.I18nUtils;
78
import org.springframework.util.CollectionUtils;
89
import org.springframework.validation.BindingResult;
910
import org.springframework.validation.ObjectError;
@@ -29,7 +30,7 @@ public static String buildMessage(BindingResult result) {
2930

3031
int index = 1;
3132
StringBuilder msg = new StringBuilder();
32-
msg.append("请检查以下信息:");
33+
msg.append(I18nUtils.getMessage("common.paramCheckError"));
3334
for (ObjectError e : errors) {
3435
msg.append(index++);
3536
// 得到错误消息

chat2db-server/chat2db-server-start/src/main/java/ai/chat2db/server/start/exception/convertor/MaxUploadSizeExceededExceptionConvertor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package ai.chat2db.server.start.exception.convertor;
22

3-
import ai.chat2db.server.tools.base.excption.CommonErrorEnum;
43
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
54

5+
import ai.chat2db.server.tools.common.util.I18nUtils;
66
import org.springframework.web.multipart.MaxUploadSizeExceededException;
77

88
/**
@@ -14,6 +14,6 @@ public class MaxUploadSizeExceededExceptionConvertor implements ExceptionConvert
1414

1515
@Override
1616
public ActionResult convert(MaxUploadSizeExceededException exception) {
17-
return ActionResult.fail(CommonErrorEnum.MAX_UPLOAD_SIZE);
17+
return ActionResult.fail("common.maxUploadSize", I18nUtils.getMessage("common.maxUploadSize"));
1818
}
1919
}

0 commit comments

Comments
 (0)