First usable version
This commit is contained in:
parent
0fd919670a
commit
57437f8d23
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) <year> <copyright holders>
|
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:
|
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:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
module git.jlel.se/jlelse/template-strings
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
|
@ -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=
|
|
@ -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...)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue