From 57437f8d23ee05fca32ba2dc7f2e9f667681f5e6 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 23 May 2021 10:04:39 +0200 Subject: [PATCH] First usable version --- LICENSE | 2 +- go.mod | 5 +++ go.sum | 4 +++ templatestrings.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 templatestrings.go diff --git a/LICENSE b/LICENSE index 2071b23..73a5796 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2021 Jan-Lukas Else Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0915e02 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.jlel.se/jlelse/template-strings + +go 1.16 + +require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e387ff0 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/templatestrings.go b/templatestrings.go new file mode 100644 index 0000000..2ceadd2 --- /dev/null +++ b/templatestrings.go @@ -0,0 +1,77 @@ +package templatestrings + +import ( + "os" + "path/filepath" + + "gopkg.in/yaml.v3" +) + +type TemplateStrings struct { + templateStrings map[string]map[string]string + defaultVariant string +} + +// Load the strings from the specified files into memory +func InitTemplateStrings(stringsDir string, fileExt string, defaultVariant string, otherVariants ...string) (*TemplateStrings, error) { + ts := &TemplateStrings{ + map[string]map[string]string{}, + defaultVariant, + } + // Load strings + variants := append(otherVariants, defaultVariant) + for _, variant := range variants { + if _, ok := ts.templateStrings[variant]; ok { + // Already loaded + continue + } + variantStrings := map[string]string{} + f, err := os.Open(filepath.Join(stringsDir, variant+fileExt)) + if err != nil { + if os.IsNotExist(err) { + continue + } + return nil, err + } + err = yaml.NewDecoder(f).Decode(variantStrings) + if err != nil { + return nil, err + } + ts.templateStrings[variant] = variantStrings + } + return ts, nil +} + +// Get a string +// input: lang, name or just name (uses default) +func (ts *TemplateStrings) GetTemplateStringVariant(input ...string) (result string) { + var lang, name string + if l := len(input); l == 1 { + // Use default variant + lang = ts.defaultVariant + name = input[0] + } else if l == 2 { + // Use specified variant + lang = input[0] + name = input[1] + } else { + // Wrong number of input strings + return "" + } + m, ok := ts.templateStrings[lang] + if !ok { + m = ts.templateStrings[ts.defaultVariant] + } + result, ok = m[name] + if !ok { + result = ts.templateStrings[ts.defaultVariant][name] + } + return +} + +// Get a func to use for templates +func (ts *TemplateStrings) GetTemplateStringVariantFunc() func(...string) string { + return func(s ...string) string { + return ts.GetTemplateStringVariant(s...) + } +}