Add locale support ford deprecated messages

This commit is contained in:
世界 2024-12-14 00:18:58 +08:00
parent 2ac2589d14
commit e6847ff50e
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
4 changed files with 53 additions and 4 deletions

View File

@ -1,8 +1,11 @@
package deprecated
import (
"fmt"
"github.com/sagernet/sing-box/common/badversion"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/locale"
F "github.com/sagernet/sing/common/format"
"golang.org/x/mod/semver"
@ -33,10 +36,11 @@ func (n Note) Impending() bool {
}
func (n Note) Message() string {
return F.ToString(
n.Description, " is deprecated in sing-box ", n.DeprecatedVersion,
" and will be removed in sing-box ", n.ScheduledVersion, ", please checkout documentation for migration.",
)
if n.MigrationLink != "" {
return fmt.Sprintf(locale.Current().DeprecatedMessage, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
} else {
return fmt.Sprintf(locale.Current().DeprecatedMessageNoLink, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
}
}
func (n Note) MessageWithLink() string {

View File

@ -9,6 +9,7 @@ import (
"github.com/sagernet/sing-box/common/humanize"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/locale"
_ "github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log"
)
@ -54,6 +55,10 @@ func SetupWithUsername(basePath string, workingPath string, tempPath string, use
return nil
}
func SetLocale(localeId string) {
locale.Set(localeId)
}
func Version() string {
return C.Version
}

View File

@ -0,0 +1,30 @@
package locale
var (
localeRegistry = make(map[string]*Locale)
current = defaultLocal
)
type Locale struct {
// deprecated messages for graphical clients
DeprecatedMessage string
DeprecatedMessageNoLink string
}
var defaultLocal = &Locale{
DeprecatedMessage: "%s is deprecated in sing-box %s and will be removed in sing-box %s please checkout documentation for migration.",
DeprecatedMessageNoLink: "%s is deprecated in sing-box %s and will be removed in sing-box %s.",
}
func Current() *Locale {
return current
}
func Set(localeId string) bool {
locale, loaded := localeRegistry[localeId]
if !loaded {
return false
}
current = locale
return true
}

View File

@ -0,0 +1,10 @@
package locale
var warningMessageForEndUsers = "\n\n如果您不明白此消息意味着什么您的配置文件已过时且将很快不可用。请联系您的配置提供者以更新配置。"
func init() {
localeRegistry["zh_CN"] = &Locale{
DeprecatedMessage: "%s 已在 sing-box %s 中被弃用,且将在 sing-box %s 中被移除,请参阅迁移指南。" + warningMessageForEndUsers,
DeprecatedMessageNoLink: "%s 已在 sing-box %s 中被弃用,且将在 sing-box %s 中被移除。" + warningMessageForEndUsers,
}
}