Provide the ability to localize notifications

This commit is contained in:
2025-02-16 22:59:44 +08:00
parent 50ff8304e4
commit 5fb769823a
13 changed files with 205 additions and 309 deletions

View File

@@ -2,10 +2,12 @@ package gap
import (
"fmt"
"strings"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/localize"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/rs/zerolog/log"
"strings"
"github.com/spf13/viper"
)
@@ -37,3 +39,7 @@ func InitializeToNexus() error {
return err
}
func LoadLocalization() error {
return localize.LoadLocalization(viper.GetString("locales_dir"))
}

View File

@@ -17,6 +17,8 @@ func AddProvider(in NotificationProvider) {
}
func PushNotification(in pushkit.NotificationPushRequest) error {
in.Notification = TranslateNotify(in.Notification, in.Lang)
requestId := uuid.NewString()
log.Debug().
Str("tk", in.Token).
@@ -77,10 +79,11 @@ func PushNotificationBatch(in pushkit.NotificationPushBatchRequest) {
Msg("Provider was not found, push skipped...")
continue
}
wg.Add(1)
go func() {
wg.Add(1)
defer wg.Done()
in.Notification = TranslateNotify(in.Notification, in.Lang[idx])
log.Debug().
Str("tk", in.Tokens[0]).
Str("provider", in.Providers[0]).

View File

@@ -0,0 +1,42 @@
package provider
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/localize"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
)
func TranslateNotify(nty pushkit.Notification, lang string) pushkit.Notification {
if nty.TranslateKey != nil {
return nty
}
localizeKeys := map[string]string{
"title": fmt.Sprintf("%s.%s", *nty.TranslateKey, "subject"),
"subtitle": fmt.Sprintf("%s.%s", *nty.TranslateKey, "subtitle"),
"body": fmt.Sprintf("%s.%s", *nty.TranslateKey, "body"),
}
for k, v := range localizeKeys {
tmpl := localize.L.GetLocalizedString(v, lang)
if args, ok := nty.TranslateArgs[k]; ok {
anySlice := make([]any, len(args))
for i, s := range args {
anySlice[i] = s
}
str := fmt.Sprintf(tmpl, anySlice...)
switch k {
case "title":
nty.Title = str
case "subtitle":
nty.Subtitle = str
case "body":
nty.Body = str
}
}
}
return nty
}