Skip to content

Commit 974492d

Browse files
committed
feat: 添加本地化错误信息
1 parent 1120fb5 commit 974492d

7 files changed

Lines changed: 43 additions & 7 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macOS-latest, windows-latest]
13-
go: ['1.21.x', '1.24.x']
13+
go: ['1.23.x', '1.24.x']
1414

1515
steps:
1616

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/issue9/upload/v3
22

33
require (
44
github.com/issue9/assert/v4 v4.3.1
5+
github.com/issue9/localeutil v0.29.4
56
github.com/issue9/watermark v1.2.5
67
)
78

8-
go 1.21
9+
require golang.org/x/text v0.24.0 // indirect
10+
11+
go 1.23.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
github.com/issue9/assert/v4 v4.3.1 h1:dHYODk1yV7j/1baIB6K6UggI4r1Hfuljqic7PaDbwLg=
22
github.com/issue9/assert/v4 v4.3.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
3+
github.com/issue9/localeutil v0.29.4 h1:VT8t64q1d7Ui1yCF6rfSI6Uibo/4v4HMtyT3n2yp6XI=
4+
github.com/issue9/localeutil v0.29.4/go.mod h1:k5nHeIIwmuQDil8ZF1dCCKn7pKEZmqe/6wCvBiunJis=
35
github.com/issue9/watermark v1.2.5 h1:mdZp+eSpj6ezyv+v36j1jDmQVbwaOcDPtv4657CPgak=
46
github.com/issue9/watermark v1.2.5/go.mod h1:RRhdNANxa6cmEMOZjEoaH7C5aChuBGBBrG/MCbPv8R8=
7+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
8+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=

locales/und.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
languages:
2+
- und
3+
messages:
4+
- key: no upload file
5+
message:
6+
msg: no upload file
7+
- key: not allow extension
8+
message:
9+
msg: not allow extension
10+
- key: the upload file is too large
11+
message:
12+
msg: the upload file is too large

locales/zh.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
languages:
2+
- cmn-Hans
3+
- zh-Hans
4+
- zh-CN
5+
messages:
6+
- key: no upload file
7+
message:
8+
msg: 没有上传文件
9+
- key: not allow extension
10+
message:
11+
msg: 不允许的上传文件类型
12+
- key: the upload file is too large
13+
message:
14+
msg: 上传的文件过大

save.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353

5454
type localSaver struct {
5555
fs fs.FS
56-
dir string
56+
dir string // TODO: 采用 [os.Root],需要 os.Root 实现 MkdirAll。
5757
baseURL string
5858
format string
5959
filenames func(dir, filename, ext string) string

upload.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5+
//go:generate web locale -l=und -m -f=yaml ./
6+
//go:generate web update-locale -src=./locales/und.yaml -dest=./locales/zh.yaml
7+
58
package upload
69

710
import (
8-
"errors"
911
"io"
1012
"io/fs"
1113
"mime/multipart"
@@ -15,14 +17,15 @@ import (
1517
"strings"
1618
"sync"
1719

20+
"github.com/issue9/localeutil"
1821
"github.com/issue9/watermark"
1922
)
2023

2124
// 常用错误类型
2225
var (
23-
errNotAllowExt = errors.New("不允许的文件上传类型")
24-
errNotAllowSize = errors.New("文件上传大小超过最大设定值")
25-
errNoUploadFile = errors.New("客户端没有上传文件")
26+
errNotAllowExt = localeutil.Error("not allow extension")
27+
errNotAllowSize = localeutil.Error("the upload file is too large")
28+
errNoUploadFile = localeutil.Error("no upload file")
2629
)
2730

2831
// Upload 用于处理文件上传

0 commit comments

Comments
 (0)