3
0

Add locale handling, translation dec/enc and serving

This commit is contained in:
Denis Arh
2021-08-10 06:29:50 +02:00
parent 64085fbd1b
commit 5d6945cec9
38 changed files with 55343 additions and 40 deletions
+5 -3
View File
@@ -5,6 +5,7 @@ import (
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
@@ -17,7 +18,7 @@ type (
// definitions are in multiple files and each definition
// should produce one output
actionsDef struct {
App string
Component string
Source string
outputDir string
@@ -109,13 +110,14 @@ func procActions(mm ...string) (dd []*actionsDef, err error) {
dd = make([]*actionsDef, 0)
for _, m := range mm {
err = func() error {
if f, err = os.Open(m); err != nil {
return err
}
defer f.Close()
d = &actionsDef{}
d = &actionsDef{Component: strings.SplitN(m, string(filepath.Separator), 2)[0]}
if err := yaml.NewDecoder(f).Decode(d); err != nil {
return err
@@ -200,7 +202,7 @@ func actionNormalize(d *actionsDef) error {
return nil
}
placeholderMatcher := regexp.MustCompile(`{(.+?)}`)
placeholderMatcher := regexp.MustCompile(`\{\{(.+?)\}\}`)
checkPlaceholders := func(def string, kind, s string) error {
for _, match := range placeholderMatcher.FindAllStringSubmatch(s, 1) {
placeholder := match[1]
+11 -7
View File
@@ -11,8 +11,9 @@ package {{ .Package }}
import (
"context"
"fmt"
"github.com/cortezaproject/corteza-server/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/locale"
{{- range .Import }}
{{ normalizeImport . }}
{{- end }}
@@ -100,7 +101,7 @@ func (p {{ $.Service }}ActionProps) Serialize() actionlog.Meta {
//
func (p {{ $.Service }}ActionProps) Format(in string, err error) string {
var (
pairs = []string{"{err}"}
pairs = []string{"{{"{{"}}err}}"}
{{- if $.Props }}
// first non-empty string
@@ -124,14 +125,14 @@ func (p {{ $.Service }}ActionProps) Format(in string, err error) string {
{{- range $prop := $.Props }}
{{- if $prop.Builtin }}
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}}", fns(p.{{ $prop.Name }}))
pairs = append(pairs, "{{"{{"}}{{ $prop.Name }}}}", fns(p.{{ $prop.Name }}))
{{- else }}
if p.{{ $prop.Name }} != nil {
// replacement for "{{"{"}}{{ $prop.Name }}}" (in order how fields are defined)
// replacement for "{{"{{"}}{{ $prop.Name }}}}" (in order how fields are defined)
pairs = append(
pairs,
"{{"{"}}{{ $prop.Name }}}",
"{{"{{"}}{{ $prop.Name }}}}",
fns(
{{- range $f := $prop.Fields }}
p.{{ $prop.Name }}.{{ camelCase " " $f }},
@@ -140,7 +141,7 @@ func (p {{ $.Service }}ActionProps) Format(in string, err error) string {
)
{{- range $f := $prop.Fields }}
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}.{{ $f }}}", fns(p.{{ $prop.Name }}.{{ camelCase " " $f }}))
pairs = append(pairs, "{{"{{"}}{{ $prop.Name }}.{{ $f }}}}", fns(p.{{ $prop.Name }}.{{ camelCase " " $f }}))
{{- end }}
}
{{- end }}
@@ -249,9 +250,12 @@ func {{ camelCase "" $.Service "Err" $e.Error }}(mm ... *{{ $.Service }}ActionPr
// action log entry; no formatting, it will be applied inside recordAction fn.
errors.Meta({{ $.Service }}LogMetaKey{}, {{ printf "%q" $e.Log }}),
{{ end -}}
errors.Meta({{ $.Service }}PropsMetaKey{}, p),
// translation namespace & key
errors.Meta(locale.ErrorMetaNamespace{}, {{ printf "%q" $.Component }}),
errors.Meta(locale.ErrorMetaKey{}, "{{ $.Service }}.errors.{{ $e.Error }}"),
errors.StackSkip(1),
)