Skip to content

Commit

Permalink
Add locale support ford deprecated messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 14, 2024
1 parent ca2c7d5 commit 654a8a9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
12 changes: 8 additions & 4 deletions experimental/deprecated/constants.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions experimental/libbox/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions experimental/locale/locale.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions experimental/locale/locale_zh_CN.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 654a8a9

Please sign in to comment.