Skip to content

Commit 97390ce

Browse files
committed
refactor(lang): move the language trans into a structure
1 parent 689aa1d commit 97390ce

2 files changed

Lines changed: 79 additions & 69 deletions

File tree

commit-msg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func logAndExit(state MsgState, v ...interface{}) {
1616
log.Fatalf(state.Hint(), v...)
1717
} else {
1818
log.Printf(state.Hint(), v...)
19-
log.Fatalf(RuleHint, strings.Join(TypeList[:], ", "))
19+
log.Fatalf(Lang.Rule, Types)
2020
}
2121
}
2222

@@ -55,7 +55,7 @@ func checkType(type_ string) {
5555
return
5656
}
5757
}
58-
logAndExit(WrongType, type_, strings.Join(TypeList[:], ", "))
58+
logAndExit(WrongType, type_, Types)
5959
}
6060

6161
func checkHeader(header string) {

config.go

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"os"
6+
"strings"
67
)
78

89
const (
@@ -17,6 +18,11 @@ type GlobalConfig struct {
1718
LineLimit int
1819
}
1920

21+
type LangPack struct {
22+
HintList []string
23+
Rule string
24+
}
25+
2026
type MsgState int
2127

2228
const (
@@ -39,10 +45,9 @@ const (
3945
)
4046

4147
var (
42-
Config *GlobalConfig
43-
RuleHint string
44-
stateHint []string
45-
TypeList = [...]string{
48+
Config *GlobalConfig
49+
Lang *LangPack
50+
TypeList = [...]string{
4651
"feat", // new feature 新功能
4752
"fix", // fix bug 修复
4853
"docs", // documentation 文档
@@ -54,64 +59,13 @@ var (
5459
"revert", // 撤销以前的 commit
5560
"Revert", // 有些工具生成的 revert 首字母大写
5661
}
57-
enRule = `Commit message rule as follow:
58-
<type>(<scope>): <subject>
59-
// empty line
60-
<body>
61-
// empty line
62-
<footer>
63-
64-
(<scope>), <body> and <footer> are optional
65-
<type> must be one of %s
66-
more specific instructions, please refer to: https://github.com/JayceChant/commit-msg.go`
67-
68-
zhCnRule = `提交信息规范如下:
69-
<type>(<scope>): <subject>
70-
// 空行
71-
<body>
72-
// 空行
73-
<footer>
74-
75-
(<scope>), <body> 和 <footer> 可选
76-
<type> 必须是关键字 %s 之一
77-
更多信息,请参考项目主页: https://github.com/JayceChant/commit-msg.go`
78-
79-
enHint = [...]string{
80-
"Validated: commit message meet the rule.\n",
81-
"Merge: merge commit detected,skip check.\n",
82-
"Error ArgumentMissing: commit message file argument missing.\n",
83-
"Error FileMissing: file %s not exists.\n",
84-
"Error ReadError: read file %s error.\n",
85-
"Error EmptyMessage: commit message has no content except whitespaces.\n",
86-
"Error EmptyHeader: header (first line) has no content except whitespaces.\n",
87-
`Error BadHeaderFormat: header (first line) not following the rule:
88-
%s
89-
if you can not find any error after check, maybe you use Chinese colon, or lack of whitespace after the colon.`,
90-
"Error WrongType: %s, should be one of the keywords:\n%s\n",
91-
"Error BodyMissing: body has no content except whitespaces.\n",
92-
"Error NoBlankLineBeforeBody: no empty line between header and body.\n",
93-
"Error LineOverLong: the length of line is %d, exceed %d:\n%s\n",
94-
"Error UndefindedError: unexpected error occurs, please raise an issue.\n",
95-
}
96-
zhCnHint = [...]string{
97-
"Validated: 提交信息符合规范。\n",
98-
"Merge: 合并提交,跳过规范检查。\n",
99-
"Error ArgumentMissing: 缺少文件参数。\n",
100-
"Error FileMissing: 文件 %s 不存在。\n",
101-
"Error ReadError: 读取 %s 错误。\n",
102-
"Error EmptyMessage: 提交信息没有内容(不包括空白字符)。\n",
103-
"Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。\n",
104-
`Error BadHeaderFormat: 标题(第一行)不符合规范:
105-
%s
106-
如果您无法发现错误,请注意是否使用了中文冒号,或者冒号后面缺少空格。`,
107-
"Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s\n",
108-
"Error BodyMissing: 消息体没有内容(不包括空白字符)。\n",
109-
"Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。\n",
110-
"Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s\n",
111-
"Error UndefindedError: 没有预料到的错误,请提交一个错误报告。\n",
112-
}
62+
Types = strings.Join(TypeList[:], ", ")
11363
)
11464

65+
func (state MsgState) Hint() string {
66+
return Lang.HintList[state]
67+
}
68+
11569
func loadConfig() *GlobalConfig {
11670
f, err := os.Open(CONFIG_FILE)
11771
if err != nil {
@@ -146,15 +100,71 @@ func init() {
146100
}
147101

148102
switch Config.Lang {
149-
case "zh-CN":
150-
stateHint = zhCnHint[:]
151-
RuleHint = zhCnRule
103+
case "zh", "zh-CN":
104+
Lang = initLangZhCn()
152105
default:
153-
stateHint = enHint[:]
154-
RuleHint = enRule
106+
Lang = initLangEn()
155107
}
156108
}
157109

158-
func (state MsgState) Hint() string {
159-
return stateHint[state]
110+
func initLangEn() *LangPack {
111+
hint := []string{
112+
"Validated: commit message meet the rule.\n",
113+
"Merge: merge commit detected,skip check.\n",
114+
"Error ArgumentMissing: commit message file argument missing.\n",
115+
"Error FileMissing: file %s not exists.\n",
116+
"Error ReadError: read file %s error.\n",
117+
"Error EmptyMessage: commit message has no content except whitespaces.\n",
118+
"Error EmptyHeader: header (first line) has no content except whitespaces.\n",
119+
`Error BadHeaderFormat: header (first line) not following the rule:
120+
%s
121+
if you can not find any error after check, maybe you use Chinese colon, or lack of whitespace after the colon.`,
122+
"Error WrongType: %s, should be one of the keywords:\n%s\n",
123+
"Error BodyMissing: body has no content except whitespaces.\n",
124+
"Error NoBlankLineBeforeBody: no empty line between header and body.\n",
125+
"Error LineOverLong: the length of line is %d, exceed %d:\n%s\n",
126+
"Error UndefindedError: unexpected error occurs, please raise an issue.\n",
127+
}
128+
rule := `Commit message rule as follow:
129+
<type>(<scope>): <subject>
130+
// empty line
131+
<body>
132+
// empty line
133+
<footer>
134+
135+
(<scope>), <body> and <footer> are optional
136+
<type> must be one of %s
137+
more specific instructions, please refer to: https://github.com/JayceChant/commit-msg.go`
138+
return &LangPack{hint, rule}
139+
}
140+
141+
func initLangZhCn() *LangPack {
142+
hint := []string{
143+
"Validated: 提交信息符合规范。\n",
144+
"Merge: 合并提交,跳过规范检查。\n",
145+
"Error ArgumentMissing: 缺少文件参数。\n",
146+
"Error FileMissing: 文件 %s 不存在。\n",
147+
"Error ReadError: 读取 %s 错误。\n",
148+
"Error EmptyMessage: 提交信息没有内容(不包括空白字符)。\n",
149+
"Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。\n",
150+
`Error BadHeaderFormat: 标题(第一行)不符合规范:
151+
%s
152+
如果您无法发现错误,请注意是否使用了中文冒号,或者冒号后面缺少空格。`,
153+
"Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s\n",
154+
"Error BodyMissing: 消息体没有内容(不包括空白字符)。\n",
155+
"Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。\n",
156+
"Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s\n",
157+
"Error UndefindedError: 没有预料到的错误,请提交一个错误报告。\n",
158+
}
159+
rule := `提交信息规范如下:
160+
<type>(<scope>): <subject>
161+
// 空行
162+
<body>
163+
// 空行
164+
<footer>
165+
166+
(<scope>), <body> 和 <footer> 可选
167+
<type> 必须是关键字 %s 之一
168+
更多信息,请参考项目主页: https://github.com/JayceChant/commit-msg.go`
169+
return &LangPack{hint, rule}
160170
}

0 commit comments

Comments
 (0)