3
0

Implement access to lang lists in the (current) app settings

This commit is contained in:
Denis Arh
2021-09-15 14:26:22 +02:00
committed by Tomaž Jerman
parent 895b40da36
commit 1e02164c01
3 changed files with 88 additions and 3 deletions

View File

@@ -32,6 +32,7 @@ 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"
@@ -491,6 +492,8 @@ func (app *CortezaApp) Activate(ctx context.Context) (err error) {
updateAuthSettings(app.AuthService, appSettings)
})
updateLocaleSettings()
app.AuthService.Watch(ctx)
// messagebus reloader and consumer listeners
@@ -589,3 +592,60 @@ func updateAuthSettings(svc authServicer, current *types.AppSettings) {
func updateFederationSettings(opt options.FederationOpt, current *types.AppSettings) {
current.Federation.Enabled = opt.Enabled
}
// 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
// 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)
}
}
updateResourceLanguages := func(appSettings *types.AppSettings) {
if len(appSettings.UI.Languages) == 0 {
appSettings.ResourceTranslations.Languages = configuredLanguages()
} else {
appSettings.ResourceTranslations.Languages = slice.IntersectStrings(configuredLanguages(), appSettings.UI.Languages)
}
}
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)
if !is {
return
}
updateResourceLanguages(appSettings)
})
}

View File

@@ -98,8 +98,6 @@ func (ctrl *Settings) Set(ctx context.Context, r *request.SettingsSet) (interfac
// Current settings, structured
//
// This is available to all authenticated users
//
// @todo selectively apply subset of user's own settings (like ui.one.panes.*)
func (ctrl *Settings) Current(ctx context.Context, r *request.SettingsCurrent) (interface{}, error) {
return service.CurrentSettings, nil
}

View File

@@ -12,7 +12,7 @@ const (
type (
// AppSettings type is structured representation of all application settings
//
// Raw settings keys are hypen (kebab) case, separated with a dot (.) that indicates sub-level
// Raw settings keys are hyphen (kebab) case, separated with a dot (.) that indicates sub-level
// JSON properties for settings are NOT converted (lower-cased, etc...)
// Use `json:"-"` tag to hide settings on REST endpoint
AppSettings struct {
@@ -172,7 +172,34 @@ 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 {
// List of all languages (resource translations) enabled and
// available for resource translations (these are module names,
// field labels, descriptions, ...)
// Always a subset of all languages available
// in Corteza instance (LOCALE_LANGUAGES)
//
// Note: later, we will enable this to contain languages
// that are not part of 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:"resource-translations" json:"resourceTranslations"`
}
ExternalAuthProviderSet []*ExternalAuthProvider