Skip to content

Commit

Permalink
feat(settings): translate notifications settings
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jan 2, 2025
1 parent 1d6d905 commit 7947c99
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ L10N_MODULES := \
services/activitylog \
services/graph \
services/notifications \
services/userlog
services/userlog \
services/settings

# if you add a module here please also add it to the .drone.star file
OCIS_MODULES = \
Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/notification-settings-translations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Translate Notification Settings

Translates the notification settings according to the users language preference.

https://github.com/owncloud/ocis/pull/10812
4 changes: 2 additions & 2 deletions ocis-pkg/l10n/l10n.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/leonelquinteros/gotext"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/settings/pkg/store/defaults"
micrometadata "go-micro.dev/v4/metadata"
)

Expand Down Expand Up @@ -140,7 +139,8 @@ func GetUserLocale(ctx context.Context, userID string, vc settingssvc.ValueServi
micrometadata.Set(ctx, middleware.AccountID, userID),
&settingssvc.GetValueByUniqueIdentifiersRequest{
AccountUuid: userID,
SettingId: defaults.SettingUUIDProfileLanguage,
// this defaults.SettingUUIDProfileLanguage. Copied here to avoid import cycles.
SettingId: "aa8cfbe5-95d4-4f7e-a032-c3c01f5f062f",
},
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions protogen/proto/ocis/services/settings/v0/settings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ message GetBundleResponse {

message ListBundlesRequest {
repeated string bundle_ids = 1;
string locale = 2;
}

message ListBundlesResponse {
Expand Down
26 changes: 26 additions & 0 deletions services/settings/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
SHELL := bash
NAME := settings

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

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

############ tooling ############
Expand Down Expand Up @@ -45,3 +49,25 @@ ci-node-check-licenses:

.PHONY: ci-node-save-licenses
ci-node-save-licenses:

############ translations ########
.PHONY: l10n-pull
l10n-pull:
cd $(OUTPUT_DIR) && tx pull --all --force --skip --minimum-perc=75

.PHONY: l10n-push
l10n-push:
cd $(OUTPUT_DIR) && tx push -s --skip

.PHONY: l10n-read
l10n-read: $(GO_XGETTEXT)
go-xgettext -o $(OUTPUT_DIR)/settings.pot \
--keyword=l10n.Template --add-comments -s \
pkg/store/defaults/templates.go

.PHONY: l10n-write
l10n-write:

.PHONY: l10n-clean
l10n-clean:
rm -f $(TEMPLATE_FILE);
22 changes: 22 additions & 0 deletions services/settings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ Services can set or query Infinite Scale *setting values* of a user from setting

The settings service needs to know the IDs of service accounts but it doesn't need their secrets. They can be configured using the `SETTINGS_SERVICE_ACCOUNTS_IDS` envvar. When only using one service account `OCIS_SERVICE_ACCOUNT_ID` can also be used. All configured service accounts will get a hidden 'service-account' role. This role contains all permissions the service account needs but will not appear calls to the list roles endpoint. It is not possible to assign the 'service-account' role to a normal user.

## Translations

The `settings` service has embedded translations sourced via transifex to provide a basic set of translated languages. These embedded translations are available for all deployment scenarios. In addition, the service supports custom translations, though it is currently not possible to just add custom translations to embedded ones. If custom translations are configured, the embedded ones are not used. To configure custom translations, the `SETTINGS_TRANSLATION_PATH` environment variable needs to point to a base folder that will contain the translation files. This path must be available from all instances of the userlog service, a shared storage is recommended. Translation files must be of type [.po](https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html#PO-Files) or [.mo](https://www.gnu.org/software/gettext/manual/html_node/Binaries.html). For each language, the filename needs to be `settings.po` (or `settings.mo`) and stored in a folder structure defining the language code. In general the path/name pattern for a translation file needs to be:

```text
{SETTINGS_TRANSLATION_PATH}/{language-code}/LC_MESSAGES/settings.po
```

The language code pattern is composed of `language[_territory]` where `language` is the base language and `_territory` is optional and defines a country.

For example, for the language `de`, one needs to place the corresponding translation files to `{SETTINGS_TRANSLATION_PATH}/de_DE/LC_MESSAGES/settings.po`.

<!-- also see the notifications readme -->

Important: For the time being, the embedded ownCloud Web frontend only supports the main language code but does not handle any territory. When strings are available in the language code `language_territory`, the web frontend does not see it as it only requests `language`. In consequence, any translations made must exist in the requested `language` to avoid a fallback to the default.

### Translation Rules

* If a requested language code is not available, the service tries to fall back to the base language if available. For example, if the requested language-code `de_DE` is not available, the service tries to fall back to translations in the `de` folder.
* If the base language `de` is also not available, the service falls back to the system's default English (`en`),
which is the source of the texts provided by the code.

## Default Language

The default language can be defined via the `OCIS_DEFAULT_LANGUAGE` environment variable. If this variable is not defined, English will be used as default. The value has the ISO 639-1 format ("de", "en", etc.) and is limited by the list supported languages. This setting can be used to set the default language for notification and invitation emails.
Expand Down
1 change: 1 addition & 0 deletions services/settings/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
ServiceAccountIDs []string `yaml:"service_account_ids" env:"SETTINGS_SERVICE_ACCOUNT_IDS;OCIS_SERVICE_ACCOUNT_ID" desc:"The list of all service account IDs. These will be assigned the hidden 'service-account' role. Note: When using 'OCIS_SERVICE_ACCOUNT_ID' this will contain only one value while 'SETTINGS_SERVICE_ACCOUNT_IDS' can have multiple. See the 'auth-service' service description for more details about service accounts." introductionVersion:"5.0"`

DefaultLanguage string `yaml:"default_language" env:"OCIS_DEFAULT_LANGUAGE" desc:"The default language used by services and the WebUI. If not defined, English will be used as default. See the documentation for more details." introductionVersion:"5.0"`
TranslationPath string `yaml:"translation_path" env:"OCIS_TRANSLATION_PATH;SETTINGS_TRANSLATION_PATH" desc:"(optional) Set this to a path with custom translations to overwrite the builtin translations. Note that file and folder naming rules apply, see the documentation for more details." introductionVersion:"7.0"`

Context context.Context `yaml:"-"`
}
Expand Down
10 changes: 10 additions & 0 deletions services/settings/pkg/service/v0/l10n/.tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[main]
host = https://www.transifex.com

[o:owncloud-org:p:owncloud:r:ocis-settings]
file_filter = locale/<lang>/LC_MESSAGES/settings.po
minimum_perc = 75
resource_name = ocis-settings
source_file = settings.pot
source_lang = en
type = PO
Empty file.
77 changes: 74 additions & 3 deletions services/settings/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package svc

import (
"context"
"embed"
"errors"
"fmt"
"strings"

cs3permissions "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/rgrpc/status"
"github.com/leonelquinteros/gotext"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
"github.com/owncloud/ocis/v2/ocis-pkg/roles"
Expand All @@ -23,6 +27,11 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)

//go:embed l10n/locale
var _translationFS embed.FS

var _domain = "settings"

// Service represents a service.
type Service struct {
id string
Expand Down Expand Up @@ -134,18 +143,29 @@ func (g Service) ListBundles(ctx context.Context, req *settingssvc.ListBundlesRe
if validationError := validateListBundles(req); validationError != nil {
return merrors.BadRequest(g.id, "%s", validationError)
}
bundles, err := g.manager.ListBundles(settingsmsg.Bundle_TYPE_DEFAULT, req.BundleIds)
bundles, err := g.manager.ListBundles(settingsmsg.Bundle_TYPE_DEFAULT, req.GetBundleIds())
if err != nil {
return merrors.NotFound(g.id, "%s", err)
}
roleIDs := g.getRoleIDs(ctx)

// find user locale
var locale string
if u, ok := ctxpkg.ContextGetUser(ctx); ok {
var err error
locale, err = g.getUserLocale(ctx, u.GetId().GetOpaqueId())
if err != nil {
g.logger.Error().Err(err).Str("userid", u.GetId().GetOpaqueId()).Msg("failed to get user locale")
}
}

// filter settings in bundles that are allowed according to roles
var filteredBundles []*settingsmsg.Bundle
for _, bundle := range bundles {
filteredBundle := g.getFilteredBundle(roleIDs, bundle)
if len(filteredBundle.Settings) > 0 {
filteredBundles = append(filteredBundles, filteredBundle)
if len(filteredBundle.GetSettings()) > 0 {
t := l10n.NewTranslatorFromCommonConfig(g.config.DefaultLanguage, _domain, g.config.TranslationPath, _translationFS, "l10n/locale").Locale(locale)
filteredBundles = append(filteredBundles, translateBundle(filteredBundle, t))
}
}

Expand Down Expand Up @@ -642,7 +662,58 @@ func (g Service) canManageRoles(ctx context.Context) bool {
return g.hasStaticPermission(ctx, RoleManagementPermissionID)
}

func (g Service) getUserLocale(ctx context.Context, userID string) (string, error) {
var resp settingssvc.GetValueResponse
err := g.GetValueByUniqueIdentifiers(
ctx,
&settingssvc.GetValueByUniqueIdentifiersRequest{
AccountUuid: userID,
SettingId: defaults.SettingUUIDProfileLanguage,
},
&resp,
)
if err != nil {
return "", err
}
val := resp.GetValue().GetValue().GetListValue().GetValues()
if len(val) == 0 {
return "", errors.New("no language setting found")
}
return val[0].GetStringValue(), nil
}

func formatPermissionName(setting *settingsmsg.Setting) string {
constraint := strings.TrimPrefix(setting.GetPermissionValue().GetConstraint().String(), "CONSTRAINT_")
return setting.Name + "." + strings.ToLower(constraint)
}

func translateBundle(bundle *settingsmsg.Bundle, t *gotext.Locale) *settingsmsg.Bundle {
for i, set := range bundle.GetSettings() {
switch set.GetId() {
default:
continue
case defaults.SettingUUIDProfileEmailSendingInterval:
// translate interval names ('Instant', 'Daily', 'Weekly', 'Never')
value := set.GetSingleChoiceValue()
for i, v := range value.GetOptions() {
value.Options[i].DisplayValue = t.Get(v.GetDisplayValue())
}
set.Value = &settingsmsg.Setting_SingleChoiceValue{SingleChoiceValue: value}
fallthrough
case defaults.SettingUUIDProfileEventShareCreated,
defaults.SettingUUIDProfileEventShareRemoved,
defaults.SettingUUIDProfileEventShareExpired,
defaults.SettingUUIDProfileEventSpaceShared,
defaults.SettingUUIDProfileEventSpaceUnshared,
defaults.SettingUUIDProfileEventSpaceMembershipExpired,
defaults.SettingUUIDProfileEventSpaceDisabled,
defaults.SettingUUIDProfileEventSpaceDeleted:
// translate event names ('Share Received', 'Share Removed', ...)
set.DisplayName = t.Get(set.GetDisplayName())
// translate event descriptions ('Notify me when I receive a share', ...)
set.Description = t.Get(set.GetDescription())
bundle.Settings[i] = set
}
}
return bundle
}
48 changes: 24 additions & 24 deletions services/settings/pkg/store/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEmailSendingInterval,
Name: "email-sending-interval-options",
DisplayName: "Email Notifications options",
Description: "Email notifications options",
DisplayName: TemplateEmailSendingInterval,
Description: TemplateEmailSendingIntervalDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -292,8 +292,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventShareCreated,
Name: "event-share-created-options",
DisplayName: "Share created",
Description: "Share created",
DisplayName: TemplateShareCreated,
Description: TemplateShareCreatedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -309,8 +309,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventShareRemoved,
Name: "event-share-removed-options",
DisplayName: "Share removed",
Description: "Share removed",
DisplayName: TemplateShareRemoved,
Description: TemplateShareRemovedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -326,8 +326,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventShareExpired,
Name: "event-share-expired-options",
DisplayName: "Share expired",
Description: "Share expired",
DisplayName: TemplateShareExpired,
Description: TemplateShareExpiredDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -343,8 +343,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventSpaceShared,
Name: "event-space-shared-options",
DisplayName: "Space shared",
Description: "Space shared",
DisplayName: TemplateSpaceShared,
Description: TemplateSpaceSharedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -360,8 +360,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventSpaceUnshared,
Name: "event-space-unshared-options",
DisplayName: "Space unshared",
Description: "Space unshared",
DisplayName: TemplateSpaceUnshared,
Description: TemplateSpaceUnsharedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -377,8 +377,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventSpaceMembershipExpired,
Name: "event-space-membership-expired-options",
DisplayName: "Space membership expired",
Description: "Space membership expired",
DisplayName: TemplateSpaceMembershipExpired,
Description: TemplateSpaceMembershipExpiredDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -394,8 +394,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventSpaceDisabled,
Name: "event-space-disabled-options",
DisplayName: "Space disabled",
Description: "Space disabled",
DisplayName: TemplateSpaceDisabled,
Description: TemplateSpaceDisabledDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -411,8 +411,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventSpaceDeleted,
Name: "event-space-deleted-options",
DisplayName: "Space deleted",
Description: "Space deleted",
DisplayName: TemplateSpaceDeleted,
Description: TemplateSpaceDeletedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -428,8 +428,8 @@ func generateBundleProfileRequest() *settingsmsg.Bundle {
{
Id: SettingUUIDProfileEventPostprocessingStepFinished,
Name: "event-postprocessing-step-finished-options",
DisplayName: "Postprocessing Step Finished",
Description: "Postprocessing Step Finished",
DisplayName: TemplateFileRejected,
Description: TemplateFileRejectedDescription,
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_USER,
},
Expand All @@ -455,7 +455,7 @@ var sendEmailOptions = settingsmsg.Setting_SingleChoiceValue{
StringValue: "instant",
},
},
DisplayValue: "Instant",
DisplayValue: TemplateIntervalInstant,
Default: true,
},
{
Expand All @@ -464,23 +464,23 @@ var sendEmailOptions = settingsmsg.Setting_SingleChoiceValue{
StringValue: "daily",
},
},
DisplayValue: "Daily",
DisplayValue: TemplateIntervalDaily,
},
{
Value: &settingsmsg.ListOptionValue{
Option: &settingsmsg.ListOptionValue_StringValue{
StringValue: "weekly",
},
},
DisplayValue: "Weekly",
DisplayValue: TemplateIntervalWeekly,
},
{
Value: &settingsmsg.ListOptionValue{
Option: &settingsmsg.ListOptionValue_StringValue{
StringValue: "never",
},
},
DisplayValue: "Never",
DisplayValue: TemplateIntervalNever,
},
},
},
Expand Down
Loading

0 comments on commit 7947c99

Please sign in to comment.