diff --git a/app/boot_levels.go b/app/boot_levels.go index 253532efd..c124e37d7 100644 --- a/app/boot_levels.go +++ b/app/boot_levels.go @@ -32,7 +32,6 @@ import ( "github.com/cortezaproject/corteza-server/pkg/scheduler" "github.com/cortezaproject/corteza-server/pkg/seeder" "github.com/cortezaproject/corteza-server/pkg/sentry" - "github.com/cortezaproject/corteza-server/pkg/slice" "github.com/cortezaproject/corteza-server/pkg/websocket" "github.com/cortezaproject/corteza-server/store" sysService "github.com/cortezaproject/corteza-server/system/service" @@ -494,7 +493,7 @@ func (app *CortezaApp) Activate(ctx context.Context) (err error) { updateAuthSettings(app.AuthService, appSettings) }) - updateLocaleSettings() + updateLocaleSettings(app.Opt.Locale) app.AuthService.Watch(ctx) @@ -597,50 +596,25 @@ func updateFederationSettings(opt options.FederationOpt, current *types.AppSetti // Sanitizes application (current) settings with languages from options // -// It updates ui.languages and resource-translations.languages slices -// -// # ui.languages -// These need to be a subset of LOCALE_LANGUAGES (we'll use the list of parsed languages) -// -// # resource-translations.languages +// It updates resource-translations.languages slice // These do not need to be subset of LOCALE_LANGUAGES but need to be valid language tags! -func updateLocaleSettings() { - configuredLanguages := func() []string { - tt := locale.Global().Tags() - out := make([]string, len(tt)) - for t := range tt { - out[t] = tt[t].String() - } - - return out - } - - updateUILanguages := func(appSettings *types.AppSettings) { - if len(appSettings.UI.Languages) == 0 { - appSettings.UI.Languages = configuredLanguages() - } else { - appSettings.UI.Languages = slice.IntersectStrings(configuredLanguages(), appSettings.UI.Languages) - } - } - +func updateLocaleSettings(opt options.LocaleOpt) { updateResourceLanguages := func(appSettings *types.AppSettings) { - if len(appSettings.UI.Languages) == 0 { - appSettings.ResourceTranslations.Languages = configuredLanguages() + out := make([]string, 0, 8) + + if opt.ResourceTranslationsEnabled { + for _, t := range locale.Global().Tags() { + out = append(out, t.String()) + } } else { - appSettings.ResourceTranslations.Languages = slice.IntersectStrings(configuredLanguages(), appSettings.UI.Languages) + // when resource translation is disabled, + // add only default (first) language to the list + out = append(out, locale.Global().Default().Tag.String()) } + + appSettings.ResourceTranslations.Languages = out } - updateUILanguages(sysService.CurrentSettings) - sysService.DefaultSettings.Register("ui.languages", func(ctx context.Context, current interface{}, _ types.SettingValueSet) { - appSettings, is := current.(*types.AppSettings) - if !is { - return - } - - updateUILanguages(appSettings) - }) - updateResourceLanguages(sysService.CurrentSettings) sysService.DefaultSettings.Register("resource-translations.languages", func(ctx context.Context, current interface{}, _ types.SettingValueSet) { appSettings, is := current.(*types.AppSettings) diff --git a/compose/service/locale.gen.go b/compose/service/locale.gen.go index 0b278a32e..a2eeecef3 100644 --- a/compose/service/locale.gen.go +++ b/compose/service/locale.gen.go @@ -13,17 +13,24 @@ package service import ( "context" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/actionlog" intAuth "github.com/cortezaproject/corteza-server/pkg/auth" "github.com/cortezaproject/corteza-server/pkg/errors" "github.com/cortezaproject/corteza-server/pkg/locale" + "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/store" systemTypes "github.com/cortezaproject/corteza-server/system/types" + "golang.org/x/text/language" ) type ( + localeAccessControl interface { + CanManageResourceTranslations(ctx context.Context) bool + } + resourceTranslationsManager struct { actionlog actionlog.Recorder locale locale.Resource @@ -228,3 +235,42 @@ func (svc resourceTranslationsManager) Page(ctx context.Context, namespaceID uin tmp, err := svc.pageExtended(ctx, res) return append(out, tmp...), err } + +func updateTranslations(ctx context.Context, ac localeAccessControl, lsvc ResourceTranslationsManagerService, tt ...*locale.ResourceTranslation) error { + if lsvc == nil || lsvc.Locale() == nil || lsvc.Locale().Default() == nil { + // gracefully handle partial initializations + return nil + } + + var ( + // assuming options will not change after start + contentLang = lsvc.Locale().Default().Tag + ) + + if options.Locale().ResourceTranslationsEnabled { + contentLang = locale.GetContentLanguageFromContext(ctx) + // Resource translations enabled + if contentLang == language.Und { + // If no content-language meta (HTTP header) info was + // used, do not run update translations - we do not know + // what is the language that we're sending in + return nil + } + + if !lsvc.Locale().SupportedLang(contentLang) { + // unsupported language + return errors.InvalidData("unsupported language") + } + + if !ac.CanManageResourceTranslations(ctx) { + return errors.Unauthorized("not allowed to manage resource translations") + } + } + + locale.ResourceTranslationSet(tt).SetLanguage(contentLang) + if err := lsvc.Upsert(ctx, tt); err != nil { + return err + } + + return nil +} diff --git a/compose/service/locale.go b/compose/service/locale.go index 855bbdf98..e95976715 100644 --- a/compose/service/locale.go +++ b/compose/service/locale.go @@ -6,20 +6,12 @@ import ( "strings" "github.com/cortezaproject/corteza-server/compose/types" - "github.com/cortezaproject/corteza-server/pkg/errors" "github.com/cortezaproject/corteza-server/pkg/locale" - "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/store" "github.com/spf13/cast" "golang.org/x/text/language" ) -type ( - localeAccessControl interface { - CanManageResourceTranslations(ctx context.Context) bool - } -) - func (svc resourceTranslationsManager) moduleExtended(ctx context.Context, res *types.Module) (out locale.ResourceTranslationSet, err error) { var ( k types.LocaleKey @@ -191,42 +183,3 @@ func (svc resourceTranslationsManager) loadChart(ctx context.Context, s store.St _, m, err = loadChart(ctx, s, namespaceID, chartID) return m, err } - -func updateTranslations(ctx context.Context, ac localeAccessControl, lsvc ResourceTranslationsManagerService, tt ...*locale.ResourceTranslation) error { - if lsvc == nil || lsvc.Locale() == nil || lsvc.Locale().Default() == nil { - // gracefully handle partial initializations - return nil - } - - var ( - // assuming options will not change after start - contentLang = lsvc.Locale().Default().Tag - ) - - if options.Locale().ResourceTranslationsEnabled { - contentLang = locale.GetContentLanguageFromContext(ctx) - // Resource translations enabled - if contentLang == language.Und { - // If no content-language meta (HTTP header) info was - // used, do not run update translations - we do not know - // what is the language that we're sending in - return nil - } - - if !lsvc.Locale().SupportedLang(contentLang) { - // unsupported language - return errors.InvalidData("unsupported language") - } - - if !ac.CanManageResourceTranslations(ctx) { - return errors.Unauthorized("resource translation not supported") - } - } - - locale.ResourceTranslationSet(tt).SetLanguage(contentLang) - if err := lsvc.Upsert(ctx, tt); err != nil { - return err - } - - return nil -} diff --git a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl index a85e194d4..b17e4b43f 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl @@ -10,15 +10,22 @@ import ( {{ . }} {{- end }} + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/actionlog" intAuth "github.com/cortezaproject/corteza-server/pkg/auth" "github.com/cortezaproject/corteza-server/pkg/errors" "github.com/cortezaproject/corteza-server/pkg/locale" + "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/store" systemTypes "github.com/cortezaproject/corteza-server/system/types" + "golang.org/x/text/language" ) type ( + localeAccessControl interface { + CanManageResourceTranslations(ctx context.Context) bool + } + resourceTranslationsManager struct { actionlog actionlog.Recorder locale locale.Resource @@ -165,3 +172,42 @@ func (svc resourceTranslationsManager) {{ .Resource }}(ctx context.Context, {{ i } {{- end }} + +func updateTranslations(ctx context.Context, ac localeAccessControl, lsvc ResourceTranslationsManagerService, tt ...*locale.ResourceTranslation) error { + if lsvc == nil || lsvc.Locale() == nil || lsvc.Locale().Default() == nil { + // gracefully handle partial initializations + return nil + } + + var ( + // assuming options will not change after start + contentLang = lsvc.Locale().Default().Tag + ) + + if options.Locale().ResourceTranslationsEnabled { + contentLang = locale.GetContentLanguageFromContext(ctx) + // Resource translations enabled + if contentLang == language.Und { + // If no content-language meta (HTTP header) info was + // used, do not run update translations - we do not know + // what is the language that we're sending in + return nil + } + + if !lsvc.Locale().SupportedLang(contentLang) { + // unsupported language + return errors.InvalidData("unsupported language") + } + + if !ac.CanManageResourceTranslations(ctx) { + return errors.Unauthorized("not allowed to manage resource translations") + } + } + + locale.ResourceTranslationSet(tt).SetLanguage(contentLang) + if err := lsvc.Upsert(ctx, tt); err != nil { + return err + } + + return nil +} diff --git a/system/service/locale.go b/system/service/locale.go deleted file mode 100644 index e031dff67..000000000 --- a/system/service/locale.go +++ /dev/null @@ -1,39 +0,0 @@ -package service - -// func (svc resourceTranslationsManager) loadReport(ctx context.Context, s store.Storer, reportID uint64) (*types.Report, error) { -// return store.LookupReportByID(ctx, s, reportID) -// } - -// func (svc resourceTranslationsManager) reportExtended(ctx context.Context, res *types.Report) (out locale.ResourceTranslationSet, er error) { -// var ( -// k types.LocaleKey -// ) - -// for _, tag := range svc.locale.Tags() { -// for i, block := range res.Blocks { -// blockID := locale.ContentID(block.BlockID, i) -// rpl := strings.NewReplacer( -// "{{blockID}}", strconv.FormatUint(blockID, 10), -// ) - -// // base stuff -// k = types.LocaleKeyReportBlockTitle -// out = append(out, &locale.ResourceTranslation{ -// Resource: res.ResourceTranslation(), -// Lang: tag.String(), -// Key: rpl.Replace(k.Path), -// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), rpl.Replace(k.Path)), -// }) - -// k = types.LocaleKeyReportBlockDescription -// out = append(out, &locale.ResourceTranslation{ -// Resource: res.ResourceTranslation(), -// Lang: tag.String(), -// Key: rpl.Replace(k.Path), -// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), rpl.Replace(k.Path)), -// }) -// } -// } - -// return -// } diff --git a/system/types/app_settings.go b/system/types/app_settings.go index e8d17bf70..71e5e213d 100644 --- a/system/types/app_settings.go +++ b/system/types/app_settings.go @@ -172,16 +172,6 @@ type ( UI struct { MainLogo string `kv:"main-logo" json:"mainLogo"` IconLogo string `kv:"icon-logo" json:"iconLogo"` - - // List of all languages (UI translations) enabled and available - // - // Always a subset of all languages available - // in Corteza instance (LOCALE_LANGUAGES) - // - // 1st language in the set is also a default one - // - // Empty slice defaults to LOCALE_LANGUAGES - Languages []string `kv:"languages" json:"languages"` } `kv:"ui" json:"ui"` ResourceTranslations struct { @@ -189,7 +179,7 @@ type ( // available for resource translations (these are module names, // field labels, descriptions, ...) - // Always a subset of all languages available + // This is always a subset of all languages available // in Corteza instance (LOCALE_LANGUAGES) // // Note: later, we will enable this to contain languages