From 54b77a714f6eae8d7652eb304789c92656f29e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 3 Jul 2022 00:12:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=94=AF=E6=8C=81=E5=A4=9A=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/code.go b/code.go index b21e827..1d5d07c 100644 --- a/code.go +++ b/code.go @@ -13,30 +13,34 @@ import ( ) var ( - // CodeTable 响应状态码和文案的映射表 + // CodeTable 响应状态码和文案的映射表, 文案信心 语言类型 => 对应语言的描述 // // Author : go_developer@163.com<白茶清欢> // // Date : 20:53 2022/6/25 - codeTable map[interface{}]string + codeTable map[interface{}]map[string]string ) func init() { // 规避没调用 InitCodeTable 导致空指针 - codeTable = make(map[interface{}]string) + codeTable = make(map[interface{}]map[string]string) } -// InitCodeTable 初始化码表, 同时只指定代表业务成功的状态码 +// InitCodeTable 初始化码表, 同时只指定代表业务成功的状态码以及使用的语言 // // Author : go_developer@163.com<白茶清欢> // // Date : 20:55 2022/6/25 -func InitCodeTable(table map[interface{}]string, convertDefaultSuccessCode interface{}) { +func InitCodeTable(table map[interface{}]map[string]string, convertDefaultSuccessCode interface{}, convertDefaultLanguage string) { codeTable = table if nil == convertDefaultSuccessCode { convertDefaultSuccessCode = 0 } + if len(convertDefaultLanguage) == 0 { + convertDefaultLanguage = defaultLanguage + } defaultSuccessCode = convertDefaultSuccessCode + defaultLanguage = convertDefaultLanguage } var ( @@ -46,6 +50,8 @@ var ( defaultHttpCode = http.StatusOK // defaultSuccessCode 默认代表成功的状态码 defaultSuccessCode interface{} + // defaultLanguage 默认的语言 + defaultLanguage = "zh" ) // MessageWithoutCode 关闭在文案后追加状态码 @@ -69,9 +75,9 @@ func getMessage(code interface{}) string { return fmt.Sprintf("未知错误【%v】", codeTable) } if messageWithCode { - return fmt.Sprintf(message+"【%v】", codeTable) + return fmt.Sprintf(message[defaultLanguage]+"【%v】", code) } - return message + return message[defaultLanguage] } // GetMessage 获取消息信息