Skip to content

Commit

Permalink
插件支持国际化
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Jul 11, 2023
1 parent c25dcca commit f574fd0
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 8 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
| v1.1.6 | 优化日志打印 |
| v1.1.7 | 新增kubernetes构建资源相关api |
| v1.1.8 | 增加fileGateway |
| v1.1.9 | 插件支持国际化,增加MessageUtil、I18nUtil工具类,AtomResult类增加setErrorInfo方法 |

[TOC]

Expand Down Expand Up @@ -131,6 +132,25 @@ java实体对象如下:
}
```

​ 对外提供的方法如下:

```
/**
* 设置错误信息
*
* @param status 执行结果状态
* @param errorCode 错误码
* @param errorType 错误类型
* @param params 替换错误描述信息占位符的参数数组
*/
public void setErrorInfo(Status status, Integer errorCode, ErrorType errorType, String[] params) {
this.status = status;
this.errorCode = errorCode;
this.errorType = errorType.getNum();
this.message =
MessageUtil.getMessageByLocale(errorCode.toString(), I18nUtil.getLanguage(), params);
}
```


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

##### 8、支持插件国际化的工具类MessageUtil.java

​ 包含的主要方法如下:

```
/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language);
/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language, String defaultMessage);
/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language, String[] params);
/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language, String[] params, String defaultMessage);
/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @param baseName 基础资源名称
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(
String messageCode,
String language,
String[] params,
String baseName,
String defaultMessage
);
```

##### 9、支持插件国际化的工具类I18nUtil.java

​ 包含的主要方法如下:

```
/**
* 获取插件执行时语言信息
*
* @return 插件执行时语言信息
*/
public static String getLanguage();
```


## 二、SDK提供的服务介绍
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.tencent.devops.ci-plugins</groupId>
<artifactId>java-plugin-sdk</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>

<inceptionYear>2018-2118</inceptionYear>
<description>bk-ci pipeline plugins sdk for java</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Result<Map<String, String>> getCredential(String credentialId){
Request request = super.buildGet("/ticket/api/build/credentials/" + credentialId + "/detail");
String responseContent = null;
try {
responseContent = super.request(request,"获取凭证信息失败");
responseContent = super.request(request,"get credential fail!");
} catch (IOException e) {
logger.error("getCredential throw Exception", e);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/tencent/bk/devops/atom/pojo/AtomResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.common.collect.Maps;
import com.tencent.bk.devops.atom.common.Status;
import com.tencent.bk.devops.atom.pojo.quality.QualityValue;
import com.tencent.bk.devops.atom.utils.I18nUtil;
import com.tencent.bk.devops.atom.utils.MessageUtil;
import com.tencent.bk.devops.plugin.pojo.ErrorType;
import lombok.Data;

import java.util.Map;
Expand Down Expand Up @@ -31,6 +34,7 @@
* }
* }
* }
*
* @version 1.0
*/
@Data
Expand Down Expand Up @@ -84,4 +88,19 @@ public class AtomResult {
@JsonProperty("monitorData")
private MonitorData monitorData;


/**
* 设置错误信息
*
* @param status 执行结果状态
* @param errorCode 错误码
* @param errorType 错误类型
* @param params 替换错误描述信息占位符的参数数组
*/
public void setErrorInfo(Status status, Integer errorCode, ErrorType errorType, String[] params) {
this.status = status;
this.errorCode = errorCode;
this.errorType = errorType.getNum();
this.message = MessageUtil.getMessageByLocale(errorCode.toString(), I18nUtil.getLanguage(), params);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/tencent/bk/devops/atom/utils/I18nUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tencent.bk.devops.atom.utils;

import org.apache.commons.lang3.StringUtils;

public class I18nUtil {

/**
* 获取插件执行时语言信息
*
* @return 插件执行时语言信息
*/
public static String getLanguage() {
String language = System.getenv("BK_CI_LOCALE_LANGUAGE");
if (StringUtils.isEmpty(language)) {
language = "zh_CN";
}
return language;
}
}
111 changes: 111 additions & 0 deletions src/main/java/com/tencent/bk/devops/atom/utils/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.tencent.bk.devops.atom.utils;

import kotlin.text.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class MessageUtil {

private static final Logger logger = LoggerFactory.getLogger(MessageUtil.class);
private static final String DEFAULT_BASE_NAME = "i18n/message";

/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language) {
return getMessageByLocale(messageCode, language, null, DEFAULT_BASE_NAME, null);
}

/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language, String defaultMessage) {
return getMessageByLocale(messageCode, language, null, DEFAULT_BASE_NAME, defaultMessage);
}

/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @return 描述信息
*/
public static String getMessageByLocale(String messageCode, String language, String[] params) {
return getMessageByLocale(messageCode, language, params, DEFAULT_BASE_NAME, null);
}

/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(
String messageCode,
String language,
String[] params,
String defaultMessage
) {
return getMessageByLocale(messageCode, language, params, DEFAULT_BASE_NAME, defaultMessage);
}

/**
* 根据语言环境获取对应的描述信息
*
* @param messageCode 消息标识
* @param language 语言信息
* @param params 替换描述信息占位符的参数数组
* @param baseName 基础资源名称
* @param defaultMessage 默认信息
* @return 描述信息
*/
public static String getMessageByLocale(
String messageCode,
String language,
String[] params,
String baseName,
String defaultMessage
) {
// 通过resourceBundle获取对应语言的描述信息
String message = null;
try {
String[] parts = language.split("_");
Locale localeObj = new Locale(language);
if (parts.length > 1) {
localeObj = new Locale(parts[0], parts[1]);
}
// 根据locale和baseName生成resourceBundle对象
ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, localeObj);
// 通过resourceBundle获取对应语言的描述信息
message = new String(resourceBundle.getString(messageCode).getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
} catch (Throwable ignored) {
logger.warn("Fail to get i18nMessage of messageCode[" + messageCode + "]", ignored);
}
if (null != params && null != message) {
MessageFormat mf = new MessageFormat(message);
// 根据参数动态替换状态码描述里的占位符
message = mf.format(params);
}
if (message == null) {
message = defaultMessage;
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class KubernetesBuildApi : BaseApi() {
val requestBody = RequestBody.create(JSON_CONTENT_TYPE, JsonUtil.toJson(dispatchJobReq))

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

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

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

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

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

return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchTaskResp?>>() {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SensitiveConfApi : BaseApi() {
fun getAtomSensitiveConf(atomCode: String): Result<List<SensitiveConfResp>?> {
val path = "/store/api/build/store/sensitiveConf/types/ATOM/codes/$atomCode"
val request = buildGet(path)
val responseContent = retryRequest(request, "获取插件私有配置失败")
val responseContent = retryRequest(request, "failed to get plugin private configuration!")
return JsonUtil.to(responseContent, object : TypeReference<Result<List<SensitiveConfResp>?>>() {})
}
}

0 comments on commit f574fd0

Please sign in to comment.