Skip to content

Commit f574fd0

Browse files
committed
插件支持国际化
1 parent c25dcca commit f574fd0

File tree

8 files changed

+254
-8
lines changed

8 files changed

+254
-8
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| v1.1.6 | 优化日志打印 |
4141
| v1.1.7 | 新增kubernetes构建资源相关api |
4242
| v1.1.8 | 增加fileGateway |
43+
| v1.1.9 | 插件支持国际化,增加MessageUtil、I18nUtil工具类,AtomResult类增加setErrorInfo方法 |
4344

4445
[TOC]
4546

@@ -131,6 +132,25 @@ java实体对象如下:
131132
}
132133
```
133134

135+
​ 对外提供的方法如下:
136+
137+
```
138+
/**
139+
* 设置错误信息
140+
*
141+
* @param status 执行结果状态
142+
* @param errorCode 错误码
143+
* @param errorType 错误类型
144+
* @param params 替换错误描述信息占位符的参数数组
145+
*/
146+
public void setErrorInfo(Status status, Integer errorCode, ErrorType errorType, String[] params) {
147+
this.status = status;
148+
this.errorCode = errorCode;
149+
this.errorType = errorType.getNum();
150+
this.message =
151+
MessageUtil.getMessageByLocale(errorCode.toString(), I18nUtil.getLanguage(), params);
152+
}
153+
```
134154

135155

136156
##### 3、AtomContext.java (流水线插件上下文类)
@@ -444,7 +464,84 @@ for (i in 1..100000000) {
444464
445465
```
446466

467+
##### 8、支持插件国际化的工具类MessageUtil.java
468+
469+
​ 包含的主要方法如下:
470+
471+
```
472+
/**
473+
* 根据语言环境获取对应的描述信息
474+
*
475+
* @param messageCode 消息标识
476+
* @param language 语言信息
477+
* @return 描述信息
478+
*/
479+
public static String getMessageByLocale(String messageCode, String language);
480+
481+
/**
482+
* 根据语言环境获取对应的描述信息
483+
*
484+
* @param messageCode 消息标识
485+
* @param language 语言信息
486+
* @param defaultMessage 默认信息
487+
* @return 描述信息
488+
*/
489+
public static String getMessageByLocale(String messageCode, String language, String defaultMessage);
490+
491+
/**
492+
* 根据语言环境获取对应的描述信息
493+
*
494+
* @param messageCode 消息标识
495+
* @param language 语言信息
496+
* @param params 替换描述信息占位符的参数数组
497+
* @return 描述信息
498+
*/
499+
public static String getMessageByLocale(String messageCode, String language, String[] params);
500+
501+
/**
502+
* 根据语言环境获取对应的描述信息
503+
*
504+
* @param messageCode 消息标识
505+
* @param language 语言信息
506+
* @param params 替换描述信息占位符的参数数组
507+
* @param defaultMessage 默认信息
508+
* @return 描述信息
509+
*/
510+
public static String getMessageByLocale(String messageCode, String language, String[] params, String defaultMessage);
511+
512+
/**
513+
* 根据语言环境获取对应的描述信息
514+
*
515+
* @param messageCode 消息标识
516+
* @param language 语言信息
517+
* @param params 替换描述信息占位符的参数数组
518+
* @param baseName 基础资源名称
519+
* @param defaultMessage 默认信息
520+
* @return 描述信息
521+
*/
522+
public static String getMessageByLocale(
523+
String messageCode,
524+
String language,
525+
String[] params,
526+
String baseName,
527+
String defaultMessage
528+
);
529+
530+
```
447531

532+
##### 9、支持插件国际化的工具类I18nUtil.java
533+
534+
​ 包含的主要方法如下:
535+
536+
```
537+
/**
538+
* 获取插件执行时语言信息
539+
*
540+
* @return 插件执行时语言信息
541+
*/
542+
public static String getLanguage();
543+
544+
```
448545

449546

450547
## 二、SDK提供的服务介绍

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.tencent.devops.ci-plugins</groupId>
88
<artifactId>java-plugin-sdk</artifactId>
9-
<version>1.1.8</version>
9+
<version>1.1.9</version>
1010

1111
<inceptionYear>2018-2118</inceptionYear>
1212
<description>bk-ci pipeline plugins sdk for java</description>

src/main/java/com/tencent/bk/devops/atom/api/impl/CredentialApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Result<Map<String, String>> getCredential(String credentialId){
2525
Request request = super.buildGet("/ticket/api/build/credentials/" + credentialId + "/detail");
2626
String responseContent = null;
2727
try {
28-
responseContent = super.request(request,"获取凭证信息失败");
28+
responseContent = super.request(request,"get credential fail!");
2929
} catch (IOException e) {
3030
logger.error("getCredential throw Exception", e);
3131
}

src/main/java/com/tencent/bk/devops/atom/pojo/AtomResult.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import com.google.common.collect.Maps;
55
import com.tencent.bk.devops.atom.common.Status;
66
import com.tencent.bk.devops.atom.pojo.quality.QualityValue;
7+
import com.tencent.bk.devops.atom.utils.I18nUtil;
8+
import com.tencent.bk.devops.atom.utils.MessageUtil;
9+
import com.tencent.bk.devops.plugin.pojo.ErrorType;
710
import lombok.Data;
811

912
import java.util.Map;
@@ -31,6 +34,7 @@
3134
* }
3235
* }
3336
* }
37+
*
3438
* @version 1.0
3539
*/
3640
@Data
@@ -84,4 +88,19 @@ public class AtomResult {
8488
@JsonProperty("monitorData")
8589
private MonitorData monitorData;
8690

91+
92+
/**
93+
* 设置错误信息
94+
*
95+
* @param status 执行结果状态
96+
* @param errorCode 错误码
97+
* @param errorType 错误类型
98+
* @param params 替换错误描述信息占位符的参数数组
99+
*/
100+
public void setErrorInfo(Status status, Integer errorCode, ErrorType errorType, String[] params) {
101+
this.status = status;
102+
this.errorCode = errorCode;
103+
this.errorType = errorType.getNum();
104+
this.message = MessageUtil.getMessageByLocale(errorCode.toString(), I18nUtil.getLanguage(), params);
105+
}
87106
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.tencent.bk.devops.atom.utils;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class I18nUtil {
6+
7+
/**
8+
* 获取插件执行时语言信息
9+
*
10+
* @return 插件执行时语言信息
11+
*/
12+
public static String getLanguage() {
13+
String language = System.getenv("BK_CI_LOCALE_LANGUAGE");
14+
if (StringUtils.isEmpty(language)) {
15+
language = "zh_CN";
16+
}
17+
return language;
18+
}
19+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.tencent.bk.devops.atom.utils;
2+
3+
import kotlin.text.Charsets;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.text.MessageFormat;
8+
import java.util.Locale;
9+
import java.util.ResourceBundle;
10+
11+
public class MessageUtil {
12+
13+
private static final Logger logger = LoggerFactory.getLogger(MessageUtil.class);
14+
private static final String DEFAULT_BASE_NAME = "i18n/message";
15+
16+
/**
17+
* 根据语言环境获取对应的描述信息
18+
*
19+
* @param messageCode 消息标识
20+
* @param language 语言信息
21+
* @return 描述信息
22+
*/
23+
public static String getMessageByLocale(String messageCode, String language) {
24+
return getMessageByLocale(messageCode, language, null, DEFAULT_BASE_NAME, null);
25+
}
26+
27+
/**
28+
* 根据语言环境获取对应的描述信息
29+
*
30+
* @param messageCode 消息标识
31+
* @param language 语言信息
32+
* @param defaultMessage 默认信息
33+
* @return 描述信息
34+
*/
35+
public static String getMessageByLocale(String messageCode, String language, String defaultMessage) {
36+
return getMessageByLocale(messageCode, language, null, DEFAULT_BASE_NAME, defaultMessage);
37+
}
38+
39+
/**
40+
* 根据语言环境获取对应的描述信息
41+
*
42+
* @param messageCode 消息标识
43+
* @param language 语言信息
44+
* @param params 替换描述信息占位符的参数数组
45+
* @return 描述信息
46+
*/
47+
public static String getMessageByLocale(String messageCode, String language, String[] params) {
48+
return getMessageByLocale(messageCode, language, params, DEFAULT_BASE_NAME, null);
49+
}
50+
51+
/**
52+
* 根据语言环境获取对应的描述信息
53+
*
54+
* @param messageCode 消息标识
55+
* @param language 语言信息
56+
* @param params 替换描述信息占位符的参数数组
57+
* @param defaultMessage 默认信息
58+
* @return 描述信息
59+
*/
60+
public static String getMessageByLocale(
61+
String messageCode,
62+
String language,
63+
String[] params,
64+
String defaultMessage
65+
) {
66+
return getMessageByLocale(messageCode, language, params, DEFAULT_BASE_NAME, defaultMessage);
67+
}
68+
69+
/**
70+
* 根据语言环境获取对应的描述信息
71+
*
72+
* @param messageCode 消息标识
73+
* @param language 语言信息
74+
* @param params 替换描述信息占位符的参数数组
75+
* @param baseName 基础资源名称
76+
* @param defaultMessage 默认信息
77+
* @return 描述信息
78+
*/
79+
public static String getMessageByLocale(
80+
String messageCode,
81+
String language,
82+
String[] params,
83+
String baseName,
84+
String defaultMessage
85+
) {
86+
// 通过resourceBundle获取对应语言的描述信息
87+
String message = null;
88+
try {
89+
String[] parts = language.split("_");
90+
Locale localeObj = new Locale(language);
91+
if (parts.length > 1) {
92+
localeObj = new Locale(parts[0], parts[1]);
93+
}
94+
// 根据locale和baseName生成resourceBundle对象
95+
ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, localeObj);
96+
// 通过resourceBundle获取对应语言的描述信息
97+
message = new String(resourceBundle.getString(messageCode).getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
98+
} catch (Throwable ignored) {
99+
logger.warn("Fail to get i18nMessage of messageCode[" + messageCode + "]", ignored);
100+
}
101+
if (null != params && null != message) {
102+
MessageFormat mf = new MessageFormat(message);
103+
// 根据参数动态替换状态码描述里的占位符
104+
message = mf.format(params);
105+
}
106+
if (message == null) {
107+
message = defaultMessage;
108+
}
109+
return message;
110+
}
111+
}

src/main/kotlin/com/tencent/bk/devops/plugin/api/impl/KubernetesBuildApi.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class KubernetesBuildApi : BaseApi() {
2929
val requestBody = RequestBody.create(JSON_CONTENT_TYPE, JsonUtil.toJson(dispatchJobReq))
3030

3131
val request = buildPost(path, requestBody, mutableMapOf("X-DEVOPS-UID" to getUserId()))
32-
val responseContent = request(request, "kubernetes job失败")
32+
val responseContent = request(request, "kubernetes job fail")
3333
logger.debug("create kubernetes job response: $responseContent")
3434

3535
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchTaskResp?>>() {})
@@ -38,23 +38,23 @@ class KubernetesBuildApi : BaseApi() {
3838
fun getJobStatus(jobName: String): Result<DispatchBuildStatusResp> {
3939
val path = "/dispatch-kubernetes/api/build/job/" + jobName + "/status"
4040
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId()))
41-
val responseContent = request(request, "获取job状态失败")
41+
val responseContent = request(request, "failed to get job status")
4242
logger.debug("get kubernetes job status response: $responseContent")
4343
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchBuildStatusResp>>() {})
4444
}
4545

4646
fun getJobLogs(jobName: String, sinceTime: Int): Result<DispatchJobLogResp> {
4747
val path = "/dispatch-kubernetes/api/build/job/" + jobName + "/logs?sinceTime=" + sinceTime
4848
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId()))
49-
val responseContent = request(request, "获取job日志失败")
49+
val responseContent = request(request, "failed to get job log")
5050
logger.debug("get kubernetes job logs response: $responseContent")
5151
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchJobLogResp>>() {})
5252
}
5353

5454
fun getTask(taskId: String): Result<DispatchBuildStatusResp> {
5555
val path = "/dispatch-kubernetes/api/build/task/status?taskId=" + taskId
5656
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId()))
57-
val responseContent = request(request, "获取task信息失败")
57+
val responseContent = request(request, "get task info fail")
5858
logger.debug("get kubernetes task response: $responseContent")
5959
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchBuildStatusResp>>() {})
6060
}
@@ -65,7 +65,7 @@ class KubernetesBuildApi : BaseApi() {
6565
val requestBody = RequestBody.create(JSON_CONTENT_TYPE, JsonUtil.toJson(dispatchBuildImageReq))
6666

6767
val request = buildPost(path, requestBody, mutableMapOf("X-DEVOPS-UID" to getUserId()))
68-
val responseContent = request(request, "kubernetes docker build失败")
68+
val responseContent = request(request, "kubernetes docker build fail")
6969
logger.debug("docker build response: $responseContent")
7070

7171
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchTaskResp?>>() {})

src/main/kotlin/com/tencent/bk/devops/plugin/api/impl/SensitiveConfApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SensitiveConfApi : BaseApi() {
1616
fun getAtomSensitiveConf(atomCode: String): Result<List<SensitiveConfResp>?> {
1717
val path = "/store/api/build/store/sensitiveConf/types/ATOM/codes/$atomCode"
1818
val request = buildGet(path)
19-
val responseContent = retryRequest(request, "获取插件私有配置失败")
19+
val responseContent = retryRequest(request, "failed to get plugin private configuration!")
2020
return JsonUtil.to(responseContent, object : TypeReference<Result<List<SensitiveConfResp>?>>() {})
2121
}
2222
}

0 commit comments

Comments
 (0)