Skip to content

Commit

Permalink
the translator added to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Aug 1, 2024
1 parent 8e7cf81 commit ddbab45
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 39 deletions.
8 changes: 4 additions & 4 deletions services/activitylog/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
return
}

// FIXME: configurable default locale?
loc := l10n.MustGetUserLocale(r.Context(), activeUser.GetId().GetOpaqueId(), r.Header.Get(l10n.HeaderAcceptLanguage), s.valService)
t := l10n.NewTranslatorFromCommonConfig("en", _domain, "", _localeFS, _localeSubPath)

resp := GetActivitiesResponse{Activities: make([]libregraph.Activity, 0, len(evRes.GetEvents()))}
for _, e := range evRes.GetEvents() {
delete(toDelete, e.GetId())
Expand Down Expand Up @@ -164,10 +168,6 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
continue
}

// FIXME: configurable default locale?
loc := l10n.MustGetUserLocale(r.Context(), activeUser.GetId().GetOpaqueId(), r.Header.Get(l10n.HeaderAcceptLanguage), s.valService)
t := l10n.NewTranslatorFromCommonConfig("en", _domain, "", _localeFS, _localeSubPath)

resp.Activities = append(resp.Activities, NewActivity(t.Translate(message, loc), ts, e.GetId(), vars))
}

Expand Down
6 changes: 3 additions & 3 deletions services/graph/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ SHELL := bash
NAME := graph

# Where to write the files generated by this makefile.
OUTPUT_DIR = ./pkg/service/v0/l10n
TEMPLATE_FILE = ./pkg/service/v0/l10n/graph.pot
OUTPUT_DIR = ./pkg/l10n
TEMPLATE_FILE = ./pkg/l10n/graph.pot

include ../../.make/recursion.mk

Expand Down Expand Up @@ -45,7 +45,7 @@ l10n-push:

.PHONY: l10n-read
l10n-read: $(GO_XGETTEXT)
go-xgettext -o $(OUTPUT_DIR)/graph.pot --keyword=l10n.Template --add-comments -s pkg/service/v0/spacetemplates.go
go-xgettext -o $(OUTPUT_DIR)/graph.pot --keyword=l10n.Template --add-comments -s pkg/service/v0/spacetemplates.go -s pkg/unifiedrole/unifiedrole.go

.PHONY: l10n-write
l10n-write:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#
# Translators:
# Alex <[email protected]>, 2024
#
#
#, fuzzy
msgid ""
msgstr ""
Expand All @@ -25,3 +25,38 @@ msgstr ""
#: pkg/service/v0/spacetemplates.go:37
msgid "Here you can add a description for this Space."
msgstr "Здесь вы можете добавить описание этого пространства."

#: pkg/unifiedrole/unifiedrole.go:87
msgid "Can edit"
msgstr ""

#. UnifiedRole Viewer, Role DisplayName (resolves directly)
#: pkg/unifiedrole/unifiedrole.go:79
msgid "Can view"
msgstr ""

#. default description for new spaces
#: pkg/service/v0/spacetemplates.go:31
msgid "Here you can add a description for this Space."
msgstr ""

#. UnifiedRole Viewer, Role Description (resolves directly)
#. UnifiedRole SpaceViewer, Role Description (resolves directly)
#: pkg/unifiedrole/unifiedrole.go:77 pkg/unifiedrole/unifiedrole.go:82
msgid "View and download."
msgstr ""

#: pkg/unifiedrole/unifiedrole.go:91
msgid "View, download and edit."
msgstr ""

#: pkg/unifiedrole/unifiedrole.go:92
msgid "View, download and upload."
msgstr ""

#. canEdit := "Can edit"
#. UnifiedRole Editor, Role Description (resolves directly)
#. UnifiedRole SpaseEditor, Role Description (resolves directly)
#: pkg/unifiedrole/unifiedrole.go:86 pkg/unifiedrole/unifiedrole.go:90
msgid "View, download, upload, edit, add and delete."
msgstr ""
30 changes: 30 additions & 0 deletions services/graph/pkg/l10n/translation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package l10n

import (
"embed"

l10npkg "github.com/owncloud/ocis/v2/ocis-pkg/l10n"
)

var (
//go:embed locale
_localeFS embed.FS
)

const (
// subfolder where the translation files are stored
_localeSubPath = "locale"

// domain of the graph service (transifex)
_domain = "graph"
)

func Translate(content, locale, defaultLocale string) string {
t := l10npkg.NewTranslatorFromCommonConfig(defaultLocale, _domain, "", _localeFS, _localeSubPath)
return t.Translate(content, locale)
}

func NewTranslateLocation(locale, defaultLocale string) func(string, ...any) string {
t := l10npkg.NewTranslatorFromCommonConfig(defaultLocale, _domain, "", _localeFS, _localeSubPath)
return t.Locale(locale).Get
}
34 changes: 34 additions & 0 deletions services/graph/pkg/service/v0/api_driveitem_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
l10n2 "github.com/owncloud/ocis/v2/services/graph/pkg/l10n"

"github.com/owncloud/ocis/v2/ocis-pkg/conversions"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
Expand Down Expand Up @@ -632,6 +634,22 @@ func (api DriveItemPermissionsApi) ListPermissions(w http.ResponseWriter, r *htt
return
}

loc := r.Header.Get(l10n.HeaderAcceptLanguage)
w.Header().Add("Content-Language", loc)
if loc != "" && loc != "en" {
trf := l10n2.NewTranslateLocation(loc, "en")
for i, role := range permissions.LibreGraphPermissionsRolesAllowedValues {
err := l10n.TranslateEntity(&role, trf,
l10n.TranslateField("Description"),
l10n.TranslateField("DisplayName"))
if err != nil {
api.logger.Warn().Err(err).Msg("tranlation error")
continue
}
permissions.LibreGraphPermissionsRolesAllowedValues[i] = role
}
}

render.Status(r, http.StatusOK)
render.JSON(w, r, permissions)
}
Expand All @@ -653,6 +671,22 @@ func (api DriveItemPermissionsApi) ListSpaceRootPermissions(w http.ResponseWrite
return
}

loc := r.Header.Get(l10n.HeaderAcceptLanguage)
w.Header().Add("Content-Language", loc)
if loc != "" && loc != "en" {
trf := l10n2.NewTranslateLocation(loc, "en")
for i, role := range permissions.LibreGraphPermissionsRolesAllowedValues {
err := l10n.TranslateEntity(&role, trf,
l10n.TranslateField("Description"),
l10n.TranslateField("DisplayName"))
if err != nil {
api.logger.Warn().Err(err).Msg("tranlation error")
continue
}
permissions.LibreGraphPermissionsRolesAllowedValues[i] = role
}
}

render.Status(r, http.StatusOK)
render.JSON(w, r, permissions)
}
Expand Down
13 changes: 2 additions & 11 deletions services/graph/pkg/service/v0/spacetemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ import (
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
l10n_pkg "github.com/owncloud/ocis/v2/services/graph/pkg/l10n"
)

var (
//go:embed spacetemplate/*
_spaceTemplateFS embed.FS

//go:embed l10n/locale
_localeFS embed.FS

// subfolder where the translation files are stored
_localeSubPath = "l10n/locale"

// name of the secret space folder
_spaceFolderName = ".space"

Expand All @@ -39,9 +34,6 @@ var (
// name of the readme.md file
_readmeName = "readme.md"

// domain of the graph service (transifex)
_domain = "graph"

// HeaderAcceptLanguage is the header key for the accept-language header
HeaderAcceptLanguage = "Accept-Language"

Expand Down Expand Up @@ -121,10 +113,9 @@ func imageUpload(ctx context.Context, mdc *metadata.CS3) (string, error) {
}

func readmeUpload(ctx context.Context, mdc *metadata.CS3, locale string, defaultLocale string) (string, error) {
t := l10n.NewTranslatorFromCommonConfig(defaultLocale, _domain, "", _localeFS, _localeSubPath)
res, err := mdc.Upload(ctx, metadata.UploadRequest{
Path: filepath.Join(_spaceFolderName, _readmeName),
Content: []byte(t.Translate(_readmeText, locale)),
Content: []byte(l10n_pkg.Translate(_readmeText, locale, defaultLocale)),
})
if err != nil {
return "", err
Expand Down
78 changes: 59 additions & 19 deletions services/graph/pkg/unifiedrole/unifiedrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
"google.golang.org/protobuf/proto"

"github.com/cs3org/reva/v2/pkg/conversions"
Expand Down Expand Up @@ -71,12 +72,54 @@ var legacyNames map[string]string = map[string]string{
UnifiedRoleSecureViewerID: conversions.RoleSecureViewer,
}

var (
// UnifiedRole Viewer, Role Description (resolves directly)
_viewerUnifiedRoleDescription = l10n.Template("View and download.")
// UnifiedRole Viewer, Role DisplayName (resolves directly)
_viewerUnifiedRoleDisplayName = l10n.Template("Can view")

// UnifiedRole SpaceViewer, Role Description (resolves directly)
_spaceViewerUnifiedRoleDescription = l10n.Template("View and download.")
// UnifiedRole SpaseViewer, Role DisplayName (resolves directly)
_spaceViewerUnifiedRoleDisplayName = l10n.Template("Can view")

// UnifiedRole Editor, Role Description (resolves directly)
_editorUnifiedRoleDescription = l10n.Template("View, download, upload, edit, add and delete.")
// UnifiedRole Editor, Role DisplayName (resolves directly)
_editorUnifiedRoleDisplayName = l10n.Template("Can edit")

// UnifiedRole SpaseEditor, Role Description (resolves directly)
_spaceEditorUnifiedRoleDescription = l10n.Template("View, download, upload, edit, add and delete.")
// UnifiedRole SpaseEditor, Role DisplayName (resolves directly)
_spaceEditorUnifiedRoleDisplayName = l10n.Template("Can edit")

// UnifiedRole FileEditor, Role Description (resolves directly)
_fileEditorUnifiedRoleDescription = l10n.Template("View, download and edit.")
// UnifiedRole FileEditor, Role DisplayName (resolves directly)
_fileEditorUnifiedRoleDisplayName = l10n.Template("Can edit")

// UnifiedRole EditorLite, Role Description (resolves directly)
_editorLiteUnifiedRoleDescription = l10n.Template("View, download and upload.")
// UnifiedRole EditorLite, Role DisplayName (resolves directly)
_editorLiteUnifiedRoleDisplayName = l10n.Template("Can upload")

// UnifiedRole Manager, Role Description (resolves directly)
_managerUnifiedRoleDescription = l10n.Template("View, download, upload, edit, add, delete and manage members.")
// UnifiedRole Manager, Role DisplayName (resolves directly)
_managerUnifiedRoleDisplayName = l10n.Template("Can manage")

// UnifiedRole SecureViewer, Role Description (resolves directly)
_secureViewerUnifiedRoleDescription = l10n.Template("View only documents, images and PDFs. Watermarks will be applied.")
// UnifiedRole SecureViewer, Role DisplayName (resolves directly)
_secureViewerUnifiedRoleDisplayName = l10n.Template("Can view (secure)")
)

// NewViewerUnifiedRole creates a viewer role.
func NewViewerUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewViewerRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleViewerID),
Description: proto.String("View and download."),
Description: proto.String(_viewerUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -97,7 +140,7 @@ func NewSpaceViewerUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewSpaceViewerRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleSpaceViewerID),
Description: proto.String("View and download."),
Description: proto.String(_spaceViewerUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -114,7 +157,7 @@ func NewEditorUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewEditorRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleEditorID),
Description: proto.String("View, download, upload, edit, add and delete."),
Description: proto.String(_editorUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -131,7 +174,7 @@ func NewSpaceEditorUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewSpaceEditorRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleSpaceEditorID),
Description: proto.String("View, download, upload, edit, add and delete."),
Description: proto.String(_spaceEditorUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -148,7 +191,7 @@ func NewFileEditorUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewFileEditorRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleFileEditorID),
Description: proto.String("View, download and edit."),
Description: proto.String(_fileEditorUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -165,7 +208,7 @@ func NewEditorLiteUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewEditorLiteRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleEditorLiteID),
Description: proto.String("View, download and upload."),
Description: proto.String(_editorLiteUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -182,7 +225,7 @@ func NewManagerUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewManagerRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleManagerID),
Description: proto.String("View, download, upload, edit, add, delete and manage members."),
Description: proto.String(_managerUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand All @@ -199,7 +242,7 @@ func NewSecureViewerUnifiedRole() *libregraph.UnifiedRoleDefinition {
r := conversions.NewSecureViewerRole()
return &libregraph.UnifiedRoleDefinition{
Id: proto.String(UnifiedRoleSecureViewerID),
Description: proto.String("View only documents, images and PDFs. Watermarks will be applied."),
Description: proto.String(_secureViewerUnifiedRoleDescription),
DisplayName: displayName(r),
RolePermissions: []libregraph.UnifiedRolePermission{
{
Expand Down Expand Up @@ -482,27 +525,24 @@ func displayName(role *conversions.Role) *string {
return nil
}

// linter wants this to be a var
canEdit := "Can edit"

var displayName string
switch role.Name {
case conversions.RoleViewer:
displayName = "Can view"
displayName = _viewerUnifiedRoleDisplayName
case conversions.RoleSpaceViewer:
displayName = "Can view"
displayName = _spaceViewerUnifiedRoleDisplayName
case conversions.RoleEditor:
displayName = canEdit
displayName = _editorUnifiedRoleDisplayName
case conversions.RoleSpaceEditor:
displayName = canEdit
displayName = _spaceEditorUnifiedRoleDisplayName
case conversions.RoleFileEditor:
displayName = canEdit
displayName = _fileEditorUnifiedRoleDisplayName
case conversions.RoleEditorLite:
displayName = "Can upload"
displayName = _editorLiteUnifiedRoleDisplayName
case conversions.RoleManager:
displayName = "Can manage"
displayName = _managerUnifiedRoleDisplayName
case conversions.RoleSecureViewer:
displayName = "Can view (secure)"
displayName = _secureViewerUnifiedRoleDisplayName
default:
return nil
}
Expand Down

0 comments on commit ddbab45

Please sign in to comment.