commit d898a995014e183ff41c690021cb1af0a2a9e114
parent 7e1398b68a8bb879a594a8fd600ad1dbd99b14fc
Author: lash <dev@holbrook.no>
Date: Fri, 21 Apr 2023 21:58:19 +0100
Add language entry symbol lookup for fs resource
Diffstat:
2 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/engine/engine_test.go b/engine/engine_test.go
@@ -33,7 +33,7 @@ func NewFsWrapper(path string, st *state.State) FsWrapper {
st,
}
wr.AddLocalFunc("one", wr.one)
- wr.AddLocalFunc("inky", wr.inky)
+ //wr.AddLocalFunc("inky", wr.inky)
wr.AddLocalFunc("pinky", wr.pinky)
wr.AddLocalFunc("set_lang", wr.set_lang)
wr.AddLocalFunc("translate", wr.translate)
diff --git a/resource/fs.go b/resource/fs.go
@@ -30,9 +30,9 @@ func NewFsResource(path string) (FsResource) {
}
func(fsr FsResource) GetTemplate(sym string, ctx context.Context) (string, error) {
- v := ctx.Value("Language")
fp := path.Join(fsr.Path, sym)
fpl := fp
+ v := ctx.Value("Language")
if v != nil {
lang := v.(lang.Language)
fpl += "_" + lang.Code
@@ -41,15 +41,14 @@ func(fsr FsResource) GetTemplate(sym string, ctx context.Context) (string, error
var err error
r, err = ioutil.ReadFile(fpl)
if err != nil {
- Logg.DebugCtxf(ctx, "err", "err", err)
if errors.Is(err, os.ErrNotExist) {
if fpl != fp {
r, err = ioutil.ReadFile(fp)
- if err != nil {
- return "", err
- }
}
}
+ if err != nil {
+ return "", fmt.Errorf("failed getting template for sym '%s': %v", sym, err)
+ }
}
s := string(r)
return strings.TrimSpace(s), err
@@ -85,21 +84,32 @@ func(fsr FsResource) String() string {
}
func(fsr FsResource) getFunc(sym string, input []byte, ctx context.Context) (Result, error) {
- v := ctx.Value("language")
+ v := ctx.Value("Language")
if v == nil {
return fsr.getFuncNoCtx(sym, input, nil)
}
- language := v.(*lang.Language)
- return fsr.getFuncNoCtx(sym, input, language)
+ language := v.(lang.Language)
+ return fsr.getFuncNoCtx(sym, input, &language)
}
func(fsr FsResource) getFuncNoCtx(sym string, input []byte, language *lang.Language) (Result, error) {
fb := sym + ".txt"
fp := path.Join(fsr.Path, fb)
- Logg.Debugf("getfunc search dir", "dir", fsr.Path, "path", fp, "sym", sym, "language", language)
+ fpl := fp
+ if language != nil {
+ fpl += "_" + language.Code
+ }
+ Logg.Debugf("getfunc search dir", "dir", fsr.Path, "path", fp, "lang_path", fpl, "sym", sym, "language", language)
r, err := ioutil.ReadFile(fp)
if err != nil {
- return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
+ if errors.Is(err, os.ErrNotExist) {
+ if fpl != fp {
+ r, err = ioutil.ReadFile(fp)
+ }
+ }
+ if err != nil {
+ return Result{}, fmt.Errorf("failed getting data for sym '%s': %v", sym, err)
+ }
}
s := string(r)
return Result{