Skip to content

Commit

Permalink
Minor locale code improvements, naming, @todos
Browse files Browse the repository at this point in the history
  • Loading branch information
darh authored and tjerman committed Sep 22, 2021
1 parent 75c5efb commit 04c6b7f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
11 changes: 10 additions & 1 deletion compose/service/locale.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ func ResourceTranslation(ls locale.Resource) *resourceTranslation {
return &resourceTranslation{
actionlog: DefaultActionlog,
store: DefaultStore,
ac: DefaultAccessControl,
locale: ls,
}
}

func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) {
// @todo AC
//if (!svc.ac.CanManageResourceTranslation(ctx)) {
// return *****ErrNotAllowedToCreate()
//}

// @todo validation

defer locale.Global().ReloadResourceTranslations(ctx)
me := auth.GetIdentityFromContext(ctx)

// - group by resource
Expand Down Expand Up @@ -91,6 +95,11 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra
if err != nil {
return err
}

// Reload ALL resource translations
// @todo we could probably do this more selectively and refresh only updated resources?
_ = locale.Global().ReloadResourceTranslations(ctx)

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/locale/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (l *Language) tResource(ns, key string, rr ...string) string {

// resourceTranslations returns all resource translations for the specified resource
func (l *Language) resourceTranslations(resource string) ResourceTranslationIndex {
l.l.RLock()
defer l.l.RUnlock()

out := make(ResourceTranslationIndex)
if l.resources == nil {
return out
Expand Down
47 changes: 18 additions & 29 deletions pkg/locale/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,24 @@ func (svc *service) ReloadStatic() (err error) {
// ReloadResourceTranslations all language configurations (as configured via path options) and
// all translation files
func (svc *service) ReloadResourceTranslations(ctx context.Context) (err error) {
if svc.s == nil {
return fmt.Errorf("store for locale service not set")
}

svc.l.RLock()
defer svc.l.RUnlock()

svc.log.Info("reloading resource translations",
zap.Strings("tags", tagsToStrings(svc.tags)),
)

for i, tag := range svc.tags {
for _, tag := range svc.tags {
lang, ok := svc.set[tag]
if !ok {
lang = &Language{
Tag: tag,
// @todo find a better name for this, because it's not the
// language that it's loaded from the store, the resource translations are
src: "store",
}

Expand All @@ -251,29 +257,11 @@ func (svc *service) ReloadResourceTranslations(ctx context.Context) (err error)
}

svc.log.Info(
"language loaded",
"resource translations loaded",
zap.Stringer("tag", lang.Tag),
zap.String("src", lang.src),
zap.Stringer("extends", lang.Extends),
zap.Int("translations", len(lang.resources)),
)

if i == 0 && svc.def == nil {
// set first one as default
svc.def = lang
}
}

// Do another pass and link all extended languages
for _, lang := range svc.set {
if lang.Extends.IsRoot() {
continue
}

if svc.set[lang.Extends] == nil {
return fmt.Errorf("could not extend langage %q from an unknown language %q", lang.Tag, lang.Extends)
}

lang.extends = svc.set[lang.Extends]
}

return nil
Expand Down Expand Up @@ -365,11 +353,11 @@ func (svc *service) EncodeExternal(w io.Writer, app string, ll ...language.Tag)
// Language is picked from the context
func (svc *service) NS(ctx context.Context, ns string) func(key string, rr ...string) string {
var (
code = GetLanguageFromContext(ctx)
tag = GetLanguageFromContext(ctx)
)

return func(key string, rr ...string) string {
return svc.t(code, ns, key, rr...)
return svc.t(tag, ns, key, rr...)
}
}

Expand All @@ -391,6 +379,7 @@ func (svc *service) TFor(tag language.Tag, ns, key string, rr ...string) string
//
// Language is picked from the context
func (svc *service) TResource(ctx context.Context, ns, key string, rr ...string) string {

return svc.tResource(GetLanguageFromContext(ctx), ns, key, rr...)
}

Expand All @@ -405,11 +394,11 @@ func (svc *service) TResourceFor(tag language.Tag, ns, key string, rr ...string)
// given resource.
//
// The response is indexed by translation key for nicer lookups.
func (svc *service) ResourceTranslations(code language.Tag, resource string) ResourceTranslationIndex {
func (svc *service) ResourceTranslations(tag language.Tag, resource string) ResourceTranslationIndex {
out := make(ResourceTranslationIndex)

if svc != nil && svc.set != nil {
if l, has := svc.set[code]; has {
if l, has := svc.set[tag]; has {
return l.resourceTranslations(resource)
}
}
Expand All @@ -418,9 +407,9 @@ func (svc *service) ResourceTranslations(code language.Tag, resource string) Res
}

// Finds language and uses it to translate the given key
func (svc *service) t(code language.Tag, ns, key string, rr ...string) string {
func (svc *service) t(tag language.Tag, ns, key string, rr ...string) string {
if svc != nil && svc.set != nil {
if l, has := svc.set[code]; has {
if l, has := svc.set[tag]; has {
return l.t(ns, key, rr...)
}
}
Expand All @@ -429,9 +418,9 @@ func (svc *service) t(code language.Tag, ns, key string, rr ...string) string {
}

// Finds language and uses it to translate the given key for resource
func (svc *service) tResource(code language.Tag, ns, key string, rr ...string) string {
func (svc *service) tResource(tag language.Tag, ns, key string, rr ...string) string {
if svc != nil && svc.set != nil {
if l, has := svc.set[code]; has {
if l, has := svc.set[tag]; has {
return l.tResource(ns, key, rr...)
}
}
Expand Down
2 changes: 2 additions & 0 deletions store/rdbms/resource_translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func (s Store) convertResourceTranslationFilter(f types.ResourceTranslationFilte

// TransformResource converts raw resource translations into the format used by
// the locale package.
//
// @todo this function knows too much (locale pkg), move it out of store
func (s *Store) TransformResource(ctx context.Context, lang language.Tag) (out map[string]map[string]*locale.ResourceTranslation, err error) {
out = make(map[string]map[string]*locale.ResourceTranslation)
var cc types.ResourceTranslationSet
Expand Down

0 comments on commit 04c6b7f

Please sign in to comment.