refactor(plugin): try to refactor plugin translation struct

This commit is contained in:
LinkinStars 2023-02-22 16:49:03 +08:00
parent ffb1e07acb
commit 5bd8b1895b
5 changed files with 58 additions and 12 deletions

View File

@ -114,8 +114,9 @@ func (cc *ConnectorController) ConnectorsInfo(ctx *gin.Context) {
resp := make([]*schema.ConnectorInfoResp, 0)
_ = plugin.CallConnector(func(fn plugin.Connector) error {
connectorName := fn.ConnectorName()
resp = append(resp, &schema.ConnectorInfoResp{
Name: fn.ConnectorName()(ctx),
Name: connectorName.Translate(ctx),
Icon: fn.ConnectorLogoSVG(),
Link: fmt.Sprintf("%s%s%s%s", general.SiteUrl,
commonRouterPrefix, ConnectorLoginRouterPrefix, fn.ConnectorSlugName()),
@ -174,8 +175,9 @@ func (cc *ConnectorController) ConnectorsUserInfo(ctx *gin.Context) {
resp := make([]*schema.ConnectorUserInfoResp, 0)
_ = plugin.CallConnector(func(fn plugin.Connector) error {
externalID := userExternalLoginMapping[fn.ConnectorSlugName()]
connectorName := fn.ConnectorName()
resp = append(resp, &schema.ConnectorUserInfoResp{
Name: fn.ConnectorName()(ctx),
Name: connectorName.Translate(ctx),
Icon: fn.ConnectorLogoSVG(),
Link: fmt.Sprintf("%s%s%s%s", general.SiteUrl,
commonRouterPrefix, ConnectorLoginRouterPrefix, fn.ConnectorSlugName()),

View File

@ -49,9 +49,9 @@ func (pc *PluginController) GetPluginList(ctx *gin.Context) {
_ = plugin.CallBase(func(base plugin.Base) error {
info := base.Info()
resp = append(resp, &schema.GetPluginListResp{
Name: info.Name(ctx),
Name: info.Name.Translate(ctx),
SlugName: info.SlugName,
Description: info.Description(ctx),
Description: info.Description.Translate(ctx),
Version: info.Version,
Enabled: plugin.StatusManager.IsEnabled(info.SlugName),
HaveConfig: pluginConfigMapping[info.SlugName],
@ -133,9 +133,9 @@ func (pc *PluginController) GetPluginConfig(ctx *gin.Context) {
return nil
}
info := base.Info()
resp.Name = info.Name(ctx)
resp.Name = info.Name.Translate(ctx)
resp.SlugName = info.SlugName
resp.Description = info.Description(ctx)
resp.Description = info.Description.Translate(ctx)
resp.Version = info.Version
return nil
})

View File

@ -48,8 +48,8 @@ func (g *GetPluginConfigResp) SetConfigFields(ctx *gin.Context, fields []plugin.
configField := ConfigField{
Name: field.Name,
Type: string(field.Type),
Title: field.Title(ctx),
Description: field.Description(ctx),
Title: field.Title.Translate(ctx),
Description: field.Description.Translate(ctx),
Required: field.Required,
Value: field.Value,
UIOptions: ConfigFieldUIOptions{
@ -57,13 +57,11 @@ func (g *GetPluginConfigResp) SetConfigFields(ctx *gin.Context, fields []plugin.
InputType: string(field.UIOptions.InputType),
},
}
if field.UIOptions.Placeholder != nil {
configField.UIOptions.Placeholder = field.UIOptions.Placeholder(ctx)
}
configField.UIOptions.Placeholder = field.UIOptions.Placeholder.Translate(ctx)
for _, option := range field.Options {
configField.Options = append(configField.Options, ConfigFieldOption{
Label: option.Label(ctx),
Label: option.Label.Translate(ctx),
Value: option.Value,
})
}

View File

@ -0,0 +1,20 @@
package sample
import "github.com/answerdev/answer/plugin"
type Connector struct {
}
func init() {
plugin.Register(&Connector{})
}
func (g *Connector) Info() plugin.Info {
return plugin.Info{
Name: plugin.MakeTranslator("plugin.connector.name"),
SlugName: "connector",
//Description: plugin.MakeTranslator("plugin.connector.description"),
Author: "answerdev",
Version: "0.0.1",
}
}

View File

@ -0,0 +1,26 @@
package sample
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/answerdev/answer/plugin"
"github.com/gin-gonic/gin"
)
func TestConnector_Info(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("", "", nil)
c.Request.Header.Set("Accept-Language", "en_US")
_ = plugin.CallBase(func(base plugin.Base) error {
info := base.Info()
fmt.Println(info.Name.Translate(c))
fmt.Println(info.SlugName)
fmt.Println(info.Description.Translate(c))
fmt.Println(info.Author)
fmt.Println(info.Version)
return nil
})
}