Skip to content

Commit

Permalink
refactor: add some i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Jun 5, 2024
1 parent c79eec6 commit a385972
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
3 changes: 2 additions & 1 deletion center/router/router_builtin_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
"github.com/toolkits/pkg/i18n"
)

// single or import
Expand All @@ -30,7 +31,7 @@ func (rt *Router) builtinMetricsAdd(c *gin.Context) {
lst[i].Lang = lang
lst[i].UUID = time.Now().UnixNano()
if err := lst[i].Add(rt.Ctx, username); err != nil {
reterr[lst[i].Name] = err.Error()
reterr[lst[i].Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
}
ginx.NewRender(c).Data(reterr, nil)
Expand Down
13 changes: 7 additions & 6 deletions center/router/router_builtin_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ccfos/nightingale/v6/models"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
"github.com/toolkits/pkg/i18n"
)

type Board struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ func (rt *Router) builtinPayloadsAdd(c *gin.Context) {
}

if err := bp.Add(rt.Ctx, username); err != nil {
reterr[bp.Name] = err.Error()
reterr[bp.Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
}
continue
Expand Down Expand Up @@ -92,7 +93,7 @@ func (rt *Router) builtinPayloadsAdd(c *gin.Context) {
}

if err := bp.Add(rt.Ctx, username); err != nil {
reterr[bp.Name] = err.Error()
reterr[bp.Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
} else if lst[i].Type == "dashboard" {
if strings.HasPrefix(strings.TrimSpace(lst[i].Content), "[") {
Expand Down Expand Up @@ -126,15 +127,15 @@ func (rt *Router) builtinPayloadsAdd(c *gin.Context) {
}

if err := bp.Add(rt.Ctx, username); err != nil {
reterr[bp.Name] = err.Error()
reterr[bp.Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
}
continue
}

dashboard := Board{}
if err := json.Unmarshal([]byte(lst[i].Content), &dashboard); err != nil {
reterr[lst[i].Name] = err.Error()
reterr[lst[i].Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
continue
}

Expand All @@ -155,11 +156,11 @@ func (rt *Router) builtinPayloadsAdd(c *gin.Context) {
}

if err := bp.Add(rt.Ctx, username); err != nil {
reterr[bp.Name] = err.Error()
reterr[bp.Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
} else {
if err := lst[i].Add(rt.Ctx, username); err != nil {
reterr[lst[i].Name] = err.Error()
reterr[lst[i].Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error())
}
}

Expand Down
9 changes: 9 additions & 0 deletions models/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func (b *Board) Add(ctx *ctx.Context) error {
}
}

cnt, err := Count(DB(ctx).Model(b).Where("name = ? and group_id = ?", b.Name, b.GroupId))
if err != nil {
return err
}

if cnt > 0 {
return errors.New("Name duplicate")
}

now := time.Now().Unix()
b.CreateAt = now
b.UpdateAt = now
Expand Down
59 changes: 56 additions & 3 deletions pkg/i18nx/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var I18N = `
"invalid tagkey(%s)": "tagkey不合法[%s]",
"duplicate tagkey(%s)":"tagkey(%s)重复了",
"name is empty": "名称不能为空",
"Ident duplicate":"大盘唯一标识已存在",
"No such dashboard":"大盘不存在",
"Ident duplicate":"仪表盘唯一标识已存在",
"No such dashboard":"仪表盘不存在",
"Name has invalid characters":"名称包含非法字符",
"Name is blank":"名称不能为空",
"forbidden":"没有权限",
Expand All @@ -48,7 +48,60 @@ var I18N = `
"invalid ibex address: %s":"ibex %s 地址无效",
"url path invalid":"url非法",
"no such server":"无此实例",
"admin role can not be modified":"管理员角色不允许修改"
"admin role can not be modified":"管理员角色不允许修改",
"builtin payload already exists":"内置模板已存在"
},
"zh_CN": {
"Username or password invalid": "用户名或密码错误",
"incorrect verification code": "验证码错误",
"roles empty": "角色不能为空",
"Username already exists": "此用户名已存在 请使用其他用户名",
"failed to count user-groups": "校验数据失败 请重试",
"UserGroup already exists": "组名已存在 请使用其他名称",
"members empty": "成员不能为空",
"At least one team have rw permission": "至少需要有一个团队有读写权限",
"Failed to create BusiGroup(%s)": "[%s]创建失败 请重试",
"business group id invalid": "业务组 id 不正确",
"idents empty": "监控对象不能为空",
"invalid tag(%s)": "tag不合法[%s]",
"invalid tagkey(%s): cannot contains . ": "tagkey[%s]不能包含.",
"invalid tagkey(%s): cannot contains _ ": "tagkey[%s]不能包含_",
"invalid tagkey(%s)": "tagkey不合法[%s]",
"duplicate tagkey(%s)":"tagkey(%s)重复了",
"name is empty": "名称不能为空",
"Ident duplicate":"仪表盘唯一标识已存在",
"Name duplicate":"仪表盘名称已存在",
"No such dashboard":"仪表盘不存在",
"Name has invalid characters":"名称包含非法字符",
"Name is blank":"名称不能为空",
"forbidden":"没有权限",
"builtin alerts is empty, file: %s":"内置告警模板为空 %s",
"input json is empty":"提交内容不能为空",
"fields empty":"选择字段不能为空",
"No such AlertRule":"无此告警规则",
"GroupId(%d) invalid":"业务组id无效",
"No such recording rule":"无此记录规则",
"tags is blank":"标签不能为空",
"oops... etime(%d) <= btime(%d)":"开始时间,不能大于结束时间",
"group_id invalid":"业务组无效",
"No such AlertMute":"无此屏蔽规则",
"rule_id and tags are both blank":"告警规则和标签不能同时为空",
"rule is blank":"规则不能为空",
"rule invalid":"规则无效 请检查是否正确",
"unsupported field: %s":"不支持字段 %s",
"arg(batch) should be nonnegative":"batch 不能为负数",
"arg(tolerance) should be nonnegative":"tolerance 不能为负数",
"arg(timeout) should be nonnegative":"timeout 不能为负数",
"arg(timeout) longer than five days":"timeout 时间不能超过5天",
"arg(title) is required":"title 为必填项",
"created task.id is zero":"任务id为零",
"invalid ibex address: %s":"ibex %s 地址无效",
"url path invalid":"url非法",
"no such server":"无此实例",
"admin role can not be modified":"管理员角色不允许修改",
"builtin payload already exists":"内置模板已存在",
"builtin metric already exists":"内置指标已存在",
"AlertRule already exists":"告警规则已存在"
}
}
`

0 comments on commit a385972

Please sign in to comment.