go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

main.go (3187B)


      1 // Example: Set and apply language translation based on input, with and without Gettext.
      2 package main
      3 
      4 import (
      5 	"context"
      6 	"fmt"
      7 	"os"
      8 	"path"
      9 
     10 	testdataloader "github.com/peteole/testdata-loader"
     11 	gotext "gopkg.in/leonelquinteros/gotext.v1"
     12 
     13 	"git.defalsify.org/vise.git/lang"
     14 	"git.defalsify.org/vise.git/persist"
     15 	"git.defalsify.org/vise.git/engine"
     16 	"git.defalsify.org/vise.git/resource"
     17 	"git.defalsify.org/vise.git/state"
     18 	fsdb "git.defalsify.org/vise.git/db/fs"
     19 	"git.defalsify.org/vise.git/logging"
     20 )
     21 
     22 const (
     23 	USERFLAG_FLIP = iota + state.FLAG_USERSTART
     24 )
     25 
     26 var (
     27 	logg = logging.NewVanilla()
     28 	baseDir = testdataloader.GetBasePath()
     29 	scriptDir = path.Join(baseDir, "examples", "languages")
     30 	translationDir = path.Join(scriptDir, "locale")
     31 )
     32 
     33 func codeFromCtx(ctx context.Context) string {
     34 	var code string
     35 	logg.DebugCtxf(ctx, "in msg", "ctx", ctx, "val", code)
     36 	if ctx.Value("Language") != nil {
     37 		lang := ctx.Value("Language").(lang.Language)
     38 		code = lang.Code
     39 	}
     40 	return code
     41 }
     42 
     43 type langController struct {
     44 	translations map[string]gotext.Locale
     45 	State *state.State
     46 }
     47 
     48 func(l *langController) lang(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     49 	lang := "nor"
     50 	var rs resource.Result
     51 	if l.State.MatchFlag(USERFLAG_FLIP, true) {
     52 		lang = "eng"
     53 		rs.FlagReset = append(rs.FlagReset, USERFLAG_FLIP)
     54 	} else {
     55 		rs.FlagSet = append(rs.FlagSet, USERFLAG_FLIP)
     56 	}
     57 	rs.Content = lang
     58 	rs.FlagSet = append(rs.FlagSet, state.FLAG_LANG)
     59 	return rs, nil
     60 }
     61 
     62 func msg(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     63 	var r resource.Result
     64 	switch codeFromCtx(ctx)	{
     65 	case "nor":
     66 		r.Content = "Denne meldingen er fra en ekstern funksjon"
     67 	default:
     68 		r.Content = "This message is from an external function"
     69 	}
     70 	return r, nil
     71 }
     72 
     73 func(l *langController) moMsg(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     74 	var r resource.Result
     75 	code := codeFromCtx(ctx)
     76 	o := gotext.NewLocale(translationDir, code)
     77 	o.AddDomain("default")	
     78 	r.Content = o.Get("This message is translated using gettext")
     79 	logg.DebugCtxf(ctx, "lang", "code", code, "translateor", o)
     80 	return r, nil
     81 }
     82 
     83 func empty(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     84 	return resource.Result{
     85 		Content: "",
     86 	}, nil
     87 }
     88 
     89 func main() {
     90 	ctx := context.Background()
     91 	rsStore := fsdb.NewFsDb()
     92 	err := rsStore.Connect(ctx, scriptDir)
     93 	if err != nil {
     94 		panic(err)
     95 	}
     96 	rs := resource.NewDbResource(rsStore)
     97 
     98 	cfg := engine.Config{
     99 		Root: "root",
    100 		SessionId: "default",
    101 	}
    102 
    103 	dp := path.Join(scriptDir, ".state")
    104 	store := fsdb.NewFsDb()
    105 	err = store.Connect(ctx, dp)
    106 	if err != nil {
    107 		logg.ErrorCtxf(ctx, "db connect fail", "err", err)
    108 		os.Exit(1)
    109 	}
    110 	pr := persist.NewPersister(store)
    111 	st := state.NewState(1)
    112 	en := engine.NewEngine(cfg, rs)
    113 	en = en.WithState(st)
    114 	en = en.WithPersister(pr)
    115 
    116 	aux := &langController{
    117 		State: st,
    118 	}
    119 	rs.AddLocalFunc("swaplang", aux.lang)
    120 	rs.AddLocalFunc("msg", msg)
    121 	rs.AddLocalFunc("momsg", aux.moMsg)
    122 	rs.AddLocalFunc("empty", empty)
    123 
    124 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
    125 	if err != nil {
    126 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
    127 		os.Exit(1)
    128 	}
    129 }