Skip to content

Commit afab180

Browse files
committed
refactor(lang): putting different languages into separate files
1 parent 22f1b9e commit afab180

File tree

7 files changed

+98
-89
lines changed

7 files changed

+98
-89
lines changed

lang/chinese.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lang
2+
3+
import (
4+
. "github.com/JayceChant/commit-msg/state"
5+
)
6+
7+
var (
8+
langZhCn = &langPack{
9+
Hints: map[State]string{
10+
Validated: "Validated: 提交信息符合规范。",
11+
Merge: "Merge: 合并提交,跳过规范检查。",
12+
ArgumentMissing: "Error ArgumentMissing: 缺少文件参数。",
13+
FileMissing: "Error FileMissing: 文件 %s 不存在。",
14+
ReadError: "Error ReadError: 读取 %s 错误。",
15+
EmptyMessage: "Error EmptyMessage: 提交信息没有内容(不包括空白字符)。",
16+
EmptyHeader: "Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。",
17+
BadHeaderFormat: `Error BadHeaderFormat: 标题(第一行)不符合规范:
18+
%s
19+
如果您无法发现错误,请注意是否使用了中文冒号,或者冒号后面缺少空格。`,
20+
WrongType: "Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s",
21+
ScopeMissing: "Error ScopeMissing: 类型后面缺少'(scope)'。",
22+
WrongScope: "Error WrongScope: %s, 范围关键字应为以下选项中的一个:\n%s",
23+
BodyMissing: "Error BodyMissing: 消息体没有内容(不包括空白字符)。",
24+
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。",
25+
LineOverLong: "Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s",
26+
UndefindedError: "Error UndefindedError: 没有预料到的错误,请提交一个错误报告。",
27+
},
28+
Rule: `提交信息规范如下:
29+
<type>(<scope>): <subject>
30+
// 空行
31+
<body>
32+
// 空行
33+
<footer>
34+
35+
(<scope>), <body> 和 <footer> 默认可选,也可以在配置设置必选
36+
<type> 必须是关键字 %s 之一
37+
更多信息,请参考项目主页: https://github.com/JayceChant/commit-msg`,
38+
}
39+
)

lang/english.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lang
2+
3+
import (
4+
. "github.com/JayceChant/commit-msg/state"
5+
)
6+
7+
var (
8+
langEn = &langPack{
9+
Hints: map[State]string{
10+
Validated: "Validated: commit message meet the rule.",
11+
Merge: "Merge: merge commit detected,skip check.",
12+
ArgumentMissing: "Error ArgumentMissing: commit message file argument missing.",
13+
FileMissing: "Error FileMissing: file %s not exists.",
14+
ReadError: "Error ReadError: read file %s error.",
15+
EmptyMessage: "Error EmptyMessage: commit message has no content except whitespaces.",
16+
EmptyHeader: "Error EmptyHeader: header (first line) has no content except whitespaces.",
17+
BadHeaderFormat: `Error BadHeaderFormat: header (first line) not following the rule:
18+
%s
19+
if you can not find any error after check, maybe you use full-width colon, or lack of whitespace after the colon.`,
20+
WrongType: "Error WrongType: %s, type should be one of the keywords:\n%s",
21+
ScopeMissing: "Error ScopeMissing: (scope) is required right after type.",
22+
WrongScope: "Error WrongScope: %s, scope should be one of the keywords:\n%s",
23+
BodyMissing: "Error BodyMissing: body has no content except whitespaces.",
24+
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: no empty line between header and body.",
25+
LineOverLong: "Error LineOverLong: the length of line is %d, exceed %d:\n%s",
26+
UndefindedError: "Error UndefindedError: unexpected error occurs, please raise an issue.",
27+
},
28+
Rule: `Commit message rule as follow:
29+
<type>(<scope>): <subject>
30+
// empty line
31+
<body>
32+
// empty line
33+
<footer>
34+
35+
(<scope>), <body> and <footer> are optional by default
36+
<type> must be one of %s
37+
more specific instructions, please refer to: https://github.com/JayceChant/commit-msg`,
38+
}
39+
)

lang/lang.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,70 +28,6 @@ func (l *langPack) GetRule(types string) string {
2828
}
2929

3030
var (
31-
langEn = &langPack{
32-
Hints: map[State]string{
33-
Validated: "Validated: commit message meet the rule.",
34-
Merge: "Merge: merge commit detected,skip check.",
35-
ArgumentMissing: "Error ArgumentMissing: commit message file argument missing.",
36-
FileMissing: "Error FileMissing: file %s not exists.",
37-
ReadError: "Error ReadError: read file %s error.",
38-
EmptyMessage: "Error EmptyMessage: commit message has no content except whitespaces.",
39-
EmptyHeader: "Error EmptyHeader: header (first line) has no content except whitespaces.",
40-
BadHeaderFormat: `Error BadHeaderFormat: header (first line) not following the rule:
41-
%s
42-
if you can not find any error after check, maybe you use Chinese colon, or lack of whitespace after the colon.`,
43-
WrongType: "Error WrongType: %s, type should be one of the keywords:\n%s",
44-
ScopeMissing: "Error ScopeMissing: (scope) is required right after type.",
45-
WrongScope: "Error WrongScope: %s, scope should be one of the keywords:\n%s",
46-
BodyMissing: "Error BodyMissing: body has no content except whitespaces.",
47-
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: no empty line between header and body.",
48-
LineOverLong: "Error LineOverLong: the length of line is %d, exceed %d:\n%s",
49-
UndefindedError: "Error UndefindedError: unexpected error occurs, please raise an issue.",
50-
},
51-
Rule: `Commit message rule as follow:
52-
<type>(<scope>): <subject>
53-
// empty line
54-
<body>
55-
// empty line
56-
<footer>
57-
58-
(<scope>), <body> and <footer> are optional by default
59-
<type> must be one of %s
60-
more specific instructions, please refer to: https://github.com/JayceChant/commit-msg`,
61-
}
62-
63-
langZhCn = &langPack{
64-
Hints: map[State]string{
65-
Validated: "Validated: 提交信息符合规范。",
66-
Merge: "Merge: 合并提交,跳过规范检查。",
67-
ArgumentMissing: "Error ArgumentMissing: 缺少文件参数。",
68-
FileMissing: "Error FileMissing: 文件 %s 不存在。",
69-
ReadError: "Error ReadError: 读取 %s 错误。",
70-
EmptyMessage: "Error EmptyMessage: 提交信息没有内容(不包括空白字符)。",
71-
EmptyHeader: "Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。",
72-
BadHeaderFormat: `Error BadHeaderFormat: 标题(第一行)不符合规范:
73-
%s
74-
如果您无法发现错误,请注意是否使用了中文冒号,或者冒号后面缺少空格。`,
75-
WrongType: "Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s",
76-
ScopeMissing: "Error ScopeMissing: 类型后面缺少'(scope)'。",
77-
WrongScope: "Error WrongScope: %s, 范围关键字应为以下选项中的一个:\n%s",
78-
BodyMissing: "Error BodyMissing: 消息体没有内容(不包括空白字符)。",
79-
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。",
80-
LineOverLong: "Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s",
81-
UndefindedError: "Error UndefindedError: 没有预料到的错误,请提交一个错误报告。",
82-
},
83-
Rule: `提交信息规范如下:
84-
<type>(<scope>): <subject>
85-
// 空行
86-
<body>
87-
// 空行
88-
<footer>
89-
90-
(<scope>), <body> 和 <footer> 默认可选,也可以在配置设置必选
91-
<type> 必须是关键字 %s 之一
92-
更多信息,请参考项目主页: https://github.com/JayceChant/commit-msg`,
93-
}
94-
9531
langs = map[string]*langPack{
9632
"en": langEn,
9733
"zh": langZhCn,

state/state.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ const (
4646
UndefindedError
4747
)
4848

49-
// Error ...
50-
// func (state State) Error() string {
51-
// return lang.Hints[state]
52-
// }
53-
5449
// LogAndExit ...
5550
func (state State) LogAndExit(v ...interface{}) {
5651
log.Println(lang.GetHint(state, v...))

validator/config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
hookDir = "./.git/hooks/"
1818
)
1919

20-
type globalConfig struct {
20+
type validateConfig struct {
2121
Lang string `json:"lang,omitempty"`
2222
BodyRequired bool `json:"bodyRequired,omitempty"`
2323
LineLimit int `json:"lineLimit,omitempty"`
@@ -30,8 +30,8 @@ type globalConfig struct {
3030
type empty struct{}
3131

3232
var (
33-
// Config ...
34-
Config *globalConfig = &globalConfig{Lang: "en", BodyRequired: false, LineLimit: 80}
33+
// globalConfig ...
34+
globalConfig *validateConfig = &validateConfig{Lang: "en", BodyRequired: false, LineLimit: 80}
3535
// TypeSet ...
3636
TypeSet = map[string]empty{
3737
"feat": {}, // new feature 新功能
@@ -67,7 +67,7 @@ func locateConfigs() []string {
6767
return paths
6868
}
6969

70-
func loadConfig(path string, cfg *globalConfig) *globalConfig {
70+
func loadConfig(path string, cfg *validateConfig) *validateConfig {
7171
f, err := os.Open(path)
7272
if err != nil && !os.IsExist(err) {
7373
return cfg
@@ -98,17 +98,17 @@ func init() {
9898
paths := locateConfigs()
9999
for _, p := range paths {
100100
// TODO: fix json value overlaping
101-
Config = loadConfig(p, Config)
101+
globalConfig = loadConfig(p, globalConfig)
102102
}
103103
// if Config == nil {
104104
// Config = initConfig(path)
105105
// }
106106

107-
for _, t := range Config.Types {
107+
for _, t := range globalConfig.Types {
108108
TypeSet[t] = empty{}
109109
}
110110

111-
for _, t := range Config.DenyTypes {
111+
for _, t := range globalConfig.DenyTypes {
112112
delete(TypeSet, t)
113113
}
114114

@@ -118,5 +118,5 @@ func init() {
118118
}
119119
TypesStr = strings.Join(types, ", ")
120120

121-
state.Init(lang.LoadLanguage(Config.Lang), TypesStr)
121+
state.Init(lang.LoadLanguage(globalConfig.Lang), TypesStr)
122122
}

validator/validate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Validate(file string) {
3232
}
3333
}()
3434

35-
validateMsg(getMsg(file), Config)
35+
validateMsg(getMsg(file), globalConfig)
3636
}
3737

3838
func getMsg(path string) string {
@@ -60,7 +60,7 @@ func getMsg(path string) string {
6060
return string(buf)
6161
}
6262

63-
func validateMsg(msg string, config *globalConfig) {
63+
func validateMsg(msg string, config *validateConfig) {
6464
if isEmpty(msg) {
6565
state.EmptyMessage.LogAndExit()
6666
}
@@ -93,7 +93,7 @@ func isMergeCommit(msg string) {
9393
}
9494
}
9595

96-
func validateHeader(header string, config *globalConfig) {
96+
func validateHeader(header string, config *validateConfig) {
9797
if isEmpty(header) {
9898
state.EmptyHeader.LogAndExit()
9999
}
@@ -143,7 +143,7 @@ func validateType(typ string) {
143143
state.WrongType.LogAndExit(typ, TypesStr)
144144
}
145145

146-
func validateScope(scope string, config *globalConfig) {
146+
func validateScope(scope string, config *validateConfig) {
147147
if isEmpty(scope) {
148148
if config.ScopeRequired {
149149
state.ScopeMissing.LogAndExit()
@@ -163,7 +163,7 @@ func validateScope(scope string, config *globalConfig) {
163163
state.WrongScope.LogAndExit(scope, strings.Join(config.Scopes, ", "))
164164
}
165165

166-
func validateBody(body string, config *globalConfig) {
166+
func validateBody(body string, config *validateConfig) {
167167
if isEmpty(body) {
168168
if config.BodyRequired {
169169
state.BodyMissing.LogAndExit()

validator/validate_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
)
1212

1313
var (
14-
zeroCfg = &globalConfig{}
15-
defaultCfg = &globalConfig{Lang: "en", BodyRequired: true, LineLimit: 80}
16-
scopeRequired = &globalConfig{ScopeRequired: true}
17-
scopesSpecified = &globalConfig{Scopes: []string{"model", "view", "controller"}}
14+
zeroCfg = &validateConfig{}
15+
defaultCfg = &validateConfig{Lang: "en", BodyRequired: true, LineLimit: 80}
16+
scopeRequired = &validateConfig{ScopeRequired: true}
17+
scopesSpecified = &validateConfig{Scopes: []string{"model", "view", "controller"}}
1818
)
1919

2020
func assertExitCode(t *testing.T, f func(), name string, expected int) {
@@ -106,7 +106,7 @@ func TestValidateHeader(t *testing.T) {
106106
var headerCases = []struct {
107107
text string
108108
name string
109-
config *globalConfig
109+
config *validateConfig
110110
want int
111111
}{
112112
{"", "empty_header", defaultCfg, int(state.EmptyHeader)},
@@ -131,7 +131,7 @@ func TestValidateBody(t *testing.T) {
131131
var bodyCases = []struct {
132132
text string
133133
name string
134-
config *globalConfig
134+
config *validateConfig
135135
want int
136136
}{
137137
{"", "body_missing", defaultCfg, int(state.BodyMissing)},
@@ -169,7 +169,7 @@ func TestValidateScope(t *testing.T) {
169169
var scopeCases = []struct {
170170
text string
171171
name string
172-
config *globalConfig
172+
config *validateConfig
173173
want int
174174
}{
175175
{"model", "normal", scopeRequired, 0},

0 commit comments

Comments
 (0)