Skip to content

Commit

Permalink
feat: 添加翻译项
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 5, 2024
1 parent 32e6402 commit db1a5ab
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/issue9/scheduled

require (
github.com/issue9/assert/v4 v4.1.1
github.com/issue9/localeutil v0.26.5
github.com/issue9/assert/v4 v4.3.0
github.com/issue9/localeutil v0.26.7
github.com/issue9/sliceutil v0.16.1
golang.org/x/text v0.14.0
golang.org/x/text v0.15.0
)

go 1.21
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc=
github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A=
github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8=
github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U=
github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/localeutil v0.26.7 h1:LLpzBoZhAmRUqyDuveWNq+LA7hxD2G2ZJvjhRYrhikg=
github.com/issue9/localeutil v0.26.7/go.mod h1:TNoqmrux5wJwS2ZcZw4qwMPJx8nmxta5OchfAKQbYaI=
github.com/issue9/sliceutil v0.16.1 h1:NVZ2feN014oUmMpZvZUrKkKMGhAfzT8WNzJF81krebM=
github.com/issue9/sliceutil v0.16.1/go.mod h1:ldun6sT4/bOJxuMtOXhtc6P7GCwE7L+avV86HNks7qk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
21 changes: 21 additions & 0 deletions locales/und.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
id: und
messages:
- key: all items are asterisk
message:
msg: all items are asterisk
- key: can not be empty
message:
msg: can not be empty
- key: cron syntax error %s
message:
msg: cron syntax error %s
- key: duplicate value %d
message:
msg: duplicate value %d
- key: incorrect length
message:
msg: incorrect length
- key: invalid direct %s
message:
msg: invalid direct %s
- key: invalid state %d
message:
msg: invalid state %d
Expand All @@ -12,3 +30,6 @@ messages:
- key: 'scheduled: start job %s at %s'
message:
msg: 'scheduled: start job %s at %s'
- key: the value %d out of range [%d,%d]
message:
msg: the value %d out of range [%d,%d]
21 changes: 21 additions & 0 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
id: und
messages:
- key: all items are asterisk
message:
msg: 所有项都是星号
- key: can not be empty
message:
msg: 不能为空
- key: cron syntax error %s
message:
msg: 语法错误: %s
- key: duplicate value %d
message:
msg: 重复的值 %d
- key: incorrect length
message:
msg: 错误的长度
- key: invalid direct %s
message:
msg: 无效的指令 %s
- key: invalid state %d
message:
msg: 无效的状态 %d
Expand All @@ -12,3 +30,6 @@ messages:
- key: 'scheduled: start job %s at %s'
message:
msg: 在 %[2]s 运行计划任务 %[1]s
- key: the value %d out of range [%d,%d]
message:
msg: 值 %d 超出了范围 [%d,%d]
14 changes: 9 additions & 5 deletions schedulers/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
package cron

import (
"errors"
"strings"
"time"

"github.com/issue9/localeutil"
"github.com/issue9/scheduled/schedulers"
"github.com/issue9/scheduled/schedulers/at"
)
Expand Down Expand Up @@ -78,20 +78,20 @@ type cron struct {
func Parse(spec string, loc *time.Location) (schedulers.Scheduler, error) {
switch {
case spec == "":
return nil, errors.New("参数 spec 不能为空")
return nil, syntaxError(localeutil.Phrase("can not be empty"))
case spec == "@reboot":
return at.At(time.Now()), nil
case spec[0] == '@':
d, found := direct[spec]
if !found {
return nil, errors.New("未找到指令:" + spec)
return nil, syntaxError(localeutil.Phrase("invalid direct %s", spec))
}
spec = d
}

fs := strings.Fields(spec)
if len(fs) != indexSize {
return nil, errors.New("长度不正确")
return nil, syntaxError(localeutil.Phrase("incorrect length"))
}

c := &cron{
Expand All @@ -118,8 +118,12 @@ func Parse(spec string, loc *time.Location) (schedulers.Scheduler, error) {
}

if allAny { // 所有项都为 *
return nil, errors.New("所有项都为 *")
return nil, syntaxError(localeutil.Phrase("all items are asterisk"))
}

return c, nil
}

func syntaxError(s localeutil.Stringer) error {
return localeutil.Error("cron syntax error %s", s)
}
10 changes: 5 additions & 5 deletions schedulers/cron/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package cron

import (
"fmt"
"math/bits"
"strconv"
"strings"

"github.com/issue9/localeutil"
"github.com/issue9/sliceutil"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ func parseField(typ int, field string) (fields, error) {
}

if !b.valid(n) {
return 0, fmt.Errorf("值 %d 超出范围:[%d,%d]", n, b.min, b.max)
return 0, syntaxError(localeutil.Phrase("the value %d out of range [%d,%d]", n, b.min, b.max))
}

// 星期中的 7 替换成 0
Expand All @@ -137,11 +137,11 @@ func parseField(typ int, field string) (fields, error) {
}

if !b.valid(n1) {
return 0, fmt.Errorf("值 %d 超出范围:[%d,%d]", n1, b.min, b.max)
return 0, syntaxError(localeutil.Phrase("the value %d out of range [%d,%d]", n1, b.min, b.max))
}

if !b.valid(n2) {
return 0, fmt.Errorf("值 %d 超出范围:[%d,%d]", n2, b.min, b.max)
return 0, syntaxError(localeutil.Phrase("the value %d out of range [%d,%d]", n2, b.min, b.max))
}

for i := n1; i <= n2; i++ {
Expand All @@ -155,7 +155,7 @@ func parseField(typ int, field string) (fields, error) {
}

if indexes := sliceutil.Dup(list, func(i, j uint64) bool { return i == j }); len(indexes) > 0 {
return 0, fmt.Errorf("重复的值 %d", list[indexes[0]])
return 0, syntaxError(localeutil.Phrase("duplicate value %d", list[indexes[0]]))
}

var ret fields
Expand Down

0 comments on commit db1a5ab

Please sign in to comment.