aboutsummaryrefslogtreecommitdiff
path: root/cmd/site/i18n.go
diff options
context:
space:
mode:
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
+}