✨ Provide the ability to localize notifications
This commit is contained in:
@@ -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]).
|
||||
|
42
pkg/internal/provider/localizer.go
Normal file
42
pkg/internal/provider/localizer.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user