aboutsummaryrefslogtreecommitdiff
path: root/cmd/site/i18n.go
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-10-31 23:10:04 +0000
committerChristine Dodrill <me@christine.website>2018-10-31 23:10:04 +0000
commit24d63f4c9832538a8b4d119f54df2507ffe46578 (patch)
tree95975a665382f7ebe3d7666e5fd0292092dd966d /cmd/site/i18n.go
parentb7493881049aaa908de04837d004ec5cf2d91b90 (diff)
downloadxesite-i18n.tar.xz
xesite-i18n.zip
add translationsi18n
Diffstat (limited to 'cmd/site/i18n.go')
-rw-r--r--cmd/site/i18n.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/cmd/site/i18n.go b/cmd/site/i18n.go
new file mode 100644
index 0000000..6c8fc92
--- /dev/null
+++ b/cmd/site/i18n.go
@@ -0,0 +1,46 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+)
+
+type locale map[string]map[string]string
+
+func (l locale) Value(group, key string, args ...interface{}) string {
+ sg, ok := l[group]
+ if !ok {
+ return "no group " + group
+ }
+
+ result, ok := sg[key]
+ if !ok {
+ return fmt.Sprintf("in group %s, no key %s", group, key)
+ }
+
+ return fmt.Sprintf(result, args...)
+}
+
+type translations struct {
+ locales map[string]locale
+}
+
+func (t *translations) LoadLocale(name string, r io.Reader) error {
+ l := locale{}
+ d := json.NewDecoder(r)
+ err := d.Decode(&l)
+ if err == nil {
+ t.locales[name] = l
+ }
+ return err
+}
+
+func (t *translations) Get(name string) (locale, bool) {
+ l, ok := t.locales[name]
+ if !ok {
+ return nil, false
+ }
+
+ return l, ok
+}