From d335d635b34ce6ff51dfe733701229027f199fb3 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 23 Aug 2024 11:08:29 +0200 Subject: [PATCH] feat(graph): add TRANSLATION_PATH envvar Signed-off-by: jkoberg --- .../unreleased/graph-translation-path.md | 5 ++++ services/graph/README.md | 26 +++++++++++++++++++ services/graph/pkg/config/config.go | 1 + services/graph/pkg/l10n/translation.go | 4 +-- .../graph/pkg/service/v0/spacetemplates.go | 6 ++--- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 changelog/unreleased/graph-translation-path.md diff --git a/changelog/unreleased/graph-translation-path.md b/changelog/unreleased/graph-translation-path.md new file mode 100644 index 0000000000..7413d81f99 --- /dev/null +++ b/changelog/unreleased/graph-translation-path.md @@ -0,0 +1,5 @@ +Enhancement: Graph translation path + +Add `GRAPH_TRANSLATION_PATH` envvar like in other l10n services + +https://github.com/owncloud/ocis/pull/9902 diff --git a/services/graph/README.md b/services/graph/README.md index 394ba12c7a..f711ff06bb 100644 --- a/services/graph/README.md +++ b/services/graph/README.md @@ -63,3 +63,29 @@ The client that is used to authenticate with keycloak has to be able to list use * `view-authorization` Note that these roles are only available to assign if the client is in the `master` realm. + +## Translations + +The `graph` 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 `GRAPH_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 graph 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 `graph.po` (or `graph.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 +{GRAPH_TRANSLATION_PATH}/{language-code}/LC_MESSAGES/graph.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 `{GRAPH_TRANSLATION_PATH}/de_DE/LC_MESSAGES/graph.po`. + + + +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. See the `settings` service for a detailed description. diff --git a/services/graph/pkg/config/config.go b/services/graph/pkg/config/config.go index 31a9ce39b6..ab9fbe5295 100644 --- a/services/graph/pkg/config/config.go +++ b/services/graph/pkg/config/config.go @@ -46,6 +46,7 @@ type Spaces struct { GroupsCacheTTL int `yaml:"groups_cache_ttl" env:"GRAPH_SPACES_GROUPS_CACHE_TTL" desc:"Max TTL in seconds for the spaces groups cache." introductionVersion:"pre5.0"` StorageUsersAddress string `yaml:"storage_users_address" env:"GRAPH_SPACES_STORAGE_USERS_ADDRESS" desc:"The address of the storage-users service." 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;GRAPH_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:"%%NEXT%%"` } type LDAP struct { diff --git a/services/graph/pkg/l10n/translation.go b/services/graph/pkg/l10n/translation.go index d2f7c0994d..c30e2177c5 100644 --- a/services/graph/pkg/l10n/translation.go +++ b/services/graph/pkg/l10n/translation.go @@ -20,8 +20,8 @@ const ( ) // Translate translates a string based on the locale and default locale -func Translate(content, locale, defaultLocale string) string { - t := l10n.NewTranslatorFromCommonConfig(defaultLocale, _domain, "", _localeFS, _localeSubPath) +func Translate(content, locale, defaultLocale, translationPath string) string { + t := l10n.NewTranslatorFromCommonConfig(defaultLocale, _domain, translationPath, _localeFS, _localeSubPath) return t.Translate(content, locale) } diff --git a/services/graph/pkg/service/v0/spacetemplates.go b/services/graph/pkg/service/v0/spacetemplates.go index 75da3a7286..99c64ec41b 100644 --- a/services/graph/pkg/service/v0/spacetemplates.go +++ b/services/graph/pkg/service/v0/spacetemplates.go @@ -71,7 +71,7 @@ func (g Graph) applyDefaultTemplate(ctx context.Context, gwc gateway.GatewayAPIC opaque = utils.AppendPlainToOpaque(opaque, SpaceImageSpecialFolderName, iid) // upload readme.md - rid, err := readmeUpload(ctx, mdc, locale, g.config.Spaces.DefaultLanguage) + rid, err := readmeUpload(ctx, mdc, locale, g.config.Spaces.DefaultLanguage, g.config.Spaces.TranslationPath) if err != nil { return err } @@ -112,10 +112,10 @@ func imageUpload(ctx context.Context, mdc *metadata.CS3) (string, error) { return res.FileID, nil } -func readmeUpload(ctx context.Context, mdc *metadata.CS3, locale string, defaultLocale string) (string, error) { +func readmeUpload(ctx context.Context, mdc *metadata.CS3, locale string, defaultLocale string, translationPath string) (string, error) { res, err := mdc.Upload(ctx, metadata.UploadRequest{ Path: filepath.Join(_spaceFolderName, _readmeName), - Content: []byte(l10n_pkg.Translate(_readmeText, locale, defaultLocale)), + Content: []byte(l10n_pkg.Translate(_readmeText, locale, defaultLocale, translationPath)), }) if err != nil { return "", err