Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/openapi components i18n #1451

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (o *OrgFormModal) SetProps() {
"required": true,
"rules": []interface{}{
map[string]interface{}{
"msg": "由小写字母、数字,-组成(不允许纯数字)",
"msg": i18nLocale.Get(i18n.I18nKeyOrgCreateRegexp),
"pattern": "/^(?:[a-z]+|[0-9]+[a-z]+|[0-9]+[-]+[a-z0-9])+(?:(?:(?:[-]*)[a-z0-9]+)+)?$/",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ const (
I18nKeyOrgBrosePublic = "wb.sidebar.component.org.browsePublic"
I18nKeyOrgAddFirstOr = "wb.sidebar.component.org.addFirstOr"
I18nKeyOrgAddFirst = "wb.sidebar.component.org.addFirst"
I18nKeyOrgCreateRegexp = "wb.sidebar.component.org.create.regexp"
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/org-list-all/i18n"
)

const defaultOrgImage = "frontImg_default_org_icon"
Expand Down Expand Up @@ -130,6 +131,7 @@ func (i *ComponentList) RenderPublicOrgs() error {
orgMap[o.ID] = true
}

i18nLocale := i.CtxBdl.Bdl.GetLocale(i.CtxBdl.Locale)
i.State.Total = orgs.Total
data := []Org{}
for _, org := range orgs.List {
Expand All @@ -141,7 +143,7 @@ func (i *ComponentList) RenderPublicOrgs() error {
ExtraInfos: []ExtraInfo{
{
Icon: "unlock",
Text: "公开组织",
Text: i18nLocale.Get(i18n.I18nPublicOrg),
},
},
}
Expand All @@ -163,7 +165,7 @@ func (i *ComponentList) RenderPublicOrgs() error {
if _, ok := orgMap[org.ID]; ok {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfo{
Icon: "user",
Text: "已加入",
Text: i18nLocale.Get(i18n.I18nOrgJoined),
})
command.Target = "dopRoot"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ package page
import (
"context"
"encoding/json"
"fmt"

"github.com/sirupsen/logrus"

"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/org-list-all/i18n"
)

func (i *ComponentPage) marshal(c *apistructs.Component) error {
Expand All @@ -37,7 +41,21 @@ func (i *ComponentPage) marshal(c *apistructs.Component) error {
return nil
}

func (i *ComponentPage) SetCtxBundle(ctx context.Context) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if bdl.Bdl == nil || bdl.I18nPrinter == nil {
return fmt.Errorf("invalid context bundle")
}
logrus.Infof("inParams:%+v, identity:%+v", bdl.InParams, bdl.Identity)
i.CtxBdl = bdl
return nil
}

func (i *ComponentPage) Render(ctx context.Context, c *apistructs.Component, s apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) (err error) {
if err := i.SetCtxBundle(ctx); err != nil {
return err
}

defer func() {
fail := i.marshal(c)
if err == nil && fail != nil {
Expand All @@ -51,9 +69,10 @@ func (i *ComponentPage) Render(ctx context.Context, c *apistructs.Component, s a
}

func (i *ComponentPage) initProperty(s apistructs.ComponentProtocolScenario) {
i18nLocale := i.CtxBdl.Bdl.GetLocale(i.CtxBdl.Locale)
publicOrgs := Option{
Key: "public",
Name: "公开组织",
Name: i18nLocale.Get(i18n.I18nPublicOrg),
Operations: map[string]interface{}{
"click": ClickOperation{
Reload: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package i18n

const (
I18nPublicOrg = "org.list.all.org.public"
I18nOrgJoined = "org.list.all.org.joined"
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/project-list-all/i18n"
)

func GetOpsInfo(opsData interface{}) (*Meta, error) {
Expand All @@ -44,8 +45,9 @@ func GetOpsInfo(opsData interface{}) (*Meta, error) {
return &meta, nil
}

func RenItem(pro apistructs.ProjectDTO, orgDomain string) (ProItem, error) {
activeTime, err := CountActiveTime(pro.ActiveTime)
func (i *ComponentList) RenItem(pro apistructs.ProjectDTO, orgDomain string) (ProItem, error) {
i18nLocale := i.ctxBdl.Bdl.GetLocale(i.ctxBdl.Locale)
activeTime, err := i.CountActiveTime(pro.ActiveTime)
if err != nil {
return ProItem{}, err
}
Expand Down Expand Up @@ -97,17 +99,17 @@ func RenItem(pro apistructs.ProjectDTO, orgDomain string) (ProItem, error) {
map[bool]ExtraInfos{
true: {
Icon: "unlock",
Text: "公开项目",
Text: i18nLocale.Get(i18n.I18nPublicProject),
},
false: {
Icon: "lock",
Text: "私有项目",
Text: i18nLocale.Get(i18n.I18nPrivateProject),
},
}[pro.IsPublic],
{
Icon: "application-one",
Text: strconv.Itoa(pro.Stats.CountApplications),
Tooltip: "应用数",
Tooltip: i18nLocale.Get(i18n.I18nAppNumber),
},
{
Icon: "time",
Expand All @@ -127,21 +129,21 @@ func RenItem(pro apistructs.ProjectDTO, orgDomain string) (ProItem, error) {
if pro.Joined {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfos{
Icon: "user",
Text: "已加入",
Text: i18nLocale.Get(i18n.I18nJoined),
})
}
// 解封状态
if pro.BlockStatus == "unblocking" {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfos{
Icon: "link-cloud-faild",
Text: "解封处理中,请稍等",
Text: i18nLocale.Get(i18n.I18nUnblocking),
Type: "warning",
Tooltip: "解封处理中,请稍等",
Tooltip: i18nLocale.Get(i18n.I18nUnblocking),
})
} else if pro.BlockStatus == "unblocked" {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfos{
Icon: "link-cloud-sucess",
Text: "已解封",
Text: i18nLocale.Get(i18n.I18nUnblocked),
Type: "success",
})
}
Expand Down Expand Up @@ -195,7 +197,7 @@ func (i *ComponentList) RenderList() error {
i.State.Total = 0
if projectDTO != nil {
for _, v := range projectDTO.List {
p, err := RenItem(v, org.Domain)
p, err := i.RenItem(v, org.Domain)
if err != nil {
return err
}
Expand Down Expand Up @@ -241,8 +243,9 @@ func (i *ComponentList) RenderChangePageNo(ops interface{}) error {
return nil
}

func CountActiveTime(ActiveTimeStr string) (string, error) {
func (i *ComponentList) CountActiveTime(ActiveTimeStr string) (string, error) {
var subStr string
i18nLocale := i.ctxBdl.Bdl.GetLocale(i.ctxBdl.Locale)
nowTime := time.Now()
activeTime, err := time.Parse("2006-01-02 15:04:05", ActiveTimeStr)
if err != nil {
Expand All @@ -252,17 +255,17 @@ func CountActiveTime(ActiveTimeStr string) (string, error) {
sub := nowTime.Sub(activeTime)
sub = sub + 8*time.Hour
if int64(sub.Hours()) >= 24*30*12 {
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30*12), 10) + " 年前"
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30*12), 10) + " " + i18nLocale.Get(i18n.I18nYearAgo)
} else if int64(sub.Hours()) >= 24*30 {
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30), 10) + " 月前"
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30), 10) + " " + i18nLocale.Get(i18n.I18nMonthAgo)
} else if int64(sub.Hours()) >= 24 {
subStr = strconv.FormatInt(int64(sub.Hours())/24, 10) + " 天前"
subStr = strconv.FormatInt(int64(sub.Hours())/24, 10) + " " + i18nLocale.Get(i18n.I18nDayAgo)
} else if int64(sub.Hours()) > 0 {
subStr = strconv.FormatInt(int64(sub.Hours()), 10) + " 小时前"
subStr = strconv.FormatInt(int64(sub.Hours()), 10) + " " + i18nLocale.Get(i18n.I18nHourAgo)
} else if int64(sub.Minutes()) > 0 {
subStr = strconv.FormatInt(int64(sub.Minutes()), 10) + " 分钟前"
subStr = strconv.FormatInt(int64(sub.Minutes()), 10) + " " + i18nLocale.Get(i18n.I18nMinuteAgo)
} else {
subStr = "几秒前"
subStr = i18nLocale.Get(i18n.I18nSecondAgo)
}
return subStr, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package i18n

const (
I18nPublicProject = "project.list.all.project.public"
I18nPrivateProject = "project.list.all.project.private"
I18nAppNumber = "project.list.all.app.number"
I18nJoined = "project.list.all.joined"
I18nUnblocking = "project.list.all.unblocking"
I18nUnblocked = "project.list.all.unblocked"
I18nYearAgo = "project.list.all.year.ago"
I18nMonthAgo = "project.list.all.month.ago"
I18nDayAgo = "project.list.all.day.ago"
I18nHourAgo = "project.list.all.hour.ago"
I18nMinuteAgo = "project.list.all.minute.ago"
I18nSecondAgo = "project.list.all.second.ago"
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ package browsePublicProjects
import (
"context"
"encoding/json"
"fmt"

"github.com/sirupsen/logrus"

"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/project-list-my/i18n"
)

func (i *ComponentBrowsePublic) SetCtxBundle(ctx context.Context) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if bdl.Bdl == nil || bdl.I18nPrinter == nil {
return fmt.Errorf("invalid context bundle")
}
logrus.Infof("inParams:%+v, identity:%+v", bdl.InParams, bdl.Identity)
i.ctxBdl = bdl
return nil
}

// GenComponentState 获取state
func (i *ComponentBrowsePublic) GenComponentState(c *apistructs.Component) error {
if c == nil || c.State == nil {
Expand All @@ -47,13 +59,17 @@ func (i *ComponentBrowsePublic) Render(ctx context.Context, c *apistructs.Compon
if err := i.GenComponentState(c); err != nil {
return err
}
if err := i.SetCtxBundle(ctx); err != nil {
return err
}

// 如果list组件的数据不为空,则直接返回
if !i.State.IsEmpty {
c.Props = map[string]interface{}{"visible": false}
return nil
}

i18nLocale := i.ctxBdl.Bdl.GetLocale(i.ctxBdl.Locale)
i.Props = Props{
Visible: true,
RenderType: "linkText",
Expand All @@ -64,7 +80,7 @@ func (i *ComponentBrowsePublic) Render(ctx context.Context, c *apistructs.Compon
Value: map[string]interface{}{
"text": []interface{}{
map[string]interface{}{
"text": "浏览该组织下的公开项目",
"text": i18nLocale.Get(i18n.I18nProjectPublicBrowse),
"operationKey": "toPublicProject",
"styleConfig": map[string]interface{}{"bold": true},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package emptyText
import (
"context"
"encoding/json"
"fmt"

"github.com/sirupsen/logrus"

"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/project-list-my/i18n"
)

// GenComponentState 获取state
Expand All @@ -43,17 +45,31 @@ func (i *ComponentText) GenComponentState(c *apistructs.Component) error {
return nil
}

func (i *ComponentText) SetCtxBundle(ctx context.Context) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if bdl.Bdl == nil || bdl.I18nPrinter == nil {
return fmt.Errorf("invalid context bundle")
}
logrus.Infof("inParams:%+v, identity:%+v", bdl.InParams, bdl.Identity)
i.ctxBdl = bdl
return nil
}

func (i *ComponentText) Render(ctx context.Context, c *apistructs.Component, _ apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) (err error) {
if err := i.GenComponentState(c); err != nil {
return err
}
if err := i.SetCtxBundle(ctx); err != nil {
return err
}

// 如果list组件的数据不为空,则直接返回
if !i.State.IsEmpty {
c.Props = map[string]interface{}{"visible": false}
return nil
}

i18nLocale := i.ctxBdl.Bdl.GetLocale(i.ctxBdl.Locale)
i.Props = Props{
Visible: true,
RenderType: "linkText",
Expand All @@ -63,7 +79,7 @@ func (i *ComponentText) Render(ctx context.Context, c *apistructs.Component, _ a
},
Value: map[string]interface{}{
"text": []interface{}{
"您当前还未被邀请加入任何项目",
i18nLocale.Get(i18n.I18nProjectNotJoined),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package i18n

const (
I18nProjectNotJoined = "project.list.my.project.not.joined"
I18nProjectPublicBrowse = "project.list.my.project.public.browse"
)
10 changes: 10 additions & 0 deletions pkg/erda-configs/i18n/org_list_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"zh-CN": {
"org.list.all.org.public": "公开组织",
"org.list.all.org.joined": "已加入"
},
"en-US": {
"org.list.all.org.public": "Public organization",
"org.list.all.org.joined": "Joined"
}
}
Loading