Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout. Co-authored-by: Cursor <cursoragent@cursor.com>
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package i18n
|
|
|
|
import "strings"
|
|
|
|
func T(lang Lang, key string, args map[string]string) string {
|
|
if key == "" {
|
|
return ""
|
|
}
|
|
if lang == "" {
|
|
lang = Default
|
|
}
|
|
s := lookup(lang, key)
|
|
if s == "" && lang != En {
|
|
s = lookup(En, key)
|
|
}
|
|
if s == "" && lang != ZhCN {
|
|
s = lookup(ZhCN, key)
|
|
}
|
|
if s == "" {
|
|
return key
|
|
}
|
|
return apply(s, args)
|
|
}
|
|
|
|
func lookup(lang Lang, key string) string {
|
|
byLang, ok := catalog[key]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return byLang[lang]
|
|
}
|
|
|
|
func apply(s string, args map[string]string) string {
|
|
for k, v := range args {
|
|
s = strings.ReplaceAll(s, "{"+k+"}", v)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// ErrorMsg is the user-visible envelope msg for a business code.
|
|
func ErrorMsg(lang Lang, code string) string {
|
|
if code == "" || code == "0" {
|
|
return T(lang, KeyOK, nil)
|
|
}
|
|
return T(lang, "err."+code, nil)
|
|
}
|
|
|
|
// Notify returns title and body for a notification type. Missing ".body" falls back to the type key.
|
|
func Notify(lang Lang, typ string, args map[string]string) (title, body string) {
|
|
title = T(lang, typ+".title", args)
|
|
if title == typ+".title" {
|
|
title = ""
|
|
}
|
|
body = T(lang, typ+".body", args)
|
|
if body == typ+".body" {
|
|
body = T(lang, typ, args)
|
|
if body == typ {
|
|
body = ""
|
|
}
|
|
}
|
|
return title, body
|
|
}
|