added the translation

This commit is contained in:
Roman Perekhod
2024-09-23 10:37:23 +02:00
parent b65355c03c
commit 1a23ea5a78
13 changed files with 146 additions and 63 deletions
+6 -5
View File
@@ -115,6 +115,9 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
vars map[string]interface{}
)
loc := l10n.MustGetUserLocale(r.Context(), activeUser.GetId().GetOpaqueId(), r.Header.Get(l10n.HeaderAcceptLanguage), s.valService)
t := l10n.NewTranslatorFromCommonConfig(s.cfg.DefaultLanguage, _domain, s.cfg.TranslationPath, _localeFS, _localeSubPath)
switch ev := s.unwrapEvent(e).(type) {
case nil:
// error already logged in unwrapEvent
@@ -158,7 +161,7 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
}
message = MessageShareUpdated
ts = utils.TSToTime(ev.MTime)
vars, err = s.GetVars(ctx, WithResource(toRef(ev.ItemID), false), WithUser(ev.Executant, ""), WithFieldMask(ev.UpdateMask))
vars, err = s.GetVars(ctx, WithResource(toRef(ev.ItemID), false), WithUser(ev.Executant, ""), WithTranslation(&t, loc, "field", ev.UpdateMask))
case events.ShareRemoved:
message = MessageShareDeleted
ts = ev.Timestamp
@@ -176,7 +179,8 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
vars, err = s.GetVars(ctx,
WithResource(toRef(ev.ItemID), false),
WithUser(ev.Executant, ""),
WithLinkFieldUpdated(&ev))
WithTranslation(&t, loc, "field", []string{ev.FieldUpdated}),
WithVar("token", ev.ItemID.GetOpaqueId(), ev.Token))
case events.LinkRemoved:
message = MessageLinkDeleted
ts = utils.TSToTime(ev.Timestamp)
@@ -196,9 +200,6 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
continue
}
loc := l10n.MustGetUserLocale(r.Context(), activeUser.GetId().GetOpaqueId(), r.Header.Get(l10n.HeaderAcceptLanguage), s.valService)
t := l10n.NewTranslatorFromCommonConfig(s.cfg.DefaultLanguage, _domain, s.cfg.TranslationPath, _localeFS, _localeSubPath)
resp.Activities = append(resp.Activities, NewActivity(t.Translate(message, loc), ts, e.GetId(), vars))
}
+46 -28
View File
@@ -3,7 +3,6 @@ package service
import (
"context"
"fmt"
"github.com/cs3org/reva/v2/pkg/events"
"path/filepath"
"strings"
"time"
@@ -35,6 +34,22 @@ var (
MessageLinkDeleted = l10n.Template("{user} removed link to {resource}")
MessageSpaceShared = l10n.Template("{user} added {sharee} as member of {space}")
MessageSpaceUnshared = l10n.Template("{user} removed {sharee} from {space}")
StrSomeField = l10n.Template(_someField)
StrPermission = l10n.Template(_permission)
StrPassword = l10n.Template(_password)
StrExpirationDate = l10n.Template(_expirationDate)
StrDisplayName = l10n.Template(_displayName)
StrDescription = l10n.Template(_description)
)
const (
_someField = "some field"
_permission = "permission"
_password = "password"
_expirationDate = "expiration date"
_displayName = "display name"
_description = "description"
)
// GetActivitiesResponse is the response on GET activities requests
@@ -230,42 +245,29 @@ func WithSpace(spaceid *provider.StorageSpaceId) ActivityOption {
}
}
// WithLinkFieldUpdated sets the field and token variables for an activity
func WithLinkFieldUpdated(e *events.LinkUpdated) ActivityOption {
// WithTranslation sets a variable that translation is needed for
func WithTranslation(t *l10n.Translator, locale string, key string, values []string) ActivityOption {
return func(_ context.Context, _ gateway.GatewayAPIClient, vars map[string]interface{}) error {
f := "some field"
switch e.FieldUpdated {
case "TYPE_PERMISSIONS":
f = "permission"
case "TYPE_PASSWORD":
f = "password"
case "TYPE_EXPIRATION":
f = "expiration date"
case "TYPE_DISPLAYNAME":
f = "display name"
case "TYPE_DESCRIPTION":
f = "description"
f := t.Translate(StrSomeField, locale)
if len(values) > 0 {
for i := range values {
values[i] = t.Translate(mapField(values[i]), locale)
}
f = strings.Join(values, ", ")
}
vars["field"] = Resource{
ID: e.ItemID.GetOpaqueId(),
vars[key] = Resource{
Name: f,
}
vars["token"] = Resource{
ID: e.ItemID.GetOpaqueId(),
Name: e.Token,
}
return nil
}
}
func WithFieldMask(mask []string) ActivityOption {
// WithVar sets a variable for an activity
func WithVar(key, id, name string) ActivityOption {
return func(_ context.Context, _ gateway.GatewayAPIClient, vars map[string]interface{}) error {
f := "some field"
if len(mask) > 0 {
f = strings.Join(mask, ", ")
}
vars["field"] = Resource{
Name: f,
vars[key] = Resource{
ID: id,
Name: name,
}
return nil
}
@@ -299,3 +301,19 @@ func (s *ActivitylogService) GetVars(ctx context.Context, opts ...ActivityOption
return vars, nil
}
func mapField(val string) string {
switch val {
case "TYPE_PERMISSIONS", "permission":
return _permission
case "TYPE_PASSWORD", "password":
return _password
case "TYPE_EXPIRATION", "expiration":
return _expirationDate
case "TYPE_DISPLAYNAME":
return _displayName
case "TYPE_DESCRIPTION":
return _description
}
return _someField
}
+1 -1
View File
@@ -115,7 +115,7 @@ func ShareUpdated(ev events.ShareUpdated) AuditEventShareUpdated {
func LinkUpdated(ev events.LinkUpdated) AuditEventShareUpdated {
uid := ev.Sharer.OpaqueId
with, typ := "", _linktype
base := BasicAuditEvent(uid, formatTime(ev.MTime), MessageLinkUpdated(uid, ev.ShareID.OpaqueId, ev.FieldUpdated), updateType(ev.FieldUpdated))
base := BasicAuditEvent(uid, formatTime(ev.MTime), MessageLinkUpdated(uid, ev.ShareID.GetOpaqueId(), ev.FieldUpdated), updateType(ev.FieldUpdated))
return AuditEventShareUpdated{
AuditEventSharing: SharingAuditEvent(ev.ShareID.GetOpaqueId(), ev.ItemID.OpaqueId, uid, base),
ShareOwner: uid,