feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
package common
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
)
// Message is text shown in the web console. Key is the English source text and
// also its key in web/src/i18n/locales; Params fill its {{name}} placeholders.
// The web console translates Key with Params, so the backend only renders the
// English text, for API callers and log fallbacks. Params carry display-ready
// values: format quota with logger.FormatQuota before passing it.
type Message struct {
Key string `json:"key"`
Params map[string]any `json:"params,omitempty"`
}
func NewMessage(key string, params ...map[string]any) *Message {
message := &Message{Key: key}
if len(params) > 0 {
message.Params = params[0]
}
return message
}
// Error renders the English text, so a Message can be returned as an error and
// reach the web console through ApiError.
func (m *Message) Error() string {
if len(m.Params) == 0 {
return m.Key
}
// One pass over the key, so a value that contains "{{name}}" stays as is.
pairs := make([]string, 0, len(m.Params)*2)
for name, value := range m.Params {
pairs = append(pairs, "{{"+name+"}}", fmt.Sprint(value))
}
return strings.NewReplacer(pairs...).Replace(m.Key)
}
// Fields returns the response fields the web console translates. Callers put
// the English text in the field their response shape already uses.
func (m *Message) Fields() gin.H {
fields := gin.H{"message_key": m.Key}
if len(m.Params) > 0 {
fields["message_params"] = m.Params
}
return fields
}